13 #if defined(USE_FFI_CLOSURE_ALLOC)
14 #elif defined(__OpenBSD__) || defined(__APPLE__) || defined(__linux__)
15 # define USE_FFI_CLOSURE_ALLOC 0
16 #elif defined(RUBY_LIBFFI_MODVERSION) && RUBY_LIBFFI_MODVERSION < 3000005 && \
17 (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_AMD64))
18 # define USE_FFI_CLOSURE_ALLOC 0
20 # define USE_FFI_CLOSURE_ALLOC 1
27 #if USE_FFI_CLOSURE_ALLOC
28 ffi_closure_free(cls->
pcl);
30 munmap(cls->
pcl,
sizeof(*cls->
pcl));
44 #if !defined(FFI_NO_RAW_API) || !FFI_NO_RAW_API
45 size += ffi_raw_size(&cls->
cif);
47 size +=
sizeof(*cls->
argv);
48 size +=
sizeof(ffi_closure);
72 for (i = 0; i <
argc; i++) {
115 rb_ary_push(params, LL2NUM(*(LONG_LONG *)args[i]));
117 case -TYPE_LONG_LONG:
118 rb_ary_push(params, ULL2NUM(*(
unsigned LONG_LONG *)args[i]));
142 *(ffi_sarg *)resp =
NUM2INT(ret);
153 *(
double *)resp =
NUM2DBL(ret);
156 *(
float *)resp = (
float)
NUM2DBL(ret);
160 *(LONG_LONG *)resp = NUM2LL(ret);
162 case -TYPE_LONG_LONG:
163 *(
unsigned LONG_LONG *)resp = NUM2ULL(ret);
177 &closure_data_type, closure);
179 #if USE_FFI_CLOSURE_ALLOC
180 closure->
pcl = ffi_closure_alloc(
sizeof(ffi_closure), &closure->
code);
182 closure->
pcl = mmap(
NULL,
sizeof(ffi_closure), PROT_READ | PROT_WRITE,
183 MAP_ANON | MAP_PRIVATE, -1, 0);
201 if (2 ==
rb_scan_args(rbargc, argv,
"21", &ret, &args, &abi))
202 abi =
INT2NUM(FFI_DEFAULT_ABI);
210 cl->
argv = (ffi_type **)
xcalloc(argc + 1,
sizeof(ffi_type *));
212 for (i = 0; i <
argc; i++) {
224 result = ffi_prep_cif(cif,
NUM2INT(abi), argc,
228 if (FFI_OK != result)
231 #if USE_FFI_CLOSURE_ALLOC
232 result = ffi_prep_closure_loc(pcl, cif,
callback,
233 (
void *)
self, cl->
code);
235 result = ffi_prep_closure(pcl, cif,
callback, (
void *)
self);
236 cl->
code = (
void *)pcl;
237 i = mprotect(pcl,
sizeof(*pcl), PROT_READ | PROT_EXEC);
243 if (FFI_OK != result)
rb_funcall2(argv[0], id_yield, argc-1, argv+1)
VALUE rb_iv_set(VALUE, const char *, VALUE)
rb_funcall(memo->yielder, id_lshift, 1, rb_assoc_new(memo->prev_value, memo->prev_elts))
VALUE rb_const_get(VALUE, ID)
VALUE rb_iv_get(VALUE, const char *)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rb_ary_tmp_new(long capa)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_raise(VALUE exc, const char *fmt,...)
void callback(ffi_cif *cif, void *resp, void **args, void *ctx)
#define TypedData_Get_Struct(obj, type, data_type, sval)
static VALUE to_i(VALUE self)
const rb_data_type_t closure_data_type
#define INT2FFI_TYPE(_type)
static void dealloc(void *ptr)
static VALUE initialize(int rbargc, VALUE argv[], VALUE self)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_sys_fail(const char *mesg)
static VALUE allocate(VALUE klass)
#define TypedData_Make_Struct(klass, type, data_type, sval)
RUBY_EXTERN VALUE rb_cObject
#define RARRAY_LENINT(ary)
void Init_fiddle_closure()
VALUE rb_define_module(const char *name)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
static size_t closure_memsize(const void *ptr)