diff options
Diffstat (limited to 'ruby_1_9_3/ext/fiddle')
-rw-r--r-- | ruby_1_9_3/ext/fiddle/closure.c | 307 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/closure.h | 8 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/conversions.c | 139 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/conversions.h | 44 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/extconf.rb | 54 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/fiddle.c | 87 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/fiddle.h | 103 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/function.c | 226 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/function.h | 8 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/lib/fiddle.rb | 34 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/lib/fiddle/closure.rb | 48 | ||||
-rw-r--r-- | ruby_1_9_3/ext/fiddle/lib/fiddle/function.rb | 6 |
12 files changed, 0 insertions, 1064 deletions
diff --git a/ruby_1_9_3/ext/fiddle/closure.c b/ruby_1_9_3/ext/fiddle/closure.c deleted file mode 100644 index 21796660c4..0000000000 --- a/ruby_1_9_3/ext/fiddle/closure.c +++ /dev/null @@ -1,307 +0,0 @@ -#include <fiddle.h> - -VALUE cFiddleClosure; - -typedef struct { - void * code; - ffi_closure *pcl; - ffi_cif cif; - int argc; - ffi_type **argv; -} fiddle_closure; - -#if defined(MACOSX) || defined(__linux) || defined(__OpenBSD__) -#define DONT_USE_FFI_CLOSURE_ALLOC -#endif - -static void -dealloc(void * ptr) -{ - fiddle_closure * cls = (fiddle_closure *)ptr; -#ifndef DONT_USE_FFI_CLOSURE_ALLOC - ffi_closure_free(cls->pcl); -#else - munmap(cls->pcl, sizeof(cls->pcl)); -#endif - if (cls->argv) xfree(cls->argv); - xfree(cls); -} - -static size_t -closure_memsize(const void * ptr) -{ - fiddle_closure * cls = (fiddle_closure *)ptr; - size_t size = 0; - - if (ptr) { - size += sizeof(*cls); -#if !defined(FFI_NO_RAW_API) || !FFI_NO_RAW_API - size += ffi_raw_size(&cls->cif); -#endif - size += sizeof(*cls->argv); - size += sizeof(ffi_closure); - } - return size; -} - -const rb_data_type_t closure_data_type = { - "fiddle/closure", - {0, dealloc, closure_memsize,}, -}; - -void -callback(ffi_cif *cif, void *resp, void **args, void *ctx) -{ - VALUE self = (VALUE)ctx; - VALUE rbargs = rb_iv_get(self, "@args"); - VALUE ctype = rb_iv_get(self, "@ctype"); - int argc = RARRAY_LENINT(rbargs); - VALUE params = rb_ary_tmp_new(argc); - VALUE ret; - VALUE cPointer; - int i, type; - - cPointer = rb_const_get(mFiddle, rb_intern("Pointer")); - - for (i = 0; i < argc; i++) { - type = NUM2INT(RARRAY_PTR(rbargs)[i]); - switch (type) { - case TYPE_VOID: - argc = 0; - break; - case TYPE_INT: - rb_ary_push(params, INT2NUM(*(int *)args[i])); - break; - case -TYPE_INT: - rb_ary_push(params, UINT2NUM(*(unsigned int *)args[i])); - break; - case TYPE_VOIDP: - rb_ary_push(params, - rb_funcall(cPointer, rb_intern("[]"), 1, - PTR2NUM(*(void **)args[i]))); - break; - case TYPE_LONG: - rb_ary_push(params, LONG2NUM(*(long *)args[i])); - break; - case -TYPE_LONG: - rb_ary_push(params, ULONG2NUM(*(unsigned long *)args[i])); - break; - case TYPE_CHAR: - rb_ary_push(params, INT2NUM(*(signed char *)args[i])); - break; - case -TYPE_CHAR: - rb_ary_push(params, UINT2NUM(*(unsigned char *)args[i])); - break; - case TYPE_SHORT: - rb_ary_push(params, INT2NUM(*(signed short *)args[i])); - break; - case -TYPE_SHORT: - rb_ary_push(params, UINT2NUM(*(unsigned short *)args[i])); - break; - case TYPE_DOUBLE: - rb_ary_push(params, rb_float_new(*(double *)args[i])); - break; - case TYPE_FLOAT: - rb_ary_push(params, rb_float_new(*(float *)args[i])); - break; -#if HAVE_LONG_LONG - case TYPE_LONG_LONG: - rb_ary_push(params, LL2NUM(*(LONG_LONG *)args[i])); - break; - case -TYPE_LONG_LONG: - rb_ary_push(params, ULL2NUM(*(unsigned LONG_LONG *)args[i])); - break; -#endif - default: - rb_raise(rb_eRuntimeError, "closure args: %d", type); - } - } - - ret = rb_funcall2(self, rb_intern("call"), argc, RARRAY_PTR(params)); - RB_GC_GUARD(params); - - type = NUM2INT(ctype); - switch (type) { - case TYPE_VOID: - break; - case TYPE_LONG: - *(long *)resp = NUM2LONG(ret); - break; - case -TYPE_LONG: - *(unsigned long *)resp = NUM2ULONG(ret); - break; - case TYPE_CHAR: - case TYPE_SHORT: - case TYPE_INT: - *(ffi_sarg *)resp = NUM2INT(ret); - break; - case -TYPE_CHAR: - case -TYPE_SHORT: - case -TYPE_INT: - *(ffi_arg *)resp = NUM2UINT(ret); - break; - case TYPE_VOIDP: - *(void **)resp = NUM2PTR(ret); - break; - case TYPE_DOUBLE: - *(double *)resp = NUM2DBL(ret); - break; - case TYPE_FLOAT: - *(float *)resp = (float)NUM2DBL(ret); - break; -#if HAVE_LONG_LONG - case TYPE_LONG_LONG: - *(LONG_LONG *)resp = NUM2LL(ret); - break; - case -TYPE_LONG_LONG: - *(unsigned LONG_LONG *)resp = NUM2ULL(ret); - break; -#endif - default: - rb_raise(rb_eRuntimeError, "closure retval: %d", type); - } -} - -static VALUE -allocate(VALUE klass) -{ - fiddle_closure * closure; - - VALUE i = TypedData_Make_Struct(klass, fiddle_closure, - &closure_data_type, closure); - -#ifndef DONT_USE_FFI_CLOSURE_ALLOC - closure->pcl = ffi_closure_alloc(sizeof(ffi_closure), &closure->code); -#else - closure->pcl = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); -#endif - - return i; -} - -static VALUE -initialize(int rbargc, VALUE argv[], VALUE self) -{ - VALUE ret; - VALUE args; - VALUE abi; - fiddle_closure * cl; - ffi_cif * cif; - ffi_closure *pcl; - ffi_status result; - int i, argc; - - if (2 == rb_scan_args(rbargc, argv, "21", &ret, &args, &abi)) - abi = INT2NUM(FFI_DEFAULT_ABI); - - Check_Type(args, T_ARRAY); - - argc = RARRAY_LENINT(args); - - TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, cl); - - cl->argv = (ffi_type **)xcalloc(argc + 1, sizeof(ffi_type *)); - - for (i = 0; i < argc; i++) { - int type = NUM2INT(RARRAY_PTR(args)[i]); - cl->argv[i] = INT2FFI_TYPE(type); - } - cl->argv[argc] = NULL; - - rb_iv_set(self, "@ctype", ret); - rb_iv_set(self, "@args", args); - - cif = &cl->cif; - pcl = cl->pcl; - - result = ffi_prep_cif(cif, NUM2INT(abi), argc, - INT2FFI_TYPE(NUM2INT(ret)), - cl->argv); - - if (FFI_OK != result) - rb_raise(rb_eRuntimeError, "error prepping CIF %d", result); - -#ifndef DONT_USE_FFI_CLOSURE_ALLOC - result = ffi_prep_closure_loc(pcl, cif, callback, - (void *)self, cl->code); -#else - result = ffi_prep_closure(pcl, cif, callback, (void *)self); - cl->code = (void *)pcl; - mprotect(pcl, sizeof(pcl), PROT_READ | PROT_EXEC); -#endif - - if (FFI_OK != result) - rb_raise(rb_eRuntimeError, "error prepping closure %d", result); - - return self; -} - -static VALUE -to_i(VALUE self) -{ - fiddle_closure * cl; - void *code; - - TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, cl); - - code = cl->code; - - return PTR2NUM(code); -} - -void -Init_fiddle_closure() -{ -#if 0 - mFiddle = rb_define_module("Fiddle"); /* let rdoc know about mFiddle */ -#endif - - /* - * Document-class: Fiddle::Closure - * - * == Description - * - * An FFI closure wrapper, for handling callbacks. - * - * == Example - * - * closure = Class.new(Fiddle::Closure) { - * def call - * 10 - * end - * }.new(Fiddle::TYPE_INT, []) - * => #<#<Class:0x0000000150d308>:0x0000000150d240> - * func = Fiddle::Function.new(closure, [], Fiddle::TYPE_INT) - * => #<Fiddle::Function:0x00000001516e58> - * func.call - * => 10 - */ - cFiddleClosure = rb_define_class_under(mFiddle, "Closure", rb_cObject); - - rb_define_alloc_func(cFiddleClosure, allocate); - - /* - * Document-method: new - * - * call-seq: new(ret, args, abi = Fiddle::DEFAULT) - * - * Construct a new Closure object. - * - * * +ret+ is the C type to be returned - * * +args+ are passed the callback - * * +abi+ is the abi of the closure - * - * If there is an error in preparing the ffi_cif or ffi_prep_closure, - * then a RuntimeError will be raised. - */ - rb_define_method(cFiddleClosure, "initialize", initialize, -1); - - /* - * Document-method: to_i - * - * Returns the memory address for this closure - */ - rb_define_method(cFiddleClosure, "to_i", to_i, 0); -} -/* vim: set noet sw=4 sts=4 */ diff --git a/ruby_1_9_3/ext/fiddle/closure.h b/ruby_1_9_3/ext/fiddle/closure.h deleted file mode 100644 index 1e870e2285..0000000000 --- a/ruby_1_9_3/ext/fiddle/closure.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef FIDDLE_CLOSURE_H -#define FIDDLE_CLOSURE_H - -#include <fiddle.h> - -void Init_fiddle_closure(); - -#endif diff --git a/ruby_1_9_3/ext/fiddle/conversions.c b/ruby_1_9_3/ext/fiddle/conversions.c deleted file mode 100644 index f2c1511778..0000000000 --- a/ruby_1_9_3/ext/fiddle/conversions.c +++ /dev/null @@ -1,139 +0,0 @@ -#include <fiddle.h> - -ffi_type * -int_to_ffi_type(int type) -{ - int signed_p = 1; - - if (type < 0) { - type = -1 * type; - signed_p = 0; - } - -#define rb_ffi_type_of(t) (signed_p ? &ffi_type_s##t : &ffi_type_u##t) - - switch (type) { - case TYPE_VOID: - return &ffi_type_void; - case TYPE_VOIDP: - return &ffi_type_pointer; - case TYPE_CHAR: - return rb_ffi_type_of(char); - case TYPE_SHORT: - return rb_ffi_type_of(short); - case TYPE_INT: - return rb_ffi_type_of(int); - case TYPE_LONG: - return rb_ffi_type_of(long); -#if HAVE_LONG_LONG - case TYPE_LONG_LONG: - return rb_ffi_type_of(long_long); -#endif - case TYPE_FLOAT: - return &ffi_type_float; - case TYPE_DOUBLE: - return &ffi_type_double; - default: - rb_raise(rb_eRuntimeError, "unknown type %d", type); - } - return &ffi_type_pointer; -} - -void -value_to_generic(int type, VALUE src, fiddle_generic * dst) -{ - switch (type) { - case TYPE_VOID: - break; - case TYPE_VOIDP: - dst->pointer = NUM2PTR(rb_Integer(src)); - break; - case TYPE_CHAR: - dst->schar = (signed char)NUM2INT(src); - break; - case -TYPE_CHAR: - dst->uchar = (unsigned char)NUM2UINT(src); - break; - case TYPE_SHORT: - dst->sshort = (unsigned short)NUM2INT(src); - break; - case -TYPE_SHORT: - dst->sshort = (signed short)NUM2UINT(src); - break; - case TYPE_INT: - dst->sint = NUM2INT(src); - break; - case -TYPE_INT: - dst->uint = NUM2UINT(src); - break; - case TYPE_LONG: - dst->slong = NUM2LONG(src); - break; - case -TYPE_LONG: - dst->ulong = NUM2ULONG(src); - break; -#if HAVE_LONG_LONG - case TYPE_LONG_LONG: - dst->slong_long = NUM2LL(src); - break; - case -TYPE_LONG_LONG: - dst->ulong_long = NUM2ULL(src); - break; -#endif - case TYPE_FLOAT: - dst->ffloat = (float)NUM2DBL(src); - break; - case TYPE_DOUBLE: - dst->ddouble = NUM2DBL(src); - break; - default: - rb_raise(rb_eRuntimeError, "unknown type %d", type); - } -} - -VALUE -generic_to_value(VALUE rettype, fiddle_generic retval) -{ - int type = NUM2INT(rettype); - VALUE cPointer; - - cPointer = rb_const_get(mFiddle, rb_intern("Pointer")); - - switch (type) { - case TYPE_VOID: - return Qnil; - case TYPE_VOIDP: - return rb_funcall(cPointer, rb_intern("[]"), 1, - PTR2NUM((void *)retval.pointer)); - case TYPE_CHAR: - return INT2NUM((signed char)retval.fffi_sarg); - case -TYPE_CHAR: - return INT2NUM((unsigned char)retval.fffi_arg); - case TYPE_SHORT: - return INT2NUM((signed short)retval.fffi_sarg); - case -TYPE_SHORT: - return INT2NUM((unsigned short)retval.fffi_arg); - case TYPE_INT: - return INT2NUM((signed int)retval.fffi_sarg); - case -TYPE_INT: - return UINT2NUM((unsigned int)retval.fffi_arg); - case TYPE_LONG: - return LONG2NUM(retval.slong); - case -TYPE_LONG: - return ULONG2NUM(retval.ulong); -#if HAVE_LONG_LONG - case TYPE_LONG_LONG: - return LL2NUM(retval.slong_long); - case -TYPE_LONG_LONG: - return ULL2NUM(retval.ulong_long); -#endif - case TYPE_FLOAT: - return rb_float_new(retval.ffloat); - case TYPE_DOUBLE: - return rb_float_new(retval.ddouble); - default: - rb_raise(rb_eRuntimeError, "unknown type %d", type); - } -} - -/* vim: set noet sw=4 sts=4 */ diff --git a/ruby_1_9_3/ext/fiddle/conversions.h b/ruby_1_9_3/ext/fiddle/conversions.h deleted file mode 100644 index d0a08d6bc0..0000000000 --- a/ruby_1_9_3/ext/fiddle/conversions.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef FIDDLE_CONVERSIONS_H -#define FIDDLE_CONVERSIONS_H - -#include <fiddle.h> - -typedef union -{ - ffi_arg fffi_arg; /* rvalue smaller than unsigned long */ - ffi_sarg fffi_sarg; /* rvalue smaller than signed long */ - unsigned char uchar; /* ffi_type_uchar */ - signed char schar; /* ffi_type_schar */ - unsigned short ushort; /* ffi_type_sshort */ - signed short sshort; /* ffi_type_ushort */ - unsigned int uint; /* ffi_type_uint */ - signed int sint; /* ffi_type_sint */ - unsigned long ulong; /* ffi_type_ulong */ - signed long slong; /* ffi_type_slong */ - float ffloat; /* ffi_type_float */ - double ddouble; /* ffi_type_double */ -#if HAVE_LONG_LONG - unsigned LONG_LONG ulong_long; /* ffi_type_ulong_long */ - signed LONG_LONG slong_long; /* ffi_type_ulong_long */ -#endif - void * pointer; /* ffi_type_pointer */ -} fiddle_generic; - -ffi_type * int_to_ffi_type(int type); -void value_to_generic(int type, VALUE src, fiddle_generic * dst); -VALUE generic_to_value(VALUE rettype, fiddle_generic retval); - -#define VALUE2GENERIC(_type, _src, _dst) value_to_generic((_type), (_src), (_dst)) -#define INT2FFI_TYPE(_type) int_to_ffi_type(_type) -#define GENERIC2VALUE(_type, _retval) generic_to_value((_type), (_retval)) - -#if SIZEOF_VOIDP == SIZEOF_LONG -# define PTR2NUM(x) (ULONG2NUM((unsigned long)(x))) -# define NUM2PTR(x) ((void*)(NUM2ULONG(x))) -#else -/* # error --->> Ruby/DL2 requires sizeof(void*) == sizeof(long) to be compiled. <<--- */ -# define PTR2NUM(x) (ULL2NUM((unsigned long long)(x))) -# define NUM2PTR(x) ((void*)(NUM2ULL(x))) -#endif - -#endif diff --git a/ruby_1_9_3/ext/fiddle/extconf.rb b/ruby_1_9_3/ext/fiddle/extconf.rb deleted file mode 100644 index 2cb9ae0ace..0000000000 --- a/ruby_1_9_3/ext/fiddle/extconf.rb +++ /dev/null @@ -1,54 +0,0 @@ -require 'mkmf' - -# :stopdoc: - -dir_config 'libffi' - -pkg_config("libffi") -unless have_header('ffi.h') - if have_header('ffi/ffi.h') - $defs.push(format('-DUSE_HEADER_HACKS')) - else - raise "ffi.h is missing. Please install libffi." - end -end - -unless have_library('ffi') || have_library('libffi') - raise "libffi is missing. Please install libffi." -end - -have_header 'sys/mman.h' - -if have_header "dlfcn.h" - have_library "dl" - - %w{ dlopen dlclose dlsym }.each do |func| - abort "missing function #{func}" unless have_func(func) - end - - have_func "dlerror" -elsif have_header "windows.h" - %w{ LoadLibrary FreeLibrary GetProcAddress }.each do |func| - abort "missing function #{func}" unless have_func(func) - end -end - -have_const('FFI_STDCALL', 'ffi.h') || have_const('FFI_STDCALL', 'ffi/ffi.h') - -config = File.read(RbConfig.expand(File.join($arch_hdrdir, "ruby/config.h"))) -types = {"SIZE_T"=>"SSIZE_T", "PTRDIFF_T"=>nil, "INTPTR_T"=>nil} -types.each do |type, signed| - if /^\#define\s+SIZEOF_#{type}\s+(SIZEOF_(.+)|\d+)/ =~ config - if size = $2 and size != 'VOIDP' - size = types.fetch(size) {size} - $defs << format("-DTYPE_%s=TYPE_%s", signed||type, size) - end - if signed - check_signedness(type.downcase, "stddef.h") - end - end -end - -create_makefile 'fiddle' - -# :startdoc: diff --git a/ruby_1_9_3/ext/fiddle/fiddle.c b/ruby_1_9_3/ext/fiddle/fiddle.c deleted file mode 100644 index 83c0bb5e1e..0000000000 --- a/ruby_1_9_3/ext/fiddle/fiddle.c +++ /dev/null @@ -1,87 +0,0 @@ -#include <fiddle.h> - -VALUE mFiddle; - -void -Init_fiddle(void) -{ - /* - * Document-module: Fiddle - * - * == Description - * - * A libffi wrapper. - * - */ - mFiddle = rb_define_module("Fiddle"); - - /* Document-const: TYPE_VOID - * - * C type - void - */ - rb_define_const(mFiddle, "TYPE_VOID", INT2NUM(TYPE_VOID)); - - /* Document-const: TYPE_VOIDP - * - * C type - void* - */ - rb_define_const(mFiddle, "TYPE_VOIDP", INT2NUM(TYPE_VOIDP)); - - /* Document-const: TYPE_CHAR - * - * C type - char - */ - rb_define_const(mFiddle, "TYPE_CHAR", INT2NUM(TYPE_CHAR)); - - /* Document-const: TYPE_SHORT - * - * C type - short - */ - rb_define_const(mFiddle, "TYPE_SHORT", INT2NUM(TYPE_SHORT)); - - /* Document-const: TYPE_INT - * - * C type - int - */ - rb_define_const(mFiddle, "TYPE_INT", INT2NUM(TYPE_INT)); - - /* Document-const: TYPE_LONG - * - * C type - long - */ - rb_define_const(mFiddle, "TYPE_LONG", INT2NUM(TYPE_LONG)); - -#if HAVE_LONG_LONG - /* Document-const: TYPE_LONG_LONG - * - * C type - long long - */ - rb_define_const(mFiddle, "TYPE_LONG_LONG", INT2NUM(TYPE_LONG_LONG)); -#endif - - /* Document-const: TYPE_FLOAT - * - * C type - float - */ - rb_define_const(mFiddle, "TYPE_FLOAT", INT2NUM(TYPE_FLOAT)); - - /* Document-const: TYPE_DOUBLE - * - * C type - double - */ - rb_define_const(mFiddle, "TYPE_DOUBLE", INT2NUM(TYPE_DOUBLE)); - - /* Document-const: WINDOWS - * - * Returns a boolean regarding whether the host is WIN32 - */ -#if defined(_WIN32) - rb_define_const(mFiddle, "WINDOWS", Qtrue); -#else - rb_define_const(mFiddle, "WINDOWS", Qfalse); -#endif - - Init_fiddle_function(); - Init_fiddle_closure(); -} -/* vim: set noet sws=4 sw=4: */ diff --git a/ruby_1_9_3/ext/fiddle/fiddle.h b/ruby_1_9_3/ext/fiddle/fiddle.h deleted file mode 100644 index 3a829fe433..0000000000 --- a/ruby_1_9_3/ext/fiddle/fiddle.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef FIDDLE_H -#define FIDDLE_H - -#include <ruby.h> -#include <errno.h> - -#if defined(_WIN32) -#include <windows.h> -#endif - -#ifdef HAVE_SYS_MMAN_H -#include <sys/mman.h> -#endif - -#ifdef USE_HEADER_HACKS -#include <ffi/ffi.h> -#else -#include <ffi.h> -#endif - -#undef ffi_type_uchar -#undef ffi_type_schar -#undef ffi_type_ushort -#undef ffi_type_sshort -#undef ffi_type_uint -#undef ffi_type_sint -#undef ffi_type_ulong -#undef ffi_type_slong - -#if CHAR_BIT == 8 -# define ffi_type_uchar ffi_type_uint8 -# define ffi_type_schar ffi_type_sint8 -#else -# error "CHAR_BIT not supported" -#endif - -# if SIZEOF_SHORT == 2 -# define ffi_type_ushort ffi_type_uint16 -# define ffi_type_sshort ffi_type_sint16 -# elif SIZEOF_SHORT == 4 -# define ffi_type_ushort ffi_type_uint32 -# define ffi_type_sshort ffi_type_sint32 -# else -# error "short size not supported" -# endif - -# if SIZEOF_INT == 2 -# define ffi_type_uint ffi_type_uint16 -# define ffi_type_sint ffi_type_sint16 -# elif SIZEOF_INT == 4 -# define ffi_type_uint ffi_type_uint32 -# define ffi_type_sint ffi_type_sint32 -# elif SIZEOF_INT == 8 -# define ffi_type_uint ffi_type_uint64 -# define ffi_type_sint ffi_type_sint64 -# else -# error "int size not supported" -# endif - -# if SIZEOF_LONG == 4 -# define ffi_type_ulong ffi_type_uint32 -# define ffi_type_slong ffi_type_sint32 -# elif SIZEOF_LONG == 8 -# define ffi_type_ulong ffi_type_uint64 -# define ffi_type_slong ffi_type_sint64 -# else -# error "long size not supported" -# endif - -#if HAVE_LONG_LONG -# if SIZEOF_LONG_LONG == 8 -# define ffi_type_slong_long ffi_type_sint64 -# define ffi_type_ulong_long ffi_type_uint64 -# else -# error "long long size not supported" -# endif -#endif - -#include <closure.h> -#include <conversions.h> -#include <function.h> - -/* FIXME - * These constants need to match up with DL. We need to refactor this to use - * the DL header files or vice versa. - */ - -#define TYPE_VOID 0 -#define TYPE_VOIDP 1 -#define TYPE_CHAR 2 -#define TYPE_SHORT 3 -#define TYPE_INT 4 -#define TYPE_LONG 5 -#if HAVE_LONG_LONG -#define TYPE_LONG_LONG 6 -#endif -#define TYPE_FLOAT 7 -#define TYPE_DOUBLE 8 - -extern VALUE mFiddle; - -#endif -/* vim: set noet sws=4 sw=4: */ diff --git a/ruby_1_9_3/ext/fiddle/function.c b/ruby_1_9_3/ext/fiddle/function.c deleted file mode 100644 index 52f7695eb7..0000000000 --- a/ruby_1_9_3/ext/fiddle/function.c +++ /dev/null @@ -1,226 +0,0 @@ -#include <fiddle.h> - -VALUE cFiddleFunction; - -static void -deallocate(void *p) -{ - ffi_cif *ptr = p; - if (ptr->arg_types) xfree(ptr->arg_types); - xfree(ptr); -} - -static size_t -function_memsize(const void *p) -{ - /* const */ffi_cif *ptr = (ffi_cif *)p; - size_t size = 0; - - if (ptr) { - size += sizeof(*ptr); -#if !defined(FFI_NO_RAW_API) || !FFI_NO_RAW_API - size += ffi_raw_size(ptr); -#endif - } - return size; -} - -const rb_data_type_t function_data_type = { - "fiddle/function", - {0, deallocate, function_memsize,}, -}; - -static VALUE -allocate(VALUE klass) -{ - ffi_cif * cif; - - return TypedData_Make_Struct(klass, ffi_cif, &function_data_type, cif); -} - -static VALUE -initialize(int argc, VALUE argv[], VALUE self) -{ - ffi_cif * cif; - ffi_type **arg_types; - ffi_status result; - VALUE ptr, args, ret_type, abi; - int i; - - rb_scan_args(argc, argv, "31", &ptr, &args, &ret_type, &abi); - if(NIL_P(abi)) abi = INT2NUM(FFI_DEFAULT_ABI); - - Check_Type(args, T_ARRAY); - - rb_iv_set(self, "@ptr", ptr); - rb_iv_set(self, "@args", args); - rb_iv_set(self, "@return_type", ret_type); - rb_iv_set(self, "@abi", abi); - - TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif); - - arg_types = xcalloc(RARRAY_LEN(args) + 1, sizeof(ffi_type *)); - - for (i = 0; i < RARRAY_LEN(args); i++) { - int type = NUM2INT(RARRAY_PTR(args)[i]); - arg_types[i] = INT2FFI_TYPE(type); - } - arg_types[RARRAY_LEN(args)] = NULL; - - result = ffi_prep_cif ( - cif, - NUM2INT(abi), - RARRAY_LENINT(args), - INT2FFI_TYPE(NUM2INT(ret_type)), - arg_types); - - if (result) - rb_raise(rb_eRuntimeError, "error creating CIF %d", result); - - return self; -} - -static VALUE -function_call(int argc, VALUE argv[], VALUE self) -{ - ffi_cif * cif; - fiddle_generic retval; - fiddle_generic *generic_args; - void **values; - VALUE cfunc, types, cPointer; - int i; - - cfunc = rb_iv_get(self, "@ptr"); - types = rb_iv_get(self, "@args"); - cPointer = rb_const_get(mFiddle, rb_intern("Pointer")); - - if(argc != RARRAY_LENINT(types)) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", - argc, RARRAY_LENINT(types)); - } - - TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif); - - if (rb_safe_level() >= 1) { - for (i = 0; i < argc; i++) { - VALUE src = argv[i]; - if (OBJ_TAINTED(src)) { - rb_raise(rb_eSecurityError, "tainted parameter not allowed"); - } - } - } - - values = xcalloc((size_t)argc + 1, (size_t)sizeof(void *)); - generic_args = xcalloc((size_t)argc, (size_t)sizeof(fiddle_generic)); - - for (i = 0; i < argc; i++) { - VALUE type = RARRAY_PTR(types)[i]; - VALUE src = argv[i]; - - if(NUM2INT(type) == TYPE_VOIDP) { - if(NIL_P(src)) { - src = INT2NUM(0); - } else if(cPointer != CLASS_OF(src)) { - src = rb_funcall(cPointer, rb_intern("[]"), 1, src); - } - src = rb_Integer(src); - } - - VALUE2GENERIC(NUM2INT(type), src, &generic_args[i]); - values[i] = (void *)&generic_args[i]; - } - values[argc] = NULL; - - ffi_call(cif, NUM2PTR(rb_Integer(cfunc)), &retval, values); - - rb_funcall(mFiddle, rb_intern("last_error="), 1, INT2NUM(errno)); -#if defined(_WIN32) - rb_funcall(mFiddle, rb_intern("win32_last_error="), 1, INT2NUM(errno)); -#endif - - xfree(values); - xfree(generic_args); - - return GENERIC2VALUE(rb_iv_get(self, "@return_type"), retval); -} - -void -Init_fiddle_function(void) -{ - /* - * Document-class: Fiddle::Function - * - * == Description - * - * A representation of a C function - * - * == Examples - * - * === 'strcpy' - * - * @libc = DL.dlopen "/lib/libc.so.6" - * => #<DL::Handle:0x00000001d7a8d8> - * f = Fiddle::Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP) - * => #<Fiddle::Function:0x00000001d8ee00> - * buff = "000" - * => "000" - * str = f.call(buff, "123") - * => #<DL::CPtr:0x00000001d0c380 ptr=0x000000018a21b8 size=0 free=0x00000000000000> - * str.to_s - * => "123" - * - * === ABI check - * - * @libc = DL.dlopen "/lib/libc.so.6" - * => #<DL::Handle:0x00000001d7a8d8> - * f = Fiddle::Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP) - * => #<Fiddle::Function:0x00000001d8ee00> - * f.abi == Fiddle::Function::DEFAULT - * => true - */ - cFiddleFunction = rb_define_class_under(mFiddle, "Function", rb_cObject); - - /* - * Document-const: DEFAULT - * - * Default ABI - * - */ - rb_define_const(cFiddleFunction, "DEFAULT", INT2NUM(FFI_DEFAULT_ABI)); - -#ifdef HAVE_CONST_FFI_STDCALL - /* - * Document-const: STDCALL - * - * FFI implementation of WIN32 stdcall convention - * - */ - rb_define_const(cFiddleFunction, "STDCALL", INT2NUM(FFI_STDCALL)); -#endif - - rb_define_alloc_func(cFiddleFunction, allocate); - - /* - * Document-method: call - * - * Calls the constructed Function, with +args+ - * - * For an example see Fiddle::Function - * - */ - rb_define_method(cFiddleFunction, "call", function_call, -1); - - /* - * Document-method: new - * call-seq: new(ptr, args, ret_type, abi = DEFAULT) - * - * Constructs a Function object. - * * +ptr+ is a referenced function, of a DL::Handle - * * +args+ is an Array of arguments, passed to the +ptr+ function - * * +ret_type+ is the return type of the function - * * +abi+ is the ABI of the function - * - */ - rb_define_method(cFiddleFunction, "initialize", initialize, -1); -} -/* vim: set noet sws=4 sw=4: */ diff --git a/ruby_1_9_3/ext/fiddle/function.h b/ruby_1_9_3/ext/fiddle/function.h deleted file mode 100644 index e5465ab64f..0000000000 --- a/ruby_1_9_3/ext/fiddle/function.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef FIDDLE_FUNCTION_H -#define FIDDLE_FUNCTION_H - -#include <fiddle.h> - -void Init_fiddle_function(); - -#endif diff --git a/ruby_1_9_3/ext/fiddle/lib/fiddle.rb b/ruby_1_9_3/ext/fiddle/lib/fiddle.rb deleted file mode 100644 index 7d55a1f7ad..0000000000 --- a/ruby_1_9_3/ext/fiddle/lib/fiddle.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'fiddle.so' -require 'fiddle/function' -require 'fiddle/closure' -require 'dl' unless Object.const_defined?(:DL) - -module Fiddle - - # A reference to DL::CPtr - Pointer = DL::CPtr - - if WINDOWS - # Returns the last win32 +Error+ of the current executing +Thread+ or nil - # if none - def self.win32_last_error - Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] - end - - # Sets the last win32 +Error+ of the current executing +Thread+ to +error+ - def self.win32_last_error= error - Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error - end - end - - # Returns the last +Error+ of the current executing +Thread+ or nil if none - def self.last_error - Thread.current[:__FIDDLE_LAST_ERROR__] - end - - # Sets the last +Error+ of the current executing +Thread+ to +error+ - def self.last_error= error - Thread.current[:__DL2_LAST_ERROR__] = error - Thread.current[:__FIDDLE_LAST_ERROR__] = error - end -end diff --git a/ruby_1_9_3/ext/fiddle/lib/fiddle/closure.rb b/ruby_1_9_3/ext/fiddle/lib/fiddle/closure.rb deleted file mode 100644 index beb90ecbe5..0000000000 --- a/ruby_1_9_3/ext/fiddle/lib/fiddle/closure.rb +++ /dev/null @@ -1,48 +0,0 @@ -module Fiddle - class Closure - - # the C type of the return of the FFI closure - attr_reader :ctype - - # arguments of the FFI closure - attr_reader :args - - # Extends Fiddle::Closure to allow for building the closure in a block - class BlockCaller < Fiddle::Closure - - # == Description - # - # Construct a new BlockCaller object. - # - # * +ctype+ is the C type to be returned - # * +args+ are passed the callback - # * +abi+ is the abi of the closure - # - # If there is an error in preparing the +ffi_cif+ or +ffi_prep_closure+, - # then a RuntimeError will be raised. - # - # == Example - # - # include Fiddle - # - # cb = Closure::BlockCaller.new(TYPE_INT, [TYPE_INT]) do |one| - # one - # end - # - # func = Function.new(cb, [TYPE_INT], TYPE_INT) - # - def initialize ctype, args, abi = Fiddle::Function::DEFAULT, &block - super(ctype, args, abi) - @block = block - end - - # Calls the constructed BlockCaller, with +args+ - # - # For an example see Fiddle::Closure::BlockCaller.new - # - def call *args - @block.call(*args) - end - end - end -end diff --git a/ruby_1_9_3/ext/fiddle/lib/fiddle/function.rb b/ruby_1_9_3/ext/fiddle/lib/fiddle/function.rb deleted file mode 100644 index 1657682498..0000000000 --- a/ruby_1_9_3/ext/fiddle/lib/fiddle/function.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Fiddle - class Function - # The ABI of the Function. - attr_reader :abi - end -end |