forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaceToC.dd
407 lines (316 loc) · 8.11 KB
/
interfaceToC.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
Ddoc
$(SPEC_S Interfacing to C,
$(P 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.
)
$(P This is done by matching the C compiler's data types, layouts,
and function call/return sequences.
)
<h2>Calling C Functions</h2>
$(P 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.
)
$(P 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);
------
$(P and then it can be called within D code in the obvious way:)
------
import std.string;
int myDfunction(char[] s)
{
return strcmp(std.string.toStringz(s), "foo");
}
------
$(P There are several things going on here:)
$(UL
$(LI D understands how C function names are "mangled" and the
correct C function call/return sequence.)
$(LI C functions cannot be overloaded with another C function
with the same name.)
$(LI There are no __cdecl, __far, __stdcall, __declspec, or other
such C type modifiers in D. These are handled by attributes, such
as $(TT extern (C)).)
$(LI 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.)
$(LI Strings are not 0 terminated in D. See "Data Type Compatibility"
for more information about this. However, string literals in D are
0 terminated.)
)
$(P 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)
{
...
}
}
------
<h2>Storage Allocation</h2>
$(P 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.
)
$(P D can still explicitly allocate memory using std.c.stdlib.malloc()
and std.c.stdlib.free(), these are useful for connecting to C
functions that expect malloc'd buffers, etc.
)
$(P 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:
)
$(UL
$(LI Making a copy of the data using std.c.stdlib.malloc() and passing
the copy instead.)
$(LI Leaving a pointer to it on the stack (as a parameter or
automatic variable), as the garbage collector will scan the stack.)
$(LI Leaving a pointer to it in the static data segment, as the
garbage collector will scan the static data segment.)
$(LI Registering the pointer with the garbage collector with the
std.gc.addRoot() or std.gc.addRange() calls.)
)
$(P 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.
)
$(P 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.
)
<h2>Data Type Compatibility</h2>
$(TABLE1
<caption>D And C Type Equivalence</caption>
$(TR
$(TH D type)
$(TH C type)
)
$(TR
$(TD $(B void))
$(TD $(B void))
)
$(TR
$(TD $(B byte))
$(TD $(B signed char))
)
$(TR
$(TD $(B ubyte))
$(TD $(B unsigned char))
)
$(TR
$(TD $(B char))
$(TD $(B char) (chars are unsigned in D))
)
$(TR
$(TD $(B wchar))
$(TD $(B wchar_t) (when sizeof(wchar_t) is 2))
)
$(TR
$(TD $(B dchar))
$(TD $(B wchar_t) (when sizeof(wchar_t) is 4))
)
$(TR
$(TD $(B short))
$(TD $(B short))
)
$(TR
$(TD $(B ushort))
$(TD $(B unsigned short))
)
$(TR
$(TD $(B int))
$(TD $(B int))
)
$(TR
$(TD $(B uint))
$(TD $(B unsigned))
)
$(TR
$(TD $(B long))
$(TD $(B long long))
)
$(TR
$(TD $(B ulong))
$(TD $(B unsigned long long))
)
$(TR
$(TD $(B float))
$(TD $(B float))
)
$(TR
$(TD $(B double))
$(TD $(B double))
)
$(TR
$(TD $(B real))
$(TD $(B long double))
)
$(TR
$(TD $(B ifloat))
$(TD $(B float _Imaginary))
)
$(TR
$(TD $(B idouble))
$(TD $(B double _Imaginary))
)
$(TR
$(TD $(B ireal))
$(TD $(B long double _Imaginary))
)
$(TR
$(TD $(B cfloat))
$(TD $(B float _Complex))
)
$(TR
$(TD $(B cdouble))
$(TD $(B double _Complex))
)
$(TR
$(TD $(B creal))
$(TD $(B long double _Complex))
)
$(TR
$(TD $(B struct))
$(TD $(B struct))
)
$(TR
$(TD $(B union))
$(TD $(B union))
)
$(TR
$(TD $(B enum))
$(TD $(B enum))
)
$(TR
$(TD $(B class))
$(TD no equivalent)
)
$(TR
$(TD $(I type)$(B *))
$(TD $(I type) $(B *))
)
$(TR
$(TD $(I type)$(B [)$(I dim)$(B ]))
$(TD $(I type)$(B [)$(I dim)$(B ]))
)
$(TR
$(TD $(I type)$(B [)$(I dim)$(B ]*))
$(TD $(I type)$(B (*)[)$(I dim)$(B ]))
)
$(TR
$(TD $(I type)$(B []))
$(TD no equivalent)
)
$(TR
$(TD $(I type)$(B [)$(I type)$(B ]))
$(TD no equivalent)
)
$(TR
$(TD $(I type) $(B function)$(B $(LPAREN))$(I parameters)$(B $(RPAREN)))
$(TD $(I type)$(B (*))$(B $(LPAREN))$(I parameters)$(B $(RPAREN)))
)
$(TR
$(TD $(I type) $(B delegate)$(B $(LPAREN))$(I parameters)$(B $(RPAREN)))
$(TD no equivalent)
)
)
$(P 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.
)
$(V2
<h2>Passing D Array Arguments to C Functions</h2>
$(P In C, arrays are passed to functions as pointers even if the function
prototype says its an array. In D, static arrays are passed by value,
not by reference. Thus, the function prototype must be adjusted to match
what C expects.)
$(TABLE1
<caption>D And C Function Prototype Equivalence</caption>
$(TR
$(TH D type)
$(TH C type)
)
$(TR
$(TD $(I T)$(B *))
$(TD $(I T)$(B []))
)
$(TR
$(TD $(B ref) $(I T)$(B [)$(I dim)$(B ]))
$(TD $(I T)$(B [)$(I dim)$(B ]))
)
)
$(P For example:)
$(CCODE
void foo(int a[3]) { ... } // C code
)
---
extern (C)
{
void foo(ref int[3] a); // D prototype
}
---
)
<h2>Calling printf()</h2>
$(P This mostly means checking that the printf format specifier
matches the corresponding D data type.
Although printf is designed to handle 0 terminated strings,
not D dynamic arrays of chars, it turns out that since D
dynamic arrays are a length followed by a pointer to the data,
the $(TT %.*s) format works perfectly:
)
------
void foo(char[] string)
{
printf("my string is: %.*s\n", string);
}
------
$(P The $(CODE printf) format string literal
in the example doesn't end with $(CODE '\0').
This is because string literals,
when they are not part of an initializer to a larger data structure,
have a $(CODE '\0') character helpfully stored after the end of them.
)
$(P An improved D function for formatted output is
$(CODE std.stdio.writef()).
)
<h2>Structs and Unions</h2>
$(P D structs and unions are analogous to C's.
)
$(P C code often adjusts the alignment and packing of struct members
with a command line switch or with various implementation specific
#pragma's. D supports explicit alignment attributes that correspond
to the C compiler's rules. Check what alignment the C code is using,
and explicitly set it for the D struct declaration.
)
$(P D does not support bit fields. If needed, they can be emulated
with shift and mask operations.
$(LINK2 htod.html, htod) will convert bit fields to inline functions that
do the right shift and masks.
)
$(V1
<hr>
<h1>Interfacing to C++</h1>
$(P D does not provide an interface to C++, other than
through $(LINK2 ../COM.html, COM programming). Since D, however,
interfaces directly to C, it can interface directly to
C++ code if it is declared as having C linkage.
)
$(P D class objects are incompatible with C++ class objects.
)
)
)
Macros:
TITLE=Interfacing to C
WIKI=InterfaceToC
CATEGORY_SPEC=$0