D is designed to fit comfortably with a C compiler for the target system. D makes up for not having its own VM by relying on the target environment's C runtime library. It would be senseless to attempt to port to D or write D wrappers for the vast array of C APIs available. How much easier it is to just call them directly.
This is done by matching the C compiler's data types, layouts, and function call/return sequences.
Calling C Functions
C functions can be called directly from D. There is no need for wrapper functions, argument swizzling, and the C functions do not need to be put into a separate DLL.
The C function must be declared and given a calling convention, most likely the "C" calling convention, for example:
extern (C) int strcmp(char *string1, char *string2);
and then it can be called within D code in the obvious way:
import string;
int myDfunction(char[] s)
{
return strcmp(string.toCharz(s), "foo\0");
}
There are several things going on here:
-
D understands how C function names are "mangled" and the correct C function call/return sequence.
-
C functions cannot be overloaded with another C function with the same name.
-
There are no __cdecl, __far, __stdcall, __declspec, or other such C type modifiers in D. These are handled by attributes, such as extern (C).
-
There are no const or volatile type modifiers in D. To declare a C function that uses those type modifiers, just drop those keywords from the declaration.
-
Strings are not 0 terminated in D. See "Data Type Compatibility" for more information about this.
C code can correspondingly call D functions, if the D functions use an attribute that is compatible with the C compiler, most likely the extern (C):
// myfunc() can be called from any C function
extern (C)
{
void myfunc(int a, int b)
{
...
}
}
Storage Allocation
C code explicitly manages memory with calls to malloc() and free(). D allocates memory using the D garbage collector, so no explicit free's are necessary.
D can still explicitly allocate memory using c.stdlib.malloc() and c.stdlib.free(), these are useful for connecting to C functions that expect malloc'd buffers, etc.
If pointers to D garbage collector allocated memory are passed to C functions, it's critical to ensure that that memory will not be collected by the garbage collector before the C function is done with it. This is accomplished by:
-
Making a copy of the data using c.stdlib.malloc() and passing the copy instead.
-
Leaving a pointer to it on the stack (as a parameter or automatic variable), as the garbage collector will scan the stack.
-
Leaving a pointer to it in the static data segment, as the garbage collector will scan the static data segment.
-
Registering the pointer with the garbage collector with the gc.addRoot() or gc.addRange() calls.
An interior pointer to the allocated memory block is sufficient to let the GC know the object is in use; i.e. it is not necessary to maintain a pointer to the beginning of the allocated memory.
The garbage collector does not scan the stacks of threads not created by the D Thread interface. Nor does it scan the data segments of other DLL's, etc.
D type
|
C type
|
void
|
void
|
bit
|
no equivalent
|
byte
|
signed char
|
ubyte
|
unsigned char
|
char
|
char (chars are unsigned in D)
|
wchar
|
wchar_t
|
short
|
short
|
ushort
|
unsigned short
|
int
|
int
|
uint
|
unsigned
|
long
|
long long
|
ulong
|
unsigned long long
|
float
|
float
|
double
|
double
|
extended
|
long double
|
imaginary
|
long double _Imaginary
|
complex
|
long double _Complex
|
type*
|
type *
|
type[dim]
|
type[dim]
|
type[]
|
no equivalent
|
type[type]
|
no equivalent
|
"string\0"
|
"string" or L"string"
|
class
|
no equivalent
|
type(*)(parameters)
|
type(*)(parameters)
|
These equivalents hold for most 32 bit C compilers. The C standard does not pin down the sizes of the types, so some care is needed.
Share with your friends: |