ffi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ffi might be problematic. Click here for more details.
- data/LICENSE +51 -0
- data/README +0 -0
- data/Rakefile +90 -0
- data/ext/AbstractMemory.c +254 -0
- data/ext/AbstractMemory.h +36 -0
- data/ext/Buffer.c +94 -0
- data/ext/Callback.c +240 -0
- data/ext/Callback.h +47 -0
- data/ext/Invoker.c +269 -0
- data/ext/MemoryPointer.c +164 -0
- data/ext/MemoryPointer.h +25 -0
- data/ext/Platform.c +41 -0
- data/ext/Platform.h +23 -0
- data/ext/Types.c +65 -0
- data/ext/Types.h +51 -0
- data/ext/extconf.rb +13 -0
- data/ext/ffi.c +54 -0
- data/ext/ffi.mk +24 -0
- data/ext/libffi.darwin.mk +44 -0
- data/ext/libffi.mk +10 -0
- data/ext/libffi/ChangeLog +3243 -0
- data/ext/libffi/ChangeLog.libffi +347 -0
- data/ext/libffi/ChangeLog.libgcj +40 -0
- data/ext/libffi/ChangeLog.v1 +764 -0
- data/ext/libffi/LICENSE +21 -0
- data/ext/libffi/Makefile.am +177 -0
- data/ext/libffi/Makefile.in +1640 -0
- data/ext/libffi/README +328 -0
- data/ext/libffi/TODO +1 -0
- data/ext/libffi/acinclude.m4 +92 -0
- data/ext/libffi/aclocal.m4 +7516 -0
- data/ext/libffi/compile +142 -0
- data/ext/libffi/config.guess +1516 -0
- data/ext/libffi/config.sub +1626 -0
- data/ext/libffi/configure +24414 -0
- data/ext/libffi/configure.ac +365 -0
- data/ext/libffi/configure.host +11 -0
- data/ext/libffi/depcomp +584 -0
- data/ext/libffi/doc/libffi.info +533 -0
- data/ext/libffi/doc/libffi.texi +541 -0
- data/ext/libffi/doc/stamp-vti +4 -0
- data/ext/libffi/doc/version.texi +4 -0
- data/ext/libffi/fficonfig.h.in +160 -0
- data/ext/libffi/include/Makefile.am +9 -0
- data/ext/libffi/include/Makefile.in +422 -0
- data/ext/libffi/include/ffi.h.in +393 -0
- data/ext/libffi/include/ffi_common.h +98 -0
- data/ext/libffi/install-sh +323 -0
- data/ext/libffi/libffi.pc.in +10 -0
- data/ext/libffi/libtool-version +29 -0
- data/ext/libffi/ltcf-c.sh +861 -0
- data/ext/libffi/ltcf-cxx.sh +1069 -0
- data/ext/libffi/ltcf-gcj.sh +700 -0
- data/ext/libffi/ltconfig +2862 -0
- data/ext/libffi/ltmain.sh +6930 -0
- data/ext/libffi/man/Makefile.am +8 -0
- data/ext/libffi/man/Makefile.in +395 -0
- data/ext/libffi/man/ffi.3 +31 -0
- data/ext/libffi/man/ffi_call.3 +103 -0
- data/ext/libffi/man/ffi_prep_cif.3 +66 -0
- data/ext/libffi/mdate-sh +201 -0
- data/ext/libffi/missing +353 -0
- data/ext/libffi/mkinstalldirs +158 -0
- data/ext/libffi/src/alpha/ffi.c +284 -0
- data/ext/libffi/src/alpha/ffitarget.h +48 -0
- data/ext/libffi/src/alpha/osf.S +366 -0
- data/ext/libffi/src/arm/ffi.c +309 -0
- data/ext/libffi/src/arm/ffitarget.h +49 -0
- data/ext/libffi/src/arm/sysv.S +299 -0
- data/ext/libffi/src/closures.c +596 -0
- data/ext/libffi/src/cris/ffi.c +383 -0
- data/ext/libffi/src/cris/ffitarget.h +51 -0
- data/ext/libffi/src/cris/sysv.S +215 -0
- data/ext/libffi/src/debug.c +59 -0
- data/ext/libffi/src/dlmalloc.c +5099 -0
- data/ext/libffi/src/frv/eabi.S +128 -0
- data/ext/libffi/src/frv/ffi.c +292 -0
- data/ext/libffi/src/frv/ffitarget.h +61 -0
- data/ext/libffi/src/ia64/ffi.c +580 -0
- data/ext/libffi/src/ia64/ffitarget.h +50 -0
- data/ext/libffi/src/ia64/ia64_flags.h +40 -0
- data/ext/libffi/src/ia64/unix.S +560 -0
- data/ext/libffi/src/java_raw_api.c +359 -0
- data/ext/libffi/src/m32r/ffi.c +232 -0
- data/ext/libffi/src/m32r/ffitarget.h +48 -0
- data/ext/libffi/src/m32r/sysv.S +121 -0
- data/ext/libffi/src/m68k/ffi.c +278 -0
- data/ext/libffi/src/m68k/ffitarget.h +49 -0
- data/ext/libffi/src/m68k/sysv.S +234 -0
- data/ext/libffi/src/mips/ffi.c +926 -0
- data/ext/libffi/src/mips/ffitarget.h +202 -0
- data/ext/libffi/src/mips/n32.S +534 -0
- data/ext/libffi/src/mips/o32.S +381 -0
- data/ext/libffi/src/pa/ffi.c +709 -0
- data/ext/libffi/src/pa/ffitarget.h +77 -0
- data/ext/libffi/src/pa/hpux32.S +368 -0
- data/ext/libffi/src/pa/linux.S +357 -0
- data/ext/libffi/src/powerpc/aix.S +225 -0
- data/ext/libffi/src/powerpc/aix_closure.S +247 -0
- data/ext/libffi/src/powerpc/asm.h +125 -0
- data/ext/libffi/src/powerpc/darwin.S +245 -0
- data/ext/libffi/src/powerpc/darwin_closure.S +317 -0
- data/ext/libffi/src/powerpc/ffi.c +1429 -0
- data/ext/libffi/src/powerpc/ffi_darwin.c +800 -0
- data/ext/libffi/src/powerpc/ffitarget.h +118 -0
- data/ext/libffi/src/powerpc/linux64.S +187 -0
- data/ext/libffi/src/powerpc/linux64_closure.S +236 -0
- data/ext/libffi/src/powerpc/ppc_closure.S +327 -0
- data/ext/libffi/src/powerpc/sysv.S +230 -0
- data/ext/libffi/src/prep_cif.c +174 -0
- data/ext/libffi/src/raw_api.c +254 -0
- data/ext/libffi/src/s390/ffi.c +780 -0
- data/ext/libffi/src/s390/ffitarget.h +60 -0
- data/ext/libffi/src/s390/sysv.S +434 -0
- data/ext/libffi/src/sh/ffi.c +716 -0
- data/ext/libffi/src/sh/ffitarget.h +49 -0
- data/ext/libffi/src/sh/sysv.S +850 -0
- data/ext/libffi/src/sh64/ffi.c +453 -0
- data/ext/libffi/src/sh64/ffitarget.h +53 -0
- data/ext/libffi/src/sh64/sysv.S +530 -0
- data/ext/libffi/src/sparc/ffi.c +610 -0
- data/ext/libffi/src/sparc/ffitarget.h +66 -0
- data/ext/libffi/src/sparc/v8.S +272 -0
- data/ext/libffi/src/sparc/v9.S +307 -0
- data/ext/libffi/src/types.c +77 -0
- data/ext/libffi/src/x86/darwin.S +443 -0
- data/ext/libffi/src/x86/darwin64.S +416 -0
- data/ext/libffi/src/x86/ffi.c +475 -0
- data/ext/libffi/src/x86/ffi64.c +572 -0
- data/ext/libffi/src/x86/ffitarget.h +90 -0
- data/ext/libffi/src/x86/freebsd.S +458 -0
- data/ext/libffi/src/x86/sysv.S +437 -0
- data/ext/libffi/src/x86/unix64.S +418 -0
- data/ext/libffi/src/x86/win32.S +391 -0
- data/ext/libffi/testsuite/Makefile.am +71 -0
- data/ext/libffi/testsuite/Makefile.in +447 -0
- data/ext/libffi/testsuite/config/default.exp +1 -0
- data/ext/libffi/testsuite/lib/libffi-dg.exp +289 -0
- data/ext/libffi/testsuite/lib/target-libpath.exp +263 -0
- data/ext/libffi/testsuite/lib/wrapper.exp +45 -0
- data/ext/libffi/testsuite/libffi.call/call.exp +36 -0
- data/ext/libffi/testsuite/libffi.call/closure_fn0.c +97 -0
- data/ext/libffi/testsuite/libffi.call/closure_fn1.c +89 -0
- data/ext/libffi/testsuite/libffi.call/closure_fn2.c +89 -0
- data/ext/libffi/testsuite/libffi.call/closure_fn3.c +90 -0
- data/ext/libffi/testsuite/libffi.call/closure_fn4.c +97 -0
- data/ext/libffi/testsuite/libffi.call/closure_fn5.c +99 -0
- data/ext/libffi/testsuite/libffi.call/closure_fn6.c +98 -0
- data/ext/libffi/testsuite/libffi.call/closure_stdcall.c +72 -0
- data/ext/libffi/testsuite/libffi.call/cls_12byte.c +102 -0
- data/ext/libffi/testsuite/libffi.call/cls_16byte.c +103 -0
- data/ext/libffi/testsuite/libffi.call/cls_18byte.c +104 -0
- data/ext/libffi/testsuite/libffi.call/cls_19byte.c +110 -0
- data/ext/libffi/testsuite/libffi.call/cls_1_1byte.c +97 -0
- data/ext/libffi/testsuite/libffi.call/cls_20byte.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_20byte1.c +101 -0
- data/ext/libffi/testsuite/libffi.call/cls_24byte.c +121 -0
- data/ext/libffi/testsuite/libffi.call/cls_2byte.c +98 -0
- data/ext/libffi/testsuite/libffi.call/cls_3_1byte.c +103 -0
- data/ext/libffi/testsuite/libffi.call/cls_3byte1.c +98 -0
- data/ext/libffi/testsuite/libffi.call/cls_3byte2.c +98 -0
- data/ext/libffi/testsuite/libffi.call/cls_4_1byte.c +106 -0
- data/ext/libffi/testsuite/libffi.call/cls_4byte.c +98 -0
- data/ext/libffi/testsuite/libffi.call/cls_5_1_byte.c +117 -0
- data/ext/libffi/testsuite/libffi.call/cls_5byte.c +106 -0
- data/ext/libffi/testsuite/libffi.call/cls_64byte.c +132 -0
- data/ext/libffi/testsuite/libffi.call/cls_6_1_byte.c +121 -0
- data/ext/libffi/testsuite/libffi.call/cls_6byte.c +107 -0
- data/ext/libffi/testsuite/libffi.call/cls_7_1_byte.c +125 -0
- data/ext/libffi/testsuite/libffi.call/cls_7byte.c +105 -0
- data/ext/libffi/testsuite/libffi.call/cls_8byte.c +96 -0
- data/ext/libffi/testsuite/libffi.call/cls_9byte1.c +98 -0
- data/ext/libffi/testsuite/libffi.call/cls_9byte2.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_double.c +101 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_float.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_longdouble.c +100 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_pointer.c +101 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_sint16.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_sint32.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_sint64.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_uint16.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_uint32.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_align_uint64.c +100 -0
- data/ext/libffi/testsuite/libffi.call/cls_double.c +51 -0
- data/ext/libffi/testsuite/libffi.call/cls_float.c +51 -0
- data/ext/libffi/testsuite/libffi.call/cls_multi_schar.c +82 -0
- data/ext/libffi/testsuite/libffi.call/cls_multi_sshort.c +82 -0
- data/ext/libffi/testsuite/libffi.call/cls_multi_sshortchar.c +94 -0
- data/ext/libffi/testsuite/libffi.call/cls_multi_uchar.c +99 -0
- data/ext/libffi/testsuite/libffi.call/cls_multi_ushort.c +82 -0
- data/ext/libffi/testsuite/libffi.call/cls_multi_ushortchar.c +94 -0
- data/ext/libffi/testsuite/libffi.call/cls_schar.c +52 -0
- data/ext/libffi/testsuite/libffi.call/cls_sint.c +50 -0
- data/ext/libffi/testsuite/libffi.call/cls_sshort.c +50 -0
- data/ext/libffi/testsuite/libffi.call/cls_uchar.c +50 -0
- data/ext/libffi/testsuite/libffi.call/cls_uint.c +51 -0
- data/ext/libffi/testsuite/libffi.call/cls_ulonglong.c +54 -0
- data/ext/libffi/testsuite/libffi.call/cls_ushort.c +51 -0
- data/ext/libffi/testsuite/libffi.call/ffitest.h +86 -0
- data/ext/libffi/testsuite/libffi.call/float.c +59 -0
- data/ext/libffi/testsuite/libffi.call/float1.c +58 -0
- data/ext/libffi/testsuite/libffi.call/float2.c +57 -0
- data/ext/libffi/testsuite/libffi.call/float3.c +72 -0
- data/ext/libffi/testsuite/libffi.call/float4.c +62 -0
- data/ext/libffi/testsuite/libffi.call/many.c +69 -0
- data/ext/libffi/testsuite/libffi.call/many_win32.c +63 -0
- data/ext/libffi/testsuite/libffi.call/negint.c +53 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct.c +160 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct1.c +169 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct10.c +141 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct2.c +118 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct3.c +119 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct4.c +119 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct5.c +120 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct6.c +139 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct7.c +119 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct8.c +139 -0
- data/ext/libffi/testsuite/libffi.call/nested_struct9.c +139 -0
- data/ext/libffi/testsuite/libffi.call/problem1.c +98 -0
- data/ext/libffi/testsuite/libffi.call/promotion.c +59 -0
- data/ext/libffi/testsuite/libffi.call/pyobjc-tc.c +114 -0
- data/ext/libffi/testsuite/libffi.call/return_dbl.c +35 -0
- data/ext/libffi/testsuite/libffi.call/return_dbl1.c +43 -0
- data/ext/libffi/testsuite/libffi.call/return_dbl2.c +42 -0
- data/ext/libffi/testsuite/libffi.call/return_fl.c +35 -0
- data/ext/libffi/testsuite/libffi.call/return_fl1.c +36 -0
- data/ext/libffi/testsuite/libffi.call/return_fl2.c +49 -0
- data/ext/libffi/testsuite/libffi.call/return_fl3.c +42 -0
- data/ext/libffi/testsuite/libffi.call/return_ldl.c +34 -0
- data/ext/libffi/testsuite/libffi.call/return_ll.c +41 -0
- data/ext/libffi/testsuite/libffi.call/return_ll1.c +42 -0
- data/ext/libffi/testsuite/libffi.call/return_sc.c +36 -0
- data/ext/libffi/testsuite/libffi.call/return_sl.c +38 -0
- data/ext/libffi/testsuite/libffi.call/return_uc.c +38 -0
- data/ext/libffi/testsuite/libffi.call/return_ul.c +38 -0
- data/ext/libffi/testsuite/libffi.call/strlen.c +44 -0
- data/ext/libffi/testsuite/libffi.call/strlen_win32.c +44 -0
- data/ext/libffi/testsuite/libffi.call/struct1.c +65 -0
- data/ext/libffi/testsuite/libffi.call/struct2.c +67 -0
- data/ext/libffi/testsuite/libffi.call/struct3.c +59 -0
- data/ext/libffi/testsuite/libffi.call/struct4.c +63 -0
- data/ext/libffi/testsuite/libffi.call/struct5.c +65 -0
- data/ext/libffi/testsuite/libffi.call/struct6.c +64 -0
- data/ext/libffi/testsuite/libffi.call/struct7.c +74 -0
- data/ext/libffi/testsuite/libffi.call/struct8.c +80 -0
- data/ext/libffi/testsuite/libffi.call/struct9.c +67 -0
- data/ext/libffi/testsuite/libffi.special/ffitestcxx.h +86 -0
- data/ext/libffi/testsuite/libffi.special/special.exp +38 -0
- data/ext/libffi/testsuite/libffi.special/unwindtest.cc +123 -0
- data/ext/libffi/testsuite/libffi.special/unwindtest_ffi_call.cc +53 -0
- data/ext/libffi/texinfo.tex +7482 -0
- data/ext/rbffi.h +32 -0
- data/lib/ffi.rb +2 -0
- data/lib/ffi/buffer.rb +25 -0
- data/lib/ffi/callback.rb +10 -0
- data/lib/ffi/ffi.rb +259 -0
- data/lib/ffi/memorypointer.rb +169 -0
- data/lib/ffi/platform.rb +75 -0
- data/lib/ffi/struct.rb +263 -0
- data/nbproject/Makefile-Default.mk +54 -0
- data/nbproject/Makefile-impl.mk +123 -0
- data/nbproject/configurations.xml +169 -0
- data/nbproject/private/configurations.xml +22 -0
- data/nbproject/private/private.xml +7 -0
- data/nbproject/project.properties +0 -0
- data/nbproject/project.xml +11 -0
- data/samples/getlogin.rb +7 -0
- data/samples/getpid.rb +7 -0
- data/samples/gettimeofday.rb +13 -0
- data/samples/hello.rb +7 -0
- data/samples/qsort.rb +20 -0
- data/specs/buffer_spec.rb +141 -0
- data/specs/callback_spec.rb +31 -0
- data/specs/rbx/attach_function_spec.rb +26 -0
- data/specs/rbx/memory_pointer_spec.rb +108 -0
- data/specs/rbx/spec_helper.rb +1 -0
- data/specs/rbx/struct_spec.rb +13 -0
- data/specs/spec_helper.rb +2 -0
- metadata +359 -0
data/ext/MemoryPointer.c
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
#include <stdbool.h>
|
2
|
+
#include <stdint.h>
|
3
|
+
#include <limits.h>
|
4
|
+
#include <ruby.h>
|
5
|
+
#include "rbffi.h"
|
6
|
+
#include "AbstractMemory.h"
|
7
|
+
#include "MemoryPointer.h"
|
8
|
+
|
9
|
+
typedef struct MemoryPointer {
|
10
|
+
AbstractMemory memory;
|
11
|
+
VALUE parent;
|
12
|
+
bool autorelease;
|
13
|
+
bool allocated;
|
14
|
+
} MemoryPointer;
|
15
|
+
|
16
|
+
static VALUE memptr_allocate(VALUE self, VALUE size, VALUE count, VALUE clear);
|
17
|
+
static void memptr_release(MemoryPointer* ptr);
|
18
|
+
static void memptr_mark(MemoryPointer* ptr);
|
19
|
+
|
20
|
+
VALUE rb_FFI_MemoryPointer_class;
|
21
|
+
static VALUE classMemoryPointer = Qnil;
|
22
|
+
|
23
|
+
VALUE
|
24
|
+
rb_FFI_MemoryPointer_new(caddr_t addr)
|
25
|
+
{
|
26
|
+
MemoryPointer* p;
|
27
|
+
|
28
|
+
p = ALLOC(MemoryPointer);
|
29
|
+
memset(p, 0, sizeof(*p));
|
30
|
+
p->memory.address = addr;
|
31
|
+
p->memory.size = LONG_MAX;
|
32
|
+
p->parent = Qnil;
|
33
|
+
return Data_Wrap_Struct(classMemoryPointer, memptr_mark, memptr_release, p);
|
34
|
+
}
|
35
|
+
|
36
|
+
static VALUE
|
37
|
+
memptr_allocate(VALUE self, VALUE size, VALUE count, VALUE clear)
|
38
|
+
{
|
39
|
+
MemoryPointer* p;
|
40
|
+
|
41
|
+
p = ALLOC(MemoryPointer);
|
42
|
+
memset(p, 0, sizeof(*p));
|
43
|
+
p->autorelease = true;
|
44
|
+
p->memory.size = NUM2LONG(size) * (count == Qnil ? 1 : NUM2LONG(count));
|
45
|
+
p->memory.address = p->memory.size > 0 ? malloc(p->memory.size) : NULL;
|
46
|
+
p->parent = Qnil;
|
47
|
+
p->allocated = true;
|
48
|
+
|
49
|
+
if (p->memory.address == NULL) {
|
50
|
+
long size = p->memory.size;
|
51
|
+
xfree(p);
|
52
|
+
rb_raise(rb_eNoMemError, "Failed to allocate memory size=%ld bytes", size);
|
53
|
+
}
|
54
|
+
if (TYPE(clear) == T_TRUE) {
|
55
|
+
memset(p->memory.address, 0, p->memory.size);
|
56
|
+
}
|
57
|
+
return Data_Wrap_Struct(classMemoryPointer, memptr_mark, memptr_release, p);
|
58
|
+
}
|
59
|
+
|
60
|
+
static VALUE
|
61
|
+
memptr_plus(VALUE self, VALUE offset)
|
62
|
+
{
|
63
|
+
MemoryPointer* ptr = (MemoryPointer *) DATA_PTR(self);
|
64
|
+
MemoryPointer* p;
|
65
|
+
long off = NUM2LONG(offset);
|
66
|
+
|
67
|
+
checkBounds(&ptr->memory, off, 1);
|
68
|
+
p = ALLOC(MemoryPointer);
|
69
|
+
memset(p, 0, sizeof(*p));
|
70
|
+
p->memory.address = ptr->memory.address + off;;
|
71
|
+
p->memory.size = ptr->memory.size - off;;
|
72
|
+
p->parent = self;
|
73
|
+
p->allocated = false;
|
74
|
+
p->autorelease = true;
|
75
|
+
return Data_Wrap_Struct(classMemoryPointer, memptr_mark, memptr_release, p);
|
76
|
+
}
|
77
|
+
|
78
|
+
static VALUE
|
79
|
+
memptr_inspect(VALUE self)
|
80
|
+
{
|
81
|
+
MemoryPointer* ptr = (MemoryPointer *) DATA_PTR(self);
|
82
|
+
char tmp[100];
|
83
|
+
snprintf(tmp, sizeof(tmp), "#<MemoryPointer address=%p>", ptr->memory.address);
|
84
|
+
return rb_str_new2(tmp);
|
85
|
+
}
|
86
|
+
|
87
|
+
static VALUE
|
88
|
+
memptr_null_p(VALUE self)
|
89
|
+
{
|
90
|
+
MemoryPointer* ptr = (MemoryPointer *) DATA_PTR(self);
|
91
|
+
return ptr->memory.address == NULL ? Qtrue : Qfalse;
|
92
|
+
}
|
93
|
+
|
94
|
+
static VALUE
|
95
|
+
memptr_equals(VALUE self, VALUE other)
|
96
|
+
{
|
97
|
+
MemoryPointer* p1 = (MemoryPointer *) DATA_PTR(self);
|
98
|
+
MemoryPointer* p2;
|
99
|
+
|
100
|
+
if (!rb_obj_is_kind_of(other, classMemoryPointer)) {
|
101
|
+
rb_raise(rb_eArgError, "Comparing MemoryPointer with non MemoryPointer");
|
102
|
+
}
|
103
|
+
p2 = (MemoryPointer *) DATA_PTR(other);
|
104
|
+
return p1->memory.address == p2->memory.address ? Qtrue : Qfalse;
|
105
|
+
}
|
106
|
+
|
107
|
+
static VALUE
|
108
|
+
memptr_free(VALUE self)
|
109
|
+
{
|
110
|
+
MemoryPointer* ptr = (MemoryPointer *) DATA_PTR(self);
|
111
|
+
if (ptr->allocated) {
|
112
|
+
free(ptr->memory.address);
|
113
|
+
ptr->memory.address = NULL;
|
114
|
+
ptr->allocated = false;
|
115
|
+
}
|
116
|
+
return self;
|
117
|
+
}
|
118
|
+
|
119
|
+
static VALUE
|
120
|
+
memptr_autorelease(VALUE self, VALUE autorelease)
|
121
|
+
{
|
122
|
+
MemoryPointer* ptr = (MemoryPointer *) DATA_PTR(self);
|
123
|
+
ptr->autorelease = autorelease == Qtrue;
|
124
|
+
return self;
|
125
|
+
}
|
126
|
+
|
127
|
+
static VALUE
|
128
|
+
memptr_address(VALUE self)
|
129
|
+
{
|
130
|
+
MemoryPointer* ptr = (MemoryPointer *) DATA_PTR(self);
|
131
|
+
return ULL2NUM((uintptr_t) ptr->memory.address);
|
132
|
+
}
|
133
|
+
|
134
|
+
static void
|
135
|
+
memptr_release(MemoryPointer* ptr)
|
136
|
+
{
|
137
|
+
if (ptr->autorelease && ptr->allocated) {
|
138
|
+
free(ptr->memory.address);
|
139
|
+
}
|
140
|
+
xfree(ptr);
|
141
|
+
|
142
|
+
}
|
143
|
+
static void
|
144
|
+
memptr_mark(MemoryPointer* ptr)
|
145
|
+
{
|
146
|
+
if (ptr->parent != Qnil) {
|
147
|
+
rb_gc_mark(ptr->parent);
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
void
|
152
|
+
rb_FFI_MemoryPointer_Init()
|
153
|
+
{
|
154
|
+
VALUE moduleFFI = rb_define_module("FFI");
|
155
|
+
rb_FFI_MemoryPointer_class = classMemoryPointer = rb_define_class_under(moduleFFI, "MemoryPointer", rb_FFI_AbstractMemory_class);
|
156
|
+
rb_define_singleton_method(classMemoryPointer, "__allocate", memptr_allocate, 3);
|
157
|
+
rb_define_method(classMemoryPointer, "inspect", memptr_inspect, 0);
|
158
|
+
rb_define_method(classMemoryPointer, "+", memptr_plus, 1);
|
159
|
+
rb_define_method(classMemoryPointer, "null?", memptr_null_p, 0);
|
160
|
+
rb_define_method(classMemoryPointer, "autorelease=", memptr_autorelease, 1);
|
161
|
+
rb_define_method(classMemoryPointer, "free", memptr_free, 0);
|
162
|
+
rb_define_method(classMemoryPointer, "address", memptr_address, 0);
|
163
|
+
rb_define_method(classMemoryPointer, "==", memptr_equals, 1);
|
164
|
+
}
|
data/ext/MemoryPointer.h
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
/*
|
2
|
+
* File: MemoryPointer.h
|
3
|
+
* Author: wayne
|
4
|
+
*
|
5
|
+
* Created on August 28, 2008, 5:24 PM
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef _MEMORYPOINTER_H
|
9
|
+
#define _MEMORYPOINTER_H
|
10
|
+
|
11
|
+
#include <ruby.h>
|
12
|
+
|
13
|
+
#ifdef __cplusplus
|
14
|
+
extern "C" {
|
15
|
+
#endif
|
16
|
+
|
17
|
+
extern void rb_FFI_MemoryPointer_Init();
|
18
|
+
extern VALUE rb_FFI_MemoryPointer_new(caddr_t addr);
|
19
|
+
extern VALUE rb_FFI_MemoryPointer_class;
|
20
|
+
#ifdef __cplusplus
|
21
|
+
}
|
22
|
+
#endif
|
23
|
+
|
24
|
+
#endif /* _MEMORYPOINTER_H */
|
25
|
+
|
data/ext/Platform.c
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#include <sys/param.h>
|
2
|
+
#include <sys/types.h>
|
3
|
+
#include <stdbool.h>
|
4
|
+
#include <ruby.h>
|
5
|
+
#include <ctype.h>
|
6
|
+
#include "Platform.h"
|
7
|
+
|
8
|
+
static VALUE modulePlatform = Qnil;
|
9
|
+
|
10
|
+
/*
|
11
|
+
* Determine the cpu type at compile time - useful for MacOSX where the the
|
12
|
+
* system installed ruby incorrectly reports 'host_cpu' as 'powerpc' when running
|
13
|
+
* on intel.
|
14
|
+
*/
|
15
|
+
#ifdef __i386__
|
16
|
+
#define CPU "i386"
|
17
|
+
#elif defined(__ppc__) || defined(__powerpc__)
|
18
|
+
#define CPU "powerpc"
|
19
|
+
#elif defined(__x86_64__)
|
20
|
+
#define CPU "x86_64"
|
21
|
+
#elif defined(__sparc__)
|
22
|
+
#define CPU "sparc"
|
23
|
+
#elif defined(__sparcv9__)
|
24
|
+
#define CPU "sparcv9"
|
25
|
+
#else
|
26
|
+
#error "Unknown cpu type"
|
27
|
+
#endif
|
28
|
+
|
29
|
+
void
|
30
|
+
rb_FFI_Platform_Init()
|
31
|
+
{
|
32
|
+
VALUE moduleFFI = rb_define_module("FFI");
|
33
|
+
VALUE platform = rb_define_module_under(moduleFFI, "Platform");
|
34
|
+
rb_define_const(platform, "LONG_SIZE", INT2FIX(sizeof(long) * 8));
|
35
|
+
rb_define_const(platform, "ADDRESS_SIZE", INT2FIX(sizeof(void *) * 8));
|
36
|
+
rb_define_const(platform, "BYTE_ORDER", INT2FIX(BYTE_ORDER));
|
37
|
+
rb_define_const(platform, "LITTLE_ENDIAN", INT2FIX(LITTLE_ENDIAN));
|
38
|
+
rb_define_const(platform, "BIG_ENDIAN", INT2FIX(BIG_ENDIAN));
|
39
|
+
rb_define_const(platform, "CPU", rb_str_new2(CPU));
|
40
|
+
modulePlatform = platform;
|
41
|
+
}
|
data/ext/Platform.h
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
/*
|
2
|
+
* File: Platform.h
|
3
|
+
* Author: wayne
|
4
|
+
*
|
5
|
+
* Created on August 29, 2008, 2:09 PM
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef _PLATFORM_H
|
9
|
+
#define _PLATFORM_H
|
10
|
+
|
11
|
+
#ifdef __cplusplus
|
12
|
+
extern "C" {
|
13
|
+
#endif
|
14
|
+
|
15
|
+
extern void rb_FFI_Platform_Init();
|
16
|
+
|
17
|
+
|
18
|
+
#ifdef __cplusplus
|
19
|
+
}
|
20
|
+
#endif
|
21
|
+
|
22
|
+
#endif /* _PLATFORM_H */
|
23
|
+
|
data/ext/Types.c
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include "MemoryPointer.h"
|
3
|
+
#include "Types.h"
|
4
|
+
|
5
|
+
ffi_type*
|
6
|
+
rb_FFI_NativeTypeToFFI(NativeType type)
|
7
|
+
{
|
8
|
+
switch (type) {
|
9
|
+
case VOID:
|
10
|
+
return &ffi_type_void;
|
11
|
+
case INT8:
|
12
|
+
return &ffi_type_sint8;
|
13
|
+
case UINT8:
|
14
|
+
return &ffi_type_uint8;
|
15
|
+
case INT32:
|
16
|
+
return &ffi_type_sint32;
|
17
|
+
case UINT32:
|
18
|
+
return &ffi_type_uint32;
|
19
|
+
case INT64:
|
20
|
+
return &ffi_type_sint64;
|
21
|
+
case UINT64:
|
22
|
+
return &ffi_type_uint64;
|
23
|
+
case FLOAT32:
|
24
|
+
return &ffi_type_float;
|
25
|
+
case FLOAT64:
|
26
|
+
return &ffi_type_double;
|
27
|
+
case STRING:
|
28
|
+
case RBXSTRING:
|
29
|
+
case POINTER:
|
30
|
+
return &ffi_type_pointer;
|
31
|
+
default:
|
32
|
+
return NULL;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
VALUE
|
37
|
+
rb_FFI_NativeValueToRuby(NativeType type, const void* ptr)
|
38
|
+
{
|
39
|
+
switch (type) {
|
40
|
+
case VOID:
|
41
|
+
return Qnil;
|
42
|
+
case INT8:
|
43
|
+
case INT16:
|
44
|
+
case INT32:
|
45
|
+
return INT2FIX(*(int *) ptr);
|
46
|
+
case UINT8:
|
47
|
+
case UINT16:
|
48
|
+
case UINT32:
|
49
|
+
return INT2FIX(*(unsigned int *) ptr);
|
50
|
+
case INT64:
|
51
|
+
return LL2NUM(*(signed long long *) ptr);
|
52
|
+
case UINT64:
|
53
|
+
return ULL2NUM(*(unsigned long long *) ptr);
|
54
|
+
case FLOAT32:
|
55
|
+
return rb_float_new(*(float *) ptr);
|
56
|
+
case FLOAT64:
|
57
|
+
return rb_float_new(*(double *) ptr);
|
58
|
+
case STRING:
|
59
|
+
return rb_str_new2(*(char **) ptr);
|
60
|
+
case POINTER:
|
61
|
+
return rb_FFI_MemoryPointer_new(*(void **) ptr);
|
62
|
+
default:
|
63
|
+
rb_raise(rb_eRuntimeError, "Unknown type: %d", type);
|
64
|
+
}
|
65
|
+
}
|
data/ext/Types.h
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
/*
|
2
|
+
* File: Types.h
|
3
|
+
* Author: wayne
|
4
|
+
*
|
5
|
+
* Created on September 9, 2008, 9:47 PM
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef _TYPES_H
|
9
|
+
#define _TYPES_H
|
10
|
+
|
11
|
+
#ifdef __cplusplus
|
12
|
+
extern "C" {
|
13
|
+
#endif
|
14
|
+
|
15
|
+
typedef enum {
|
16
|
+
VOID,
|
17
|
+
INT8,
|
18
|
+
UINT8,
|
19
|
+
INT16,
|
20
|
+
UINT16,
|
21
|
+
INT32,
|
22
|
+
UINT32,
|
23
|
+
INT64,
|
24
|
+
UINT64,
|
25
|
+
FLOAT32,
|
26
|
+
FLOAT64,
|
27
|
+
POINTER,
|
28
|
+
CALLBACK,
|
29
|
+
BUFFER_IN,
|
30
|
+
BUFFER_OUT,
|
31
|
+
BUFFER_INOUT,
|
32
|
+
CHAR_ARRAY,
|
33
|
+
|
34
|
+
/**
|
35
|
+
* An immutable string. Nul terminated, but only copies in to the native function
|
36
|
+
*/
|
37
|
+
STRING,
|
38
|
+
/** A Rubinus :string arg - copies data both ways, and nul terminates */
|
39
|
+
RBXSTRING
|
40
|
+
} NativeType;
|
41
|
+
|
42
|
+
#include <ffi.h>
|
43
|
+
extern ffi_type* rb_FFI_NativeTypeToFFI(NativeType type);
|
44
|
+
VALUE rb_FFI_NativeValueToRuby(NativeType type, const void* ptr);
|
45
|
+
|
46
|
+
#ifdef __cplusplus
|
47
|
+
}
|
48
|
+
#endif
|
49
|
+
|
50
|
+
#endif /* _TYPES_H */
|
51
|
+
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'mkmf'
|
3
|
+
require 'rbconfig'
|
4
|
+
dir_config("ffi_c")
|
5
|
+
create_makefile("ffi_c")
|
6
|
+
File.open("Makefile", "a") do |mf|
|
7
|
+
mf.puts "include $(srcdir)/ffi.mk"
|
8
|
+
if Config::CONFIG['host_os'] =~ /darwin/
|
9
|
+
mf.puts "include $(srcdir)/libffi.darwin.mk"
|
10
|
+
else
|
11
|
+
mf.puts "include $(srcdir)/libffi.mk"
|
12
|
+
end
|
13
|
+
end
|
data/ext/ffi.c
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#include <sys/types.h>
|
2
|
+
#include <stdio.h>
|
3
|
+
#include <dlfcn.h>
|
4
|
+
#include <ruby.h>
|
5
|
+
|
6
|
+
#include <ffi.h>
|
7
|
+
|
8
|
+
#include "rbffi.h"
|
9
|
+
#include "AbstractMemory.h"
|
10
|
+
#include "MemoryPointer.h"
|
11
|
+
#include "Platform.h"
|
12
|
+
#include "Types.h"
|
13
|
+
|
14
|
+
|
15
|
+
void Init_ffi();
|
16
|
+
|
17
|
+
static VALUE moduleFFI = Qnil;
|
18
|
+
static VALUE moduleNativeType = Qnil;
|
19
|
+
|
20
|
+
void
|
21
|
+
Init_ffi_c() {
|
22
|
+
moduleFFI = rb_define_module("FFI");
|
23
|
+
moduleNativeType = rb_define_module_under(moduleFFI, "NativeType");
|
24
|
+
|
25
|
+
rb_define_const(moduleNativeType, "VOID", INT2FIX(VOID));
|
26
|
+
rb_define_const(moduleNativeType, "INT8", INT2FIX(INT8));
|
27
|
+
rb_define_const(moduleNativeType, "UINT8", INT2FIX(UINT8));
|
28
|
+
rb_define_const(moduleNativeType, "INT16", INT2FIX(INT16));
|
29
|
+
rb_define_const(moduleNativeType, "UINT16", INT2FIX(UINT16));
|
30
|
+
rb_define_const(moduleNativeType, "INT32", INT2FIX(INT32));
|
31
|
+
rb_define_const(moduleNativeType, "UINT32", INT2FIX(UINT32));
|
32
|
+
rb_define_const(moduleNativeType, "INT64", INT2FIX(INT64));
|
33
|
+
rb_define_const(moduleNativeType, "UINT64", INT2FIX(UINT64));
|
34
|
+
rb_define_const(moduleNativeType, "FLOAT32", INT2FIX(FLOAT32));
|
35
|
+
rb_define_const(moduleNativeType, "FLOAT64", INT2FIX(FLOAT64));
|
36
|
+
rb_define_const(moduleNativeType, "POINTER", INT2FIX(POINTER));
|
37
|
+
rb_define_const(moduleNativeType, "STRING", INT2FIX(STRING));
|
38
|
+
rb_define_const(moduleNativeType, "RBXSTRING", INT2FIX(RBXSTRING));
|
39
|
+
rb_define_const(moduleNativeType, "CHAR_ARRAY", INT2FIX(CHAR_ARRAY));
|
40
|
+
if (sizeof(long) == 4) {
|
41
|
+
rb_define_const(moduleNativeType, "LONG", INT2FIX(INT32));
|
42
|
+
rb_define_const(moduleNativeType, "ULONG", INT2FIX(UINT32));
|
43
|
+
} else {
|
44
|
+
rb_define_const(moduleNativeType, "LONG", INT2FIX(INT64));
|
45
|
+
rb_define_const(moduleNativeType, "ULONG", INT2FIX(UINT64));
|
46
|
+
}
|
47
|
+
rb_FFI_Platform_Init();
|
48
|
+
rb_FFI_AbstractMemory_Init();
|
49
|
+
rb_FFI_MemoryPointer_Init();
|
50
|
+
rb_FFI_Buffer_Init();
|
51
|
+
rb_FFI_Callback_Init();
|
52
|
+
rb_FFI_Invoker_Init();
|
53
|
+
}
|
54
|
+
|
data/ext/ffi.mk
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- makefile -*-
|
2
|
+
|
3
|
+
CFLAGS += -Werror
|
4
|
+
LIBS += -lpthread
|
5
|
+
INCFLAGS += -I$(BUILD_DIR) -I$(LIBFFI_BUILD_DIR)/include
|
6
|
+
LOCAL_LIBS += $(LIBFFI)
|
7
|
+
|
8
|
+
FFI_MMAP_EXEC = -DFFI_MMAP_EXEC_WRIT
|
9
|
+
FFI_CFLAGS = $(FFI_MMAP_EXEC) $(OFLAGS)
|
10
|
+
BUILD_DIR = $(PWD)/build
|
11
|
+
LIBFFI_SRC_DIR = $(PWD)/$(srcdir)/libffi
|
12
|
+
LIBFFI_BUILD_DIR = $(BUILD_DIR)/libffi
|
13
|
+
LIBFFI = $(LIBFFI_BUILD_DIR)/.libs/libffi_convenience.a
|
14
|
+
FFI_CONFIGURE = sh $(LIBFFI_SRC_DIR)/configure --disable-static \
|
15
|
+
--with-pic=yes --disable-dependency-tracking
|
16
|
+
|
17
|
+
$(OBJS): $(LIBFFI)
|
18
|
+
|
19
|
+
clean: libffi_clean
|
20
|
+
|
21
|
+
libffi_clean::
|
22
|
+
$(RM) -r $(BUILD_DIR)
|
23
|
+
|
24
|
+
|