FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

correct D type for C long type

 
Post new topic   Reply to topic     Forum Index -> Bindings
View previous topic :: View next topic  
Author Message
maksim



Joined: 03 Feb 2011
Posts: 4
Location: Russia

PostPosted: Fri Feb 04, 2011 1:37 pm    Post subject: correct D type for C long type Reply with quote

Hello!

They say that for long type in *.h file we should use int in *.d file:
http://www.digitalmars.com/d/2.0/htod.html

But in some files in this project long and ulong types are used for long and unsigned long C types.

So, what way is correct?
Back to top
View user's profile Send private message
maksim



Joined: 03 Feb 2011
Posts: 4
Location: Russia

PostPosted: Sat Feb 05, 2011 9:02 am    Post subject: Reply with quote

Well, on my 64-bit platform:
C:
sizeof(int) = 4
sizeof(long) = 8
D:
int.sizeof = 4
long.sizeof = 8

So there is no problem if I compile my code with 64-bit libraries.

As far as I know, on 32-bit platforms sizeof(long) = 4 in C, but in D it's still 8 bytes.
May be it's better to have the following code somewhere?
Code:
version( LibName_32bit )
{
  alias int c_long;
  alias uint c_ulong;
}
else
{
  alias long c_long;
  alias ulong c_ulong;
}
Back to top
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Sun Feb 06, 2011 5:26 am    Post subject: Reply with quote

In D the size of types is always fixed, so in D the size of int will always be 4 and the size of long will always be 8. This is not the case in C. In C it depends on the platform is 32bit or 64it and if it's 64bit it depends on the data model that is used. There are four data models that exist but it's basically only two you need to worry about. They are:

LLP64 - used on Windows
LP64 - used on Posix

So you can use the following code to correctly bind C functions using the "long" type:

Code:
version( Windows )
{
   alias int   c_long;
   alias uint  c_ulong;
}

else // Posix
{
   static if ((void*).sizeof > int.sizeof) // 64bit
   {
      alias long  c_long;
      alias ulong c_ulong;
   }

   else
   {
      alias int   c_long;
      alias uint  c_ulong;
   }
}


Now whenever you bind a C function use "c_(u)long" instead of (u)long.

Read more about 64bit data models: http://en.wikipedia.org/wiki/64-bit
Back to top
View user's profile Send private message
maksim



Joined: 03 Feb 2011
Posts: 4
Location: Russia

PostPosted: Sun Feb 06, 2011 6:15 am    Post subject: Reply with quote

Thank you!

Edited:
It doesn't work. Features like "(void*).sizeof" and "version( X86_64 )" depend on complier not CPU or OS.
I use a 32-bit D compiler and it says "(void*).sizeof = 4", though my CPU and OS are both 64-bit.
When you link a program with C libraries you should find out the version of each library and indicate them explicitly:

In the program:
Code:
version = LibName_32bit;

In the binding file:
Code:
version( Windows )
{
   alias int c_long;
   alias uint c_ulong;
}
else // Posix
{
  version( LibName_32bit )
  {
    alias int c_long;
    alias uint c_ulong;
  }
  else
  {
    alias long c_long;
    alias ulong c_ulong;
  }
}
Back to top
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Mon Feb 07, 2011 11:08 am    Post subject: Reply with quote

Well, yes, "(void*).sizeof" would depend on the compiler and not on the CPU or OS. But, you should only link 32bit libraries with other 32bit libraries, even if you're running a 64bit OS. Currently DMD is only 32bit which mean you should link to other 32bit libraries regardless if you're OS is 64bit or not.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Bindings All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group