Debian Codegraph @

include/ffi.h.in

raw
1 /* -----------------------------------------------------------------*-C-*-
2 libffi @VERSION@
3 - Copyright (c) 2011, 2014, 2019, 2021, 2022, 2024, 2025 Anthony Green
4 - Copyright (c) 1996-2003, 2007, 2008 Red Hat, Inc.
5
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation
8 files (the ``Software''), to deal in the Software without
9 restriction, including without limitation the rights to use, copy,
10 modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25
26 ----------------------------------------------------------------------- */
27
28 /* -------------------------------------------------------------------
29 Most of the API is documented in doc/libffi.texi.
30
31 The raw API is designed to bypass some of the argument packing and
32 unpacking on architectures for which it can be avoided. Routines
33 are provided to emulate the raw API if the underlying platform
34 doesn't allow faster implementation.
35
36 More details on the raw API can be found in:
37
38 http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
39
40 and
41
42 http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
43 -------------------------------------------------------------------- */
44
45 #ifndef LIBFFI_H
46 #define LIBFFI_H
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 /* Specify which architecture libffi is configured for. */
53 #ifndef @TARGET@
54 #define @TARGET@
55 #endif
56
57 /* ---- System configuration information --------------------------------- */
58
59 /* If these change, update src/mips/ffitarget.h. */
60 #define FFI_TYPE_VOID 0
61 #define FFI_TYPE_INT 1
62 #define FFI_TYPE_FLOAT 2
63 #define FFI_TYPE_DOUBLE 3
64 #if @HAVE_LONG_DOUBLE@
65 #define FFI_TYPE_LONGDOUBLE 4
66 #else
67 #define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
68 #endif
69 #define FFI_TYPE_UINT8 5
70 #define FFI_TYPE_SINT8 6
71 #define FFI_TYPE_UINT16 7
72 #define FFI_TYPE_SINT16 8
73 #define FFI_TYPE_UINT32 9
74 #define FFI_TYPE_SINT32 10
75 #define FFI_TYPE_UINT64 11
76 #define FFI_TYPE_SINT64 12
77 #define FFI_TYPE_STRUCT 13
78 #define FFI_TYPE_POINTER 14
79 #define FFI_TYPE_COMPLEX 15
80
81 /* This should always refer to the last type code (for sanity checks). */
82 #define FFI_TYPE_LAST FFI_TYPE_COMPLEX
83
84 #include <ffitarget.h>
85
86 #ifndef LIBFFI_ASM
87
88 #if defined(_MSC_VER) && !defined(__clang__)
89 #define __attribute__(X)
90 #endif
91
92 #include <stddef.h>
93 #include <limits.h>
94
95 /* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
96 But we can find it either under the correct ANSI name, or under GNU
97 C's internal name. */
98
99 #define FFI_64_BIT_MAX 9223372036854775807
100
101 #ifdef LONG_LONG_MAX
102 # define FFI_LONG_LONG_MAX LONG_LONG_MAX
103 #else
104 # ifdef LLONG_MAX
105 # define FFI_LONG_LONG_MAX LLONG_MAX
106 # ifdef _AIX52 /* or newer has C99 LLONG_MAX */
107 # undef FFI_64_BIT_MAX
108 # define FFI_64_BIT_MAX 9223372036854775807LL
109 # endif /* _AIX52 or newer */
110 # else
111 # ifdef __GNUC__
112 # define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
113 # endif
114 # ifdef _AIX /* AIX 5.1 and earlier have LONGLONG_MAX */
115 # ifndef __PPC64__
116 # if defined (__IBMC__) || defined (__IBMCPP__)
117 # define FFI_LONG_LONG_MAX LONGLONG_MAX
118 # endif
119 # endif /* __PPC64__ */
120 # undef FFI_64_BIT_MAX
121 # define FFI_64_BIT_MAX 9223372036854775807LL
122 # endif
123 # endif
124 #endif
125
126 /* The closure code assumes that this works on pointers, i.e. a size_t
127 can hold a pointer. */
128
129 typedef struct _ffi_type
130 {
131 size_t size;
132 unsigned short alignment;
133 unsigned short type;
134 struct _ffi_type **elements;
135 } ffi_type;
136
137 /* Need minimal decorations for DLLs to work on Windows. GCC has
138 autoimport and autoexport. Always mark externally visible symbols
139 as dllimport for MSVC clients, even if it means an extra indirection
140 when using the static version of the library.
141 Besides, as a workaround, they can define FFI_BUILDING if they
142 *know* they are going to link with the static library. */
143 #if defined _MSC_VER && !defined(FFI_STATIC_BUILD)
144 # if defined FFI_BUILDING_DLL /* Building libffi.DLL with msvcc.sh */
145 # define FFI_API __declspec(dllexport)
146 # else /* Importing libffi.DLL */
147 # define FFI_API __declspec(dllimport)
148 # endif
149 #else
150 # define FFI_API
151 #endif
152
153 /* The externally visible type declarations also need the MSVC DLL
154 decorations, or they will not be exported from the object file. */
155 #if defined LIBFFI_HIDE_BASIC_TYPES
156 # define FFI_EXTERN FFI_API
157 #else
158 # define FFI_EXTERN extern FFI_API
159 #endif
160
161 #ifndef LIBFFI_HIDE_BASIC_TYPES
162 #if SCHAR_MAX == 127
163 # define ffi_type_uchar ffi_type_uint8
164 # define ffi_type_schar ffi_type_sint8
165 #else
166 #error "char size not supported"
167 #endif
168
169 #if SHRT_MAX == 32767
170 # define ffi_type_ushort ffi_type_uint16
171 # define ffi_type_sshort ffi_type_sint16
172 #elif SHRT_MAX == 2147483647
173 # define ffi_type_ushort ffi_type_uint32
174 # define ffi_type_sshort ffi_type_sint32
175 #else
176 #error "short size not supported"
177 #endif
178
179 #if INT_MAX == 32767
180 # define ffi_type_uint ffi_type_uint16
181 # define ffi_type_sint ffi_type_sint16
182 #elif INT_MAX == 2147483647
183 # define ffi_type_uint ffi_type_uint32
184 # define ffi_type_sint ffi_type_sint32
185 #elif INT_MAX == 9223372036854775807
186 # define ffi_type_uint ffi_type_uint64
187 # define ffi_type_sint ffi_type_sint64
188 #else
189 #error "int size not supported"
190 #endif
191
192 #if LONG_MAX == 2147483647
193 # if FFI_LONG_LONG_MAX != FFI_64_BIT_MAX
194 #error "no 64-bit data type supported"
195 # endif
196 #elif LONG_MAX != FFI_64_BIT_MAX
197 #error "long size not supported"
198 #endif
199
200 #if LONG_MAX == 2147483647
201 # define ffi_type_ulong ffi_type_uint32
202 # define ffi_type_slong ffi_type_sint32
203 #elif LONG_MAX == FFI_64_BIT_MAX
204 # define ffi_type_ulong ffi_type_uint64
205 # define ffi_type_slong ffi_type_sint64
206 #else
207 #error "long size not supported"
208 #endif
209
210 /* These are defined in types.c. */
211 FFI_EXTERN ffi_type ffi_type_void;
212 FFI_EXTERN ffi_type ffi_type_uint8;
213 FFI_EXTERN ffi_type ffi_type_sint8;
214 FFI_EXTERN ffi_type ffi_type_uint16;
215 FFI_EXTERN ffi_type ffi_type_sint16;
216 FFI_EXTERN ffi_type ffi_type_uint32;
217 FFI_EXTERN ffi_type ffi_type_sint32;
218 FFI_EXTERN ffi_type ffi_type_uint64;
219 FFI_EXTERN ffi_type ffi_type_sint64;
220 FFI_EXTERN ffi_type ffi_type_float;
221 FFI_EXTERN ffi_type ffi_type_double;
222 FFI_EXTERN ffi_type ffi_type_pointer;
223 FFI_EXTERN ffi_type ffi_type_longdouble;
224
225 #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
226 FFI_EXTERN ffi_type ffi_type_complex_float;
227 FFI_EXTERN ffi_type ffi_type_complex_double;
228 FFI_EXTERN ffi_type ffi_type_complex_longdouble;
229 #endif
230 #endif /* LIBFFI_HIDE_BASIC_TYPES */
231
232 typedef enum {
233 FFI_OK = 0,
234 FFI_BAD_TYPEDEF,
235 FFI_BAD_ABI,
236 FFI_BAD_ARGTYPE
237 } ffi_status;
238
239 typedef struct {
240 ffi_abi abi;
241 unsigned nargs;
242 ffi_type **arg_types;
243 ffi_type *rtype;
244 unsigned bytes;
245 unsigned flags;
246 #ifdef FFI_EXTRA_CIF_FIELDS
247 FFI_EXTRA_CIF_FIELDS;
248 #endif
249 } ffi_cif;
250
251 /* ---- Definitions for the raw API -------------------------------------- */
252
253 #ifndef FFI_SIZEOF_ARG
254 # if LONG_MAX == 2147483647
255 # define FFI_SIZEOF_ARG 4
256 # elif LONG_MAX == FFI_64_BIT_MAX
257 # define FFI_SIZEOF_ARG 8
258 # endif
259 #endif
260
261 #ifndef FFI_SIZEOF_JAVA_RAW
262 # define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
263 #endif
264
265 typedef union {
266 ffi_sarg sint;
267 ffi_arg uint;
268 float flt;
269 char data[FFI_SIZEOF_ARG];
270 void* ptr;
271 } ffi_raw;
272
273 #if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
274 /* This is a special case for mips64/n32 ABI (and perhaps others) where
275 sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8. */
276 typedef union {
277 signed int sint;
278 unsigned int uint;
279 float flt;
280 char data[FFI_SIZEOF_JAVA_RAW];
281 void* ptr;
282 } ffi_java_raw;
283 #else
284 typedef ffi_raw ffi_java_raw;
285 #endif
286
287
288 FFI_API
289 void ffi_raw_call (ffi_cif *cif,
290 void (*fn)(void),
291 void *rvalue,
292 ffi_raw *avalue);
293
294 FFI_API void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
295 FFI_API void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
296 FFI_API size_t ffi_raw_size (ffi_cif *cif);
297
298 /* This is analogous to the raw API, except it uses Java parameter
299 packing, even on 64-bit machines. I.e. on 64-bit machines longs
300 and doubles are followed by an empty 64-bit word. */
301
302 #if !FFI_NATIVE_RAW_API
303 FFI_API
304 void ffi_java_raw_call (ffi_cif *cif,
305 void (*fn)(void),
306 void *rvalue,
307 ffi_java_raw *avalue) __attribute__((deprecated));
308 #endif
309
310 FFI_API
311 void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw) __attribute__((deprecated));
312 FFI_API
313 void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args) __attribute__((deprecated));
314 FFI_API
315 size_t ffi_java_raw_size (ffi_cif *cif) __attribute__((deprecated));
316
317 /* ---- Version API ------------------------------------------------------ */
318
319 #define FFI_VERSION_STRING "@FFI_VERSION_STRING@"
320 #define FFI_VERSION_NUMBER @FFI_VERSION_NUMBER@
321
322 #ifndef LIBFFI_ASM
323 /* Return a version string. */
324 FFI_API const char *ffi_get_version (void);
325
326 /* Return the version as an unsigned long value: (x * 10000 + y * 100 + z) */
327 FFI_API unsigned long ffi_get_version_number (void);
328 #endif
329
330 /* ---- Internals API ---------------------------------------------------- */
331
332 FFI_API unsigned int ffi_get_default_abi (void);
333 FFI_API size_t ffi_get_closure_size (void);
334
335 /* ---- Definitions for closures ----------------------------------------- */
336
337 #if FFI_CLOSURES
338
339 #ifdef _MSC_VER
340 __declspec(align(8))
341 #endif
342 typedef struct {
343 #if @FFI_EXEC_TRAMPOLINE_TABLE@
344 void *trampoline_table;
345 void *trampoline_table_entry;
346 #else
347 union {
348 char tramp[FFI_TRAMPOLINE_SIZE];
349 void *ftramp;
350 };
351 #endif
352 ffi_cif *cif;
353 void (*fun)(ffi_cif*,void*,void**,void*);
354 void *user_data;
355 #if defined(_MSC_VER) && defined(_M_IX86)
356 void *padding;
357 #endif
358 } ffi_closure
359 #ifdef __GNUC__
360 __attribute__((aligned (8)))
361 #endif
362 ;
363
364 #ifndef __GNUC__
365 # ifdef __sgi
366 # pragma pack 0
367 # endif
368 #endif
369
370 FFI_API void *ffi_closure_alloc (size_t size, void **code);
371 FFI_API void ffi_closure_free (void *);
372
373 FFI_API ffi_status
374 ffi_prep_closure (ffi_closure*,
375 ffi_cif *,
376 void (*fun)(ffi_cif*,void*,void**,void*),
377 void *user_data)
378 #if defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 405)
379 __attribute__((deprecated ("use ffi_prep_closure_loc instead")))
380 #elif defined(__GNUC__) && __GNUC__ >= 3
381 __attribute__((deprecated))
382 #endif
383 ;
384
385 FFI_API ffi_status
386 ffi_prep_closure_loc (ffi_closure*,
387 ffi_cif *,
388 void (*fun)(ffi_cif*,void*,void**,void*),
389 void *user_data,
390 void *codeloc);
391
392 #ifdef __sgi
393 # pragma pack 8
394 #endif
395 typedef struct {
396 #if @FFI_EXEC_TRAMPOLINE_TABLE@
397 void *trampoline_table;
398 void *trampoline_table_entry;
399 #else
400 char tramp[FFI_TRAMPOLINE_SIZE];
401 #endif
402 ffi_cif *cif;
403
404 #if !FFI_NATIVE_RAW_API
405
406 /* If this is enabled, then a raw closure has the same layout
407 as a regular closure. We use this to install an intermediate
408 handler to do the translation, void** -> ffi_raw*. */
409
410 void (*translate_args)(ffi_cif*,void*,void**,void*);
411 void *this_closure;
412
413 #endif
414
415 void (*fun)(ffi_cif*,void*,ffi_raw*,void*);
416 void *user_data;
417
418 } ffi_raw_closure;
419
420 typedef struct {
421 #if @FFI_EXEC_TRAMPOLINE_TABLE@
422 void *trampoline_table;
423 void *trampoline_table_entry;
424 #else
425 char tramp[FFI_TRAMPOLINE_SIZE];
426 #endif
427
428 ffi_cif *cif;
429
430 #if !FFI_NATIVE_RAW_API
431
432 /* If this is enabled, then a raw closure has the same layout
433 as a regular closure. We use this to install an intermediate
434 handler to do the translation, void** -> ffi_raw*. */
435
436 void (*translate_args)(ffi_cif*,void*,void**,void*);
437 void *this_closure;
438
439 #endif
440
441 void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
442 void *user_data;
443
444 } ffi_java_raw_closure;
445
446 FFI_API ffi_status
447 ffi_prep_raw_closure (ffi_raw_closure*,
448 ffi_cif *cif,
449 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
450 void *user_data);
451
452 FFI_API ffi_status
453 ffi_prep_raw_closure_loc (ffi_raw_closure*,
454 ffi_cif *cif,
455 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
456 void *user_data,
457 void *codeloc);
458
459 #if !FFI_NATIVE_RAW_API
460 FFI_API ffi_status
461 ffi_prep_java_raw_closure (ffi_java_raw_closure*,
462 ffi_cif *cif,
463 void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
464 void *user_data) __attribute__((deprecated));
465
466 FFI_API ffi_status
467 ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
468 ffi_cif *cif,
469 void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
470 void *user_data,
471 void *codeloc) __attribute__((deprecated));
472 #endif
473
474 #endif /* FFI_CLOSURES */
475
476 #ifdef FFI_GO_CLOSURES
477
478 typedef struct {
479 void *tramp;
480 ffi_cif *cif;
481 void (*fun)(ffi_cif*,void*,void**,void*);
482 } ffi_go_closure;
483
484 FFI_API ffi_status ffi_prep_go_closure (ffi_go_closure*, ffi_cif *,
485 void (*fun)(ffi_cif*,void*,void**,void*));
486
487 FFI_API void ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
488 void **avalue, void *closure);
489
490 #endif /* FFI_GO_CLOSURES */
491
492 /* ---- Public interface definition -------------------------------------- */
493
494 FFI_API
495 ffi_status ffi_prep_cif(ffi_cif *cif,
496 ffi_abi abi,
497 unsigned int nargs,
498 ffi_type *rtype,
499 ffi_type **atypes);
500
501 FFI_API
502 ffi_status ffi_prep_cif_var(ffi_cif *cif,
503 ffi_abi abi,
504 unsigned int nfixedargs,
505 unsigned int ntotalargs,
506 ffi_type *rtype,
507 ffi_type **atypes);
508
509 FFI_API
510 void ffi_call(ffi_cif *cif,
511 void (*fn)(void),
512 void *rvalue,
513 void **avalue);
514
515 FFI_API
516 ffi_status ffi_get_struct_offsets (ffi_abi abi, ffi_type *struct_type,
517 size_t *offsets);
518
519 /* Convert between closure and function pointers. */
520 #if defined(PA_LINUX) || defined(PA_HPUX)
521 #define FFI_FN(f) ((void (*)(void))((unsigned int)(f) | 2))
522 #define FFI_CL(f) ((void *)((unsigned int)(f) & ~3))
523 #else
524 #define FFI_FN(f) ((void (*)(void))f)
525 #define FFI_CL(f) ((void *)(f))
526 #endif
527
528 /* ---- Definitions shared with assembly code ---------------------------- */
529
530 #endif
531
532 #ifdef __cplusplus
533 }
534 #endif
535
536 #endif