ffi 1.2.0.pre1 → 1.2.0.pre2

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.

@@ -0,0 +1,45 @@
1
+ /*
2
+ * Copyright (C) 2007 Wayne Meissner
3
+ *
4
+ * All rights reserved.
5
+ *
6
+ * This file is part of ruby-ffi.
7
+ *
8
+ * This code is free software: you can redistribute it and/or modify it under
9
+ * the terms of the GNU Lesser General Public License version 3 only, as
10
+ * published by the Free Software Foundation.
11
+ *
12
+ * This code is distributed in the hope that it will be useful, but WITHOUT
13
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15
+ * version 3 for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+
21
+
22
+ #define MEMSET(buf, value, size) do { \
23
+ int i; for (i = 0; i < size; ++i) buf[i] = value; \
24
+ } while(0)
25
+ #define MEMCPY(dst, src, size) do { \
26
+ int i; for (i = 0; i < size; ++i) dst[i] = src[i]; \
27
+ } while(0)
28
+
29
+ #define FILL(JTYPE, CTYPE) \
30
+ void fill##JTYPE##Buffer(CTYPE* buf, CTYPE value, int size) { MEMSET(buf, value, size); }
31
+
32
+ #define COPY(JTYPE, CTYPE) \
33
+ void copy##JTYPE##Buffer(CTYPE* dst, CTYPE* src, int size) { MEMCPY(dst, src, size); }
34
+
35
+ #define FUNC(JTYPE, CTYPE) \
36
+ FILL(JTYPE, CTYPE); \
37
+ COPY(JTYPE, CTYPE)
38
+
39
+ FUNC(Byte, char);
40
+ FUNC(Short, short);
41
+ FUNC(Int, int);
42
+ FUNC(Long, long long);
43
+ FUNC(Float, float);
44
+ FUNC(Double, double);
45
+
@@ -0,0 +1,204 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * All rights reserved.
5
+ *
6
+ * This file is part of ruby-ffi.
7
+ *
8
+ * This code is free software: you can redistribute it and/or modify it under
9
+ * the terms of the GNU Lesser General Public License version 3 only, as
10
+ * published by the Free Software Foundation.
11
+ *
12
+ * This code is distributed in the hope that it will be useful, but WITHOUT
13
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15
+ * version 3 for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+
21
+ #include <stdlib.h>
22
+ #include <stdbool.h>
23
+ #ifndef _WIN32
24
+ # include <pthread.h>
25
+ #else
26
+ # include <windows.h>
27
+ # include <process.h>
28
+ #endif
29
+
30
+ #define R(T, rtype) rtype testClosureVr##T(rtype (*closure)(void)) { \
31
+ return closure != NULL ? (*closure)() : (rtype) 0; \
32
+ }
33
+
34
+ #define P(T, ptype) void testClosure##T##rV(void (*closure)(ptype), ptype a1) { \
35
+ if (closure != NULL) (*closure)(a1); \
36
+ }
37
+
38
+ void testClosureVrV(void (*closure)(void))
39
+ {
40
+ (*closure)();
41
+ }
42
+
43
+ R(Z, bool);
44
+ R(B, char);
45
+ R(S, short);
46
+ R(I, int);
47
+ R(L, long);
48
+ R(J, long long);
49
+ R(LL, long long);
50
+ R(F, float);
51
+ R(D, double);
52
+ R(P, const void*);
53
+
54
+
55
+ P(Z, bool);
56
+ P(B, char);
57
+ P(S, short);
58
+ P(I, int);
59
+ P(L, long);
60
+ P(J, long long);
61
+ P(LL, long long);
62
+ P(F, float);
63
+ P(D, double);
64
+ P(P, const void*);
65
+ P(UL, unsigned long);
66
+
67
+ void testOptionalClosureBrV(void (*closure)(char), char a1)
68
+ {
69
+ if (closure) {
70
+ (*closure)(a1);
71
+ }
72
+ }
73
+
74
+
75
+ struct ThreadVrV {
76
+ void (*closure)(void);
77
+ int count;
78
+ };
79
+
80
+ static void *
81
+ threadVrV(void *arg)
82
+ {
83
+ struct ThreadVrV* t = (struct ThreadVrV *) arg;
84
+
85
+ int i;
86
+ for (i = 0; i < t->count; i++) {
87
+ (*t->closure)();
88
+ }
89
+
90
+ return NULL;
91
+ }
92
+
93
+ void testThreadedClosureVrV(void (*closure)(void), int n)
94
+ {
95
+ struct ThreadVrV arg = {closure, n};
96
+ #ifndef _WIN32
97
+ pthread_t t;
98
+ pthread_create(&t, NULL, threadVrV, &arg);
99
+ pthread_join(t, NULL);
100
+ #else
101
+ HANDLE hThread = (HANDLE) _beginthread((void (*)(void *))threadVrV, 0, &arg);
102
+ WaitForSingleObject(hThread, INFINITE);
103
+ #endif
104
+ }
105
+
106
+ struct s8f32s32 {
107
+ char s8;
108
+ float f32;
109
+ int s32;
110
+ };
111
+
112
+ // Takes a struct argument
113
+ void testClosureTrV(void (*closure)(struct s8f32s32 s), struct s8f32s32* s)
114
+ {
115
+ (*closure)(*s);
116
+ }
117
+
118
+ // Returns a struct value
119
+ struct s8f32s32 testClosureVrT(struct s8f32s32 (*closure)())
120
+ {
121
+ return (*closure)();
122
+ }
123
+
124
+ typedef int (*returnTypeClosure_t)(int) ;
125
+ typedef returnTypeClosure_t (*lookupClosure_t)();
126
+
127
+ int testReturnsClosure(lookupClosure_t lookup, int val)
128
+ {
129
+ returnTypeClosure_t func = lookup ? (*lookup)() : NULL;
130
+ return func ? (*func)(val) : 0;
131
+ }
132
+
133
+ static int multiplyByTwo(int value)
134
+ {
135
+ return value * 2;
136
+ }
137
+
138
+ returnTypeClosure_t testReturnsFunctionPointer()
139
+ {
140
+ return multiplyByTwo;
141
+ }
142
+
143
+ typedef int (*argumentClosure_t)(int);
144
+ typedef int (*withArgumentClosure_t)(argumentClosure_t, int);
145
+
146
+ int testArgumentClosure(withArgumentClosure_t closure_with, argumentClosure_t closure_arg, int val)
147
+ {
148
+ return (*closure_with)(closure_arg, val);
149
+ }
150
+
151
+
152
+ //
153
+ // These macros produce functions of the form:
154
+ // testClosureBIrV(void (*closure)(char, int), char a1, int a2) {}
155
+ //
156
+ #define C2_(J1, J2, N1, N2) \
157
+ void testClosure##J1##J2##rV(void (*closure)(N1, N2), N1 a1, N2 a2) \
158
+ { \
159
+ if (closure != NULL) (*closure)(a1, a2); \
160
+ }
161
+
162
+ #define C2(J, N) \
163
+ C2_(B, J, char, N) \
164
+ C2_(S, J, short, N) \
165
+ C2_(I, J, int, N) \
166
+ C2_(LL, J, long long, N) \
167
+ C2_(F, J, float, N) \
168
+ C2_(D, J, double, N) \
169
+
170
+
171
+ C2(B, char);
172
+ C2(S, short);
173
+ C2(I, int);
174
+ C2(LL, long long);
175
+ C2(F, float);
176
+ C2(D, double);
177
+
178
+ #define C3_(J1, J2, J3, N1, N2, N3) \
179
+ void testClosure##J1##J2##J3##rV(void (*closure)(N1, N2, N3), N1 a1, N2 a2, N3 a3) \
180
+ { \
181
+ (*closure)(a1, a2, a3); \
182
+ }
183
+
184
+
185
+ #define C3(J, N) \
186
+ C3_(B, J, B, char, N, char) \
187
+ C3_(S, J, S, short, N, short) \
188
+ C3_(I, J, I, int, N, int) \
189
+ C3_(LL, J, LL, long long, N, long long) \
190
+ C3_(F, J, F, float, N, float) \
191
+ C3_(D, J, D, double, N, double) \
192
+
193
+ C3(B, char);
194
+ C3(S, short);
195
+ C3(I, int);
196
+ C3(LL, long long);
197
+ C3(F, float);
198
+ C3(D, double);
199
+ C3_(B, S, I, char, short, int);
200
+ C3_(B, S, LL, char, short, long long);
201
+ C3_(LL, S, B, long long, short, char);
202
+ C3_(LL, B, S, long long, char, short);
203
+
204
+
@@ -0,0 +1,48 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * All rights reserved.
5
+ *
6
+ * This file is part of ruby-ffi.
7
+ *
8
+ * This code is free software: you can redistribute it and/or modify it under
9
+ * the terms of the GNU Lesser General Public License version 3 only, as
10
+ * published by the Free Software Foundation.
11
+ *
12
+ * This code is distributed in the hope that it will be useful, but WITHOUT
13
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15
+ * version 3 for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+
21
+ int test_untagged_enum(int val) {
22
+ return val;
23
+ }
24
+
25
+ int test_untagged_typedef_enum(int val) {
26
+ return val;
27
+ }
28
+
29
+ typedef enum {c1, c2, c3, c4} enum_type1;
30
+ enum_type1 test_tagged_typedef_enum1(enum_type1 val) {
31
+ return val;
32
+ }
33
+
34
+ typedef enum {c5 = 42, c6, c7, c8} enum_type2;
35
+ enum_type2 test_tagged_typedef_enum2(enum_type2 val) {
36
+ return val;
37
+ }
38
+
39
+ typedef enum {c9 = 42, c10, c11 = 4242, c12} enum_type3;
40
+ enum_type3 test_tagged_typedef_enum3(enum_type3 val) {
41
+ return val;
42
+ }
43
+
44
+ typedef enum {c13 = 42, c14 = 4242, c15 = 424242, c16 = 42424242} enum_type4;
45
+ enum_type4 test_tagged_typedef_enum4(enum_type4 val) {
46
+ return val;
47
+ }
48
+
@@ -0,0 +1,72 @@
1
+ /*
2
+ * Copyright (c) 2009 Wayne Meissner. All rights reserved.
3
+ *
4
+ * All rights reserved.
5
+ *
6
+ * This file is part of ruby-ffi.
7
+ *
8
+ * This code is free software: you can redistribute it and/or modify it under
9
+ * the terms of the GNU Lesser General Public License version 3 only, as
10
+ * published by the Free Software Foundation.
11
+ *
12
+ * This code is distributed in the hope that it will be useful, but WITHOUT
13
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15
+ * version 3 for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+
21
+ #ifdef _WIN32
22
+ #include <windows.h>
23
+ #define sleep(x) Sleep(x)
24
+ #endif
25
+
26
+ #ifndef _WIN32
27
+ #include <unistd.h>
28
+ #include <pthread.h>
29
+ #endif
30
+
31
+ int testAdd(int a, int b)
32
+ {
33
+ return a + b;
34
+ };
35
+
36
+ int testFunctionAdd(int a, int b, int (*f)(int, int))
37
+ {
38
+ return f(a, b);
39
+ };
40
+
41
+ void testBlocking(int seconds) {
42
+ sleep(seconds);
43
+ };
44
+
45
+ struct async_data {
46
+ void (*fn)(int);
47
+ int value;
48
+ };
49
+
50
+ static void* asyncThreadCall(void *data)
51
+ {
52
+ struct async_data* d = (struct async_data *) data;
53
+ if (d != NULL && d->fn != NULL) {
54
+ (*d->fn)(d->value);
55
+ }
56
+
57
+ return NULL;
58
+ }
59
+
60
+ void testAsyncCallback(void (*fn)(int), int value)
61
+ {
62
+ #ifndef _WIN32
63
+ pthread_t t;
64
+ struct async_data d;
65
+ d.fn = fn;
66
+ d.value = value;
67
+ pthread_create(&t, NULL, asyncThreadCall, &d);
68
+ pthread_join(t, NULL);
69
+ #else
70
+ (*fn)(value);
71
+ #endif
72
+ }
@@ -0,0 +1,149 @@
1
+ # -*- makefile -*-
2
+
3
+ ifeq ($(OS),)
4
+ BUILD_OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
5
+ OS := $(BUILD_OS)
6
+ endif
7
+
8
+ ifeq ($(CPU),)
9
+ CPU := $(shell uname -m | sed -e 's/i[345678]86/i386/')
10
+ endif
11
+
12
+ PLATFORM = $(CPU)-$(OS)
13
+
14
+ ifeq ($(OS), sunos)
15
+ OS = solaris
16
+ endif
17
+
18
+ SRC_DIR = libtest
19
+ BUILD_DIR ?= build
20
+ TEST_BUILD_DIR = $(BUILD_DIR)/libtest
21
+ # Set defaults to unix (linux/solaris/bsd)
22
+ PREFIX = lib
23
+ LIBEXT ?= so
24
+ LIBNAME = $(PREFIX)test.$(LIBEXT)
25
+
26
+ export MACOSX_DEPLOYMENT_TARGET=10.4
27
+
28
+ CCACHE := $(strip $(realpath $(shell which ccache 2> /dev/null)))
29
+
30
+ TEST_SRCS = $(wildcard $(SRC_DIR)/*.c)
31
+ TEST_OBJS := $(patsubst $(SRC_DIR)/%.c, $(TEST_BUILD_DIR)/%.o, $(TEST_SRCS))
32
+
33
+ #
34
+ # Compiler/linker flags from:
35
+ # http://weblogs.java.net/blog/kellyohair/archive/2006/01/compilation_of_1.html
36
+ JFLAGS = -fno-omit-frame-pointer -fno-strict-aliasing
37
+ OFLAGS = -O2 $(JFLAGS)
38
+ WFLAGS = -W -Wall -Wno-unused -Wno-parentheses
39
+ PICFLAGS = -fPIC
40
+ SOFLAGS = -shared
41
+ LDFLAGS += $(SOFLAGS)
42
+
43
+ IFLAGS = -I"$(BUILD_DIR)"
44
+ CFLAGS = $(OFLAGS) $(WFLAGS) $(IFLAGS) $(PICFLAGS) -D_REENTRANT
45
+
46
+ ifneq ($(strip $(findstring $(OS), win32, mingw, cygwin)),)
47
+ # For cygwin => win32-native builds, strip out cygwin deps
48
+ ifneq ($(findstring cygwin, $(BUILD_OS)),)
49
+ CC += -mno-cygwin -mwin32
50
+ LDFLAGS += -mno-cygwin -Wl,--add-stdcall-alias
51
+ endif
52
+ PICFLAGS=
53
+ LIBEXT=dll
54
+ CC = gcc
55
+ endif
56
+
57
+ ifeq ($(OS), darwin)
58
+ ifneq ($(findstring $(CPU),ppc),)
59
+ ARCHFLAGS += -arch ppc
60
+ endif
61
+ ifneq ($(findstring $(CPU),i386 x86_64),)
62
+ ARCHFLAGS += -arch i386 -arch x86_64
63
+ endif
64
+ CFLAGS += $(ARCHFLAGS) -DTARGET_RT_MAC_CFM=0
65
+ CFLAGS += -fno-common
66
+ LDFLAGS = $(ARCHFLAGS) -dynamiclib
67
+ # link against the universal libraries on ppc machines
68
+ LDFLAGS += -L$(MACSDK)/usr/lib
69
+ LIBEXT = dylib
70
+ FFI_CFLAGS += -isysroot $(MACSDK)
71
+ PICFLAGS =
72
+ SOFLAGS =
73
+ endif
74
+
75
+ ifeq ($(OS), linux)
76
+ SOFLAGS += -Wl,-soname,$(LIBNAME)
77
+ endif
78
+
79
+ ifeq ($(OS), solaris)
80
+ CC = /usr/sfw/bin/gcc -std=c99
81
+ LD = /usr/ccs/bin/ld
82
+ SOFLAGS = -shared -static-libgcc
83
+ endif
84
+
85
+ ifeq ($(OS), aix)
86
+ LIBEXT = a
87
+ SOFLAGS = -shared -static-libgcc
88
+ PICFLAGS += -pthread
89
+ endif
90
+
91
+ ifneq ($(findstring bsd, $(OS)),)
92
+ SOFLAGS = -shared -static-libgcc
93
+ CFLAGS += -pthread
94
+ LDFLAGS += -pthread
95
+ endif
96
+
97
+ ifeq ($(CPU), i386)
98
+ MODEL = 32
99
+ endif
100
+
101
+ ifeq ($(CPU), sparcv9)
102
+ MODEL = 64
103
+ endif
104
+
105
+ ifeq ($(CPU), amd64)
106
+ MODEL = 64
107
+ endif
108
+
109
+ ifeq ($(CPU), x86_64)
110
+ MODEL = 64
111
+ endif
112
+
113
+ ifeq ($(CPU), ppc64)
114
+ MODEL = 64
115
+ endif
116
+
117
+ ifeq ($(CPU), powerpc64)
118
+ MODEL = 64
119
+ endif
120
+
121
+ MODELFLAG =
122
+ ifneq ($(MODEL),)
123
+ MODELFLAG = -m$(MODEL)
124
+ endif
125
+
126
+ # On platforms (linux, solaris) that support both 32bit and 64bit, force building for one or the other
127
+ ifneq ($(or $(findstring linux, $(OS)), $(findstring solaris, $(OS))),)
128
+ # Change the CC/LD instead of CFLAGS/LDFLAGS, incase other things in the flags
129
+ # makes the libffi build choke
130
+ CC += $(MODELFLAG)
131
+ LD += $(MODELFLAG)
132
+ endif
133
+
134
+ LIBTEST = $(BUILD_DIR)/$(LIBNAME)
135
+
136
+ all: $(LIBTEST)
137
+
138
+ $(TEST_BUILD_DIR)/%.o : $(SRC_DIR)/%.c
139
+ @mkdir -p $(@D)
140
+ $(CCACHE) $(CC) $(CFLAGS) -c $< -o $@
141
+
142
+ $(LIBTEST): $(TEST_OBJS)
143
+ $(CC) -o $@ $(LDFLAGS) $(TEST_OBJS) -lm
144
+
145
+ clean::
146
+ # nothing to do - ant will delete the build dir
147
+
148
+ debug::
149
+ @echo "SRCS=$(TEST_SRCS)"