| 1 |
#ifndef FFI_PLATYPUS_H |
| 2 |
#define FFI_PLATYPUS_H |
| 3 |
|
| 4 |
#include <ffi.h> |
| 5 |
#include "ffi_platypus_config.h" |
| 6 |
|
| 7 |
#ifdef HAVE_DLFCN_H |
| 8 |
#ifndef PERL_OS_WINDOWS |
| 9 |
#include <dlfcn.h> |
| 10 |
#endif |
| 11 |
#endif |
| 12 |
#ifdef HAVE_UNISTD_H |
| 13 |
#include <unistd.h> |
| 14 |
#endif |
| 15 |
#ifdef HAVE_STDLIB_H |
| 16 |
#include <stdlib.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_STDDEF_H |
| 19 |
#include <stddef.h> |
| 20 |
#endif |
| 21 |
#ifdef HAVE_STDINT_H |
| 22 |
#include <stdint.h> |
| 23 |
#endif |
| 24 |
#ifdef HAVE_INTTYPES_H |
| 25 |
#include <inttypes.h> |
| 26 |
#endif |
| 27 |
#ifdef HAVE_ALLOCA_H |
| 28 |
#include <alloca.h> |
| 29 |
#endif |
| 30 |
#ifdef HAVE_STRING_H |
| 31 |
#include <string.h> |
| 32 |
#endif |
| 33 |
#ifdef HAVE_COMPLEX_H |
| 34 |
#include <complex.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifdef __cplusplus |
| 38 |
extern "C" { |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifdef PERL_OS_WINDOWS |
| 42 |
|
| 43 |
void *windlopen(const char *, int); |
| 44 |
const char *windlerror(void); |
| 45 |
void *windlsym(void *, const char *); |
| 46 |
int windlclose(void *); |
| 47 |
|
| 48 |
#define dlopen(filename, flag) windlopen(filename, flag) |
| 49 |
#define dlerror() windlerror() |
| 50 |
#define dlsym(handle, symbol) windlsym(handle, symbol) |
| 51 |
#define dlclose(handle) windlclose(handle) |
| 52 |
|
| 53 |
#endif |
| 54 |
|
| 55 |
typedef enum _ffi_pl_type_code { |
| 56 |
|
| 57 |
/* |
| 58 |
* the first three bits represent the |
| 59 |
* native unit size for each type. |
| 60 |
*/ |
| 61 |
FFI_PL_SIZE_0 = 0x0000, |
| 62 |
FFI_PL_SIZE_8 = 0x0001, |
| 63 |
FFI_PL_SIZE_16 = 0x0002, |
| 64 |
FFI_PL_SIZE_32 = 0x0003, |
| 65 |
FFI_PL_SIZE_64 = 0x0004, |
| 66 |
FFI_PL_SIZE_128 = 0x0005, |
| 67 |
FFI_PL_SIZE_256 = 0x0006, |
| 68 |
FFI_PL_SIZE_512 = 0x0007, |
| 69 |
#if SIZEOF_VOIDP == 4 |
| 70 |
FFI_PL_SIZE_PTR = FFI_PL_SIZE_32, |
| 71 |
#elif SIZEOF_VOIDP == 8 |
| 72 |
FFI_PL_SIZE_PTR = FFI_PL_SIZE_64, |
| 73 |
#else |
| 74 |
#error "strange pointer size" |
| 75 |
#endif |
| 76 |
FFI_PL_SIZE_MASK = 0x0007, |
| 77 |
|
| 78 |
/* |
| 79 |
* The next nine bits represent the type: |
| 80 |
* basics: void, integer, float, complex |
| 81 |
* |
| 82 |
* opaque is a pointer to something, a void* |
| 83 |
* |
| 84 |
* string is a pointer to a null terminated string |
| 85 |
* (a c string, basically) |
| 86 |
* |
| 87 |
* closure is a pointer to a function, usually a |
| 88 |
* Perl function, enclosed within a FFI::Platypus::Closure |
| 89 |
* |
| 90 |
* record is a fixed bitmap, could be either a struct, |
| 91 |
* or a fixed length string. |
| 92 |
*/ |
| 93 |
FFI_PL_BASE_VOID = 0x0008, |
| 94 |
FFI_PL_BASE_SINT = 0x0010, |
| 95 |
FFI_PL_BASE_UINT = 0x0020, |
| 96 |
FFI_PL_BASE_FLOAT = 0x0040, |
| 97 |
FFI_PL_BASE_COMPLEX = 0x0080, |
| 98 |
FFI_PL_BASE_OPAQUE = 0x0100, |
| 99 |
FFI_PL_BASE_STRING = 0x0200, |
| 100 |
FFI_PL_BASE_CLOSURE = 0x0400, |
| 101 |
FFI_PL_BASE_RECORD = 0x0800, |
| 102 |
FFI_PL_BASE_MASK = 0x0ff8, |
| 103 |
|
| 104 |
/* |
| 105 |
* The shape describes how the data is organized. |
| 106 |
* sclar is a simple value, pointer is usually used |
| 107 |
* for pass by reference, array is a list of objects |
| 108 |
* and custom types allow users to create their own |
| 109 |
* custom types. |
| 110 |
*/ |
| 111 |
FFI_PL_SHAPE_SCALAR = 0x0000, |
| 112 |
FFI_PL_SHAPE_POINTER = 0x1000, |
| 113 |
FFI_PL_SHAPE_ARRAY = 0x2000, |
| 114 |
FFI_PL_SHAPE_CUSTOM_PERL = 0x3000, |
| 115 |
FFI_PL_SHAPE_OBJECT = 0x4000, |
| 116 |
FFI_PL_SHAPE_MASK = 0xf000, |
| 117 |
|
| 118 |
/* |
| 119 |
* You can or together the different bit fields above to |
| 120 |
* describe a type. An int for example (usually signed 32 bit integer) |
| 121 |
* is `FFI_PL_SIZE_32 | FFI_PL_BASE_SINT`. Not all combinations |
| 122 |
* have meaning, for example `FFI_PL_SIZE_8 | FFI_PL_BASE_FLOAT` |
| 123 |
* is gibberish |
| 124 |
*/ |
| 125 |
FFI_PL_TYPE_VOID = FFI_PL_SIZE_0 | FFI_PL_BASE_VOID, |
| 126 |
FFI_PL_TYPE_SINT8 = FFI_PL_SIZE_8 | FFI_PL_BASE_SINT, |
| 127 |
FFI_PL_TYPE_SINT16 = FFI_PL_SIZE_16 | FFI_PL_BASE_SINT, |
| 128 |
FFI_PL_TYPE_SINT32 = FFI_PL_SIZE_32 | FFI_PL_BASE_SINT, |
| 129 |
FFI_PL_TYPE_SINT64 = FFI_PL_SIZE_64 | FFI_PL_BASE_SINT, |
| 130 |
FFI_PL_TYPE_UINT8 = FFI_PL_SIZE_8 | FFI_PL_BASE_UINT, |
| 131 |
FFI_PL_TYPE_UINT16 = FFI_PL_SIZE_16 | FFI_PL_BASE_UINT, |
| 132 |
FFI_PL_TYPE_UINT32 = FFI_PL_SIZE_32 | FFI_PL_BASE_UINT, |
| 133 |
FFI_PL_TYPE_UINT64 = FFI_PL_SIZE_64 | FFI_PL_BASE_UINT, |
| 134 |
FFI_PL_TYPE_FLOAT = FFI_PL_SIZE_32 | FFI_PL_BASE_FLOAT, |
| 135 |
FFI_PL_TYPE_DOUBLE = FFI_PL_SIZE_64 | FFI_PL_BASE_FLOAT, |
| 136 |
FFI_PL_TYPE_LONG_DOUBLE = FFI_PL_SIZE_128 | FFI_PL_BASE_FLOAT, |
| 137 |
FFI_PL_TYPE_COMPLEX_FLOAT = FFI_PL_SIZE_64 | FFI_PL_BASE_COMPLEX, |
| 138 |
FFI_PL_TYPE_COMPLEX_DOUBLE = FFI_PL_SIZE_128 | FFI_PL_BASE_COMPLEX, |
| 139 |
|
| 140 |
FFI_PL_TYPE_OPAQUE = FFI_PL_SIZE_PTR | FFI_PL_BASE_OPAQUE, |
| 141 |
|
| 142 |
/* |
| 143 |
* These types are passed as pointers, and act like opaque types |
| 144 |
* in terms of sizeof, alignof, etc, but get passed differently. |
| 145 |
*/ |
| 146 |
FFI_PL_TYPE_STRING = FFI_PL_TYPE_OPAQUE | FFI_PL_BASE_STRING, |
| 147 |
FFI_PL_TYPE_CLOSURE = FFI_PL_TYPE_OPAQUE | FFI_PL_BASE_CLOSURE, |
| 148 |
FFI_PL_TYPE_RECORD = FFI_PL_TYPE_OPAQUE | FFI_PL_BASE_RECORD, |
| 149 |
FFI_PL_TYPE_RECORD_VALUE = FFI_PL_BASE_RECORD, |
| 150 |
} ffi_pl_type_code; |
| 151 |
|
| 152 |
typedef enum _platypus_string_type { |
| 153 |
FFI_PL_TYPE_STRING_RO = 0, |
| 154 |
FFI_PL_TYPE_STRING_RW = 1 |
| 155 |
} platypus_string_type; |
| 156 |
|
| 157 |
typedef struct _ffi_pl_type_extra_object { |
| 158 |
char *class; /* base class */ |
| 159 |
} ffi_pl_type_extra_object; |
| 160 |
|
| 161 |
typedef struct _ffi_pl_record_meta_t { |
| 162 |
ffi_type ffi_type; |
| 163 |
int can_return_from_closure; |
| 164 |
ffi_type *elements[0]; |
| 165 |
} ffi_pl_record_meta_t; |
| 166 |
|
| 167 |
typedef struct _ffi_pl_type_extra_record { |
| 168 |
size_t size; |
| 169 |
char *class; /* base class */ |
| 170 |
ffi_pl_record_meta_t *meta; |
| 171 |
} ffi_pl_type_extra_record; |
| 172 |
|
| 173 |
typedef struct _ffi_pl_type_extra_custom_perl { |
| 174 |
union { |
| 175 |
ffi_pl_type_extra_record record; |
| 176 |
} ox; |
| 177 |
void *perl_to_native; |
| 178 |
void *native_to_perl; |
| 179 |
void *perl_to_native_post; |
| 180 |
int argument_count; |
| 181 |
} ffi_pl_type_extra_custom_perl; |
| 182 |
|
| 183 |
typedef struct _ffi_pl_type_extra_array { |
| 184 |
int element_count; |
| 185 |
} ffi_pl_type_extra_array; |
| 186 |
|
| 187 |
struct _ffi_pl_type; |
| 188 |
|
| 189 |
typedef struct _ffi_pl_type_extra_closure { |
| 190 |
ffi_cif ffi_cif; |
| 191 |
int flags; |
| 192 |
struct _ffi_pl_type *return_type; |
| 193 |
struct _ffi_pl_type *argument_types[0]; |
| 194 |
} ffi_pl_type_extra_closure; |
| 195 |
|
| 196 |
typedef union _ffi_pl_type_extra { |
| 197 |
ffi_pl_type_extra_custom_perl custom_perl; |
| 198 |
ffi_pl_type_extra_array array; |
| 199 |
ffi_pl_type_extra_closure closure; |
| 200 |
ffi_pl_type_extra_record record; |
| 201 |
ffi_pl_type_extra_object object; |
| 202 |
} ffi_pl_type_extra; |
| 203 |
|
| 204 |
typedef struct _ffi_pl_type { |
| 205 |
unsigned short type_code; |
| 206 |
unsigned short sub_type; |
| 207 |
ffi_pl_type_extra extra[0]; |
| 208 |
} ffi_pl_type; |
| 209 |
|
| 210 |
typedef struct _ffi_pl_function { |
| 211 |
void *address; |
| 212 |
void *platypus_sv; /* really a Perl SV* */ |
| 213 |
int platypus_api; |
| 214 |
ffi_cif ffi_cif; |
| 215 |
ffi_pl_type *return_type; |
| 216 |
ffi_pl_type *argument_types[0]; |
| 217 |
} ffi_pl_function; |
| 218 |
|
| 219 |
typedef struct _ffi_pl_closure { |
| 220 |
ffi_closure *ffi_closure; |
| 221 |
void *function_pointer; /* C function pointer */ |
| 222 |
void *coderef; /* Perl HV* pointing to FFI::Platypus::Closure object */ |
| 223 |
ffi_pl_type *type; |
| 224 |
} ffi_pl_closure; |
| 225 |
|
| 226 |
typedef const char *ffi_pl_string; |
| 227 |
|
| 228 |
typedef union _ffi_pl_result { |
| 229 |
void *pointer; |
| 230 |
const char *string; |
| 231 |
int8_t sint8; |
| 232 |
uint8_t uint8; |
| 233 |
#if defined FFI_PL_PROBE_BIGENDIAN |
| 234 |
int8_t sint8_array[4]; |
| 235 |
uint8_t uint8_array[4]; |
| 236 |
#elif defined FFI_PL_PROBE_BIGENDIAN64 |
| 237 |
int8_t sint8_array[8]; |
| 238 |
uint8_t uint8_array[8]; |
| 239 |
#endif |
| 240 |
int16_t sint16; |
| 241 |
uint16_t uint16; |
| 242 |
#if defined FFI_PL_PROBE_BIGENDIAN |
| 243 |
int16_t sint16_array[2]; |
| 244 |
uint16_t uint16_array[2]; |
| 245 |
#elif defined FFI_PL_PROBE_BIGENDIAN64 |
| 246 |
int16_t sint16_array[4]; |
| 247 |
uint16_t uint16_array[4]; |
| 248 |
#endif |
| 249 |
int32_t sint32; |
| 250 |
uint32_t uint32; |
| 251 |
#if defined FFI_PL_PROBE_BIGENDIAN64 |
| 252 |
uint32_t uint32_array[2]; |
| 253 |
int32_t sint32_array[2]; |
| 254 |
#endif |
| 255 |
int64_t sint64; |
| 256 |
uint64_t uint64; |
| 257 |
float xfloat; |
| 258 |
double xdouble; |
| 259 |
#ifdef FFI_PL_PROBE_LONGDOUBLE |
| 260 |
long double longdouble; |
| 261 |
#endif |
| 262 |
#ifdef FFI_TARGET_HAS_COMPLEX_TYPE |
| 263 |
#ifdef SIZEOF_FLOAT_COMPLEX |
| 264 |
float complex complex_float; |
| 265 |
#endif |
| 266 |
#ifdef SIZEOF_DOUBLE_COMPLEX |
| 267 |
double complex complex_double; |
| 268 |
#endif |
| 269 |
#endif |
| 270 |
} ffi_pl_result; |
| 271 |
|
| 272 |
typedef union _ffi_pl_argument { |
| 273 |
void *pointer; |
| 274 |
const char *string; |
| 275 |
int8_t sint8; |
| 276 |
uint8_t uint8; |
| 277 |
int16_t sint16; |
| 278 |
uint16_t uint16; |
| 279 |
int32_t sint32; |
| 280 |
uint32_t uint32; |
| 281 |
int64_t sint64; |
| 282 |
uint64_t uint64; |
| 283 |
float xfloat; |
| 284 |
double xdouble; |
| 285 |
} ffi_pl_argument; |
| 286 |
|
| 287 |
typedef struct _ffi_pl_arguments { |
| 288 |
int count; |
| 289 |
int reserved; |
| 290 |
ffi_pl_argument slot[0]; |
| 291 |
} ffi_pl_arguments; |
| 292 |
|
| 293 |
typedef struct _ffi_pl_record_member { |
| 294 |
int offset; |
| 295 |
int count; |
| 296 |
} ffi_pl_record_member; |
| 297 |
|
| 298 |
#define ffi_pl_arguments_count(arguments) (arguments->count) |
| 299 |
#define ffi_pl_arguments_set_pointer(arguments, i, value) (arguments->slot[i].pointer = value) |
| 300 |
#define ffi_pl_arguments_get_pointer(arguments, i) (arguments->slot[i].pointer) |
| 301 |
#define ffi_pl_arguments_set_string(arguments, i, value) (arguments->slot[i].string = value) |
| 302 |
#define ffi_pl_arguments_get_string(arguments, i) (arguments->slot[i].string) |
| 303 |
|
| 304 |
#define ffi_pl_arguments_set_sint8(arguments, i, value) (arguments->slot[i].sint8 = value) |
| 305 |
#define ffi_pl_arguments_get_sint8(arguments, i) (arguments->slot[i].sint8) |
| 306 |
#define ffi_pl_arguments_set_uint8(arguments, i, value) (arguments->slot[i].uint8 = value) |
| 307 |
#define ffi_pl_arguments_get_uint8(arguments, i) (arguments->slot[i].uint8) |
| 308 |
#define ffi_pl_arguments_set_sint16(arguments, i, value) (arguments->slot[i].sint16 = value) |
| 309 |
#define ffi_pl_arguments_get_sint16(arguments, i) (arguments->slot[i].sint16) |
| 310 |
#define ffi_pl_arguments_set_uint16(arguments, i, value) (arguments->slot[i].uint16 = value) |
| 311 |
#define ffi_pl_arguments_get_uint16(arguments, i) (arguments->slot[i].uint16) |
| 312 |
#define ffi_pl_arguments_set_sint32(arguments, i, value) (arguments->slot[i].sint32 = value) |
| 313 |
#define ffi_pl_arguments_get_sint32(arguments, i) (arguments->slot[i].sint32) |
| 314 |
#define ffi_pl_arguments_set_uint32(arguments, i, value) (arguments->slot[i].uint32 = value) |
| 315 |
#define ffi_pl_arguments_get_uint32(arguments, i) (arguments->slot[i].uint32) |
| 316 |
#define ffi_pl_arguments_set_sint64(arguments, i, value) (arguments->slot[i].sint64 = value) |
| 317 |
#define ffi_pl_arguments_get_sint64(arguments, i) (arguments->slot[i].sint64) |
| 318 |
#define ffi_pl_arguments_set_uint64(arguments, i, value) (arguments->slot[i].uint64 = value) |
| 319 |
#define ffi_pl_arguments_get_uint64(arguments, i) (arguments->slot[i].uint64) |
| 320 |
|
| 321 |
#define ffi_pl_arguments_set_float(arguments, i, value) (arguments->slot[i].xfloat = value) |
| 322 |
#define ffi_pl_arguments_get_float(arguments, i) (arguments->slot[i].xfloat) |
| 323 |
#define ffi_pl_arguments_set_double(arguments, i, value) (arguments->slot[i].xdouble = value) |
| 324 |
#define ffi_pl_arguments_get_double(arguments, i) (arguments->slot[i].xdouble) |
| 325 |
|
| 326 |
#define ffi_pl_arguments_pointers(arguments) ((void**)&arguments->slot[arguments->count]) |
| 327 |
|
| 328 |
typedef struct _ffi_pl_heap { |
| 329 |
void *_this; |
| 330 |
void *_next; |
| 331 |
} ffi_pl_heap; |
| 332 |
|
| 333 |
#define ffi_pl_heap_add(ptr, count, type) { \ |
| 334 |
ffi_pl_heap *n; \ |
| 335 |
Newx(ptr, count, type); \ |
| 336 |
Newx(n, 1, ffi_pl_heap); \ |
| 337 |
n->_this = ptr; \ |
| 338 |
n->_next = (void*) heap; \ |
| 339 |
heap = n; \ |
| 340 |
} |
| 341 |
|
| 342 |
#define ffi_pl_heap_add_ptr(ptr) { \ |
| 343 |
ffi_pl_heap *n; \ |
| 344 |
Newx(n, 1, ffi_pl_heap); \ |
| 345 |
n->_this = ptr; \ |
| 346 |
n->_next = (void*) heap; \ |
| 347 |
heap = n; \ |
| 348 |
} |
| 349 |
|
| 350 |
#define ffi_pl_heap_free() { \ |
| 351 |
while(heap != NULL) \ |
| 352 |
{ \ |
| 353 |
ffi_pl_heap *old = heap; \ |
| 354 |
heap = (ffi_pl_heap *) old->_next; \ |
| 355 |
Safefree(old->_this); \ |
| 356 |
Safefree(old); \ |
| 357 |
} \ |
| 358 |
} |
| 359 |
|
| 360 |
#define ffi_pl_croak \ |
| 361 |
ffi_pl_heap_free(); \ |
| 362 |
croak |
| 363 |
|
| 364 |
#if defined(_MSC_VER) |
| 365 |
#define Newx_or_alloca(ptr, count, type) ptr = _alloca(sizeof(type)*count) |
| 366 |
#elif defined(FFI_PL_PROBE_ALLOCA) |
| 367 |
#define Newx_or_alloca(ptr, count, type) ptr = alloca(sizeof(type)*count) |
| 368 |
#else |
| 369 |
#define Newx_or_alloca(ptr, count, type) ffi_pl_heap_add(ptr, count, type) |
| 370 |
#endif |
| 371 |
|
| 372 |
ffi_type *ffi_pl_type_to_libffi_type(ffi_pl_type *type); |
| 373 |
ffi_pl_type *ffi_pl_type_new(size_t size); |
| 374 |
|
| 375 |
#if SIZEOF_VOIDP == 4 && !defined(__hppa__) |
| 376 |
uint64_t cast0(void); |
| 377 |
#else |
| 378 |
void *cast0(void); |
| 379 |
#endif |
| 380 |
|
| 381 |
#if SIZEOF_VOIDP == 4 && !defined(__hppa__) |
| 382 |
uint64_t cast1(uint64_t value); |
| 383 |
#else |
| 384 |
void *cast1(void *value); |
| 385 |
#endif |
| 386 |
|
| 387 |
#ifdef __cplusplus |
| 388 |
} |
| 389 |
#endif |
| 390 |
|
| 391 |
#endif |