alinta-ffi 1.9.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +49 -0
  3. data/LICENSE +24 -0
  4. data/README.md +112 -0
  5. data/Rakefile +243 -0
  6. data/ext/ffi_c/AbstractMemory.c +1109 -0
  7. data/ext/ffi_c/AbstractMemory.h +175 -0
  8. data/ext/ffi_c/ArrayType.c +162 -0
  9. data/ext/ffi_c/ArrayType.h +59 -0
  10. data/ext/ffi_c/Buffer.c +365 -0
  11. data/ext/ffi_c/Call.c +517 -0
  12. data/ext/ffi_c/Call.h +110 -0
  13. data/ext/ffi_c/ClosurePool.c +283 -0
  14. data/ext/ffi_c/ClosurePool.h +57 -0
  15. data/ext/ffi_c/DataConverter.c +91 -0
  16. data/ext/ffi_c/DynamicLibrary.c +339 -0
  17. data/ext/ffi_c/DynamicLibrary.h +98 -0
  18. data/ext/ffi_c/Function.c +998 -0
  19. data/ext/ffi_c/Function.h +87 -0
  20. data/ext/ffi_c/FunctionInfo.c +271 -0
  21. data/ext/ffi_c/LastError.c +184 -0
  22. data/ext/ffi_c/LastError.h +47 -0
  23. data/ext/ffi_c/LongDouble.c +63 -0
  24. data/ext/ffi_c/LongDouble.h +51 -0
  25. data/ext/ffi_c/MappedType.c +168 -0
  26. data/ext/ffi_c/MappedType.h +59 -0
  27. data/ext/ffi_c/MemoryPointer.c +197 -0
  28. data/ext/ffi_c/MemoryPointer.h +53 -0
  29. data/ext/ffi_c/MethodHandle.c +358 -0
  30. data/ext/ffi_c/MethodHandle.h +55 -0
  31. data/ext/ffi_c/Platform.c +129 -0
  32. data/ext/ffi_c/Platform.h +45 -0
  33. data/ext/ffi_c/Pointer.c +508 -0
  34. data/ext/ffi_c/Pointer.h +63 -0
  35. data/ext/ffi_c/Struct.c +829 -0
  36. data/ext/ffi_c/Struct.h +106 -0
  37. data/ext/ffi_c/StructByReference.c +190 -0
  38. data/ext/ffi_c/StructByReference.h +50 -0
  39. data/ext/ffi_c/StructByValue.c +150 -0
  40. data/ext/ffi_c/StructByValue.h +55 -0
  41. data/ext/ffi_c/StructLayout.c +698 -0
  42. data/ext/ffi_c/Thread.c +352 -0
  43. data/ext/ffi_c/Thread.h +95 -0
  44. data/ext/ffi_c/Type.c +397 -0
  45. data/ext/ffi_c/Type.h +62 -0
  46. data/ext/ffi_c/Types.c +139 -0
  47. data/ext/ffi_c/Types.h +89 -0
  48. data/ext/ffi_c/Variadic.c +304 -0
  49. data/ext/ffi_c/compat.h +78 -0
  50. data/ext/ffi_c/extconf.rb +71 -0
  51. data/ext/ffi_c/ffi.c +98 -0
  52. data/ext/ffi_c/libffi.bsd.mk +40 -0
  53. data/ext/ffi_c/libffi.darwin.mk +105 -0
  54. data/ext/ffi_c/libffi.gnu.mk +32 -0
  55. data/ext/ffi_c/libffi.mk +18 -0
  56. data/ext/ffi_c/libffi.vc.mk +26 -0
  57. data/ext/ffi_c/libffi.vc64.mk +26 -0
  58. data/ext/ffi_c/rbffi.h +57 -0
  59. data/ext/ffi_c/rbffi_endian.h +59 -0
  60. data/ext/ffi_c/win32/stdbool.h +8 -0
  61. data/ext/ffi_c/win32/stdint.h +201 -0
  62. data/ffi.gemspec +23 -0
  63. data/gen/Rakefile +30 -0
  64. data/lib/ffi.rb +20 -0
  65. data/lib/ffi/autopointer.rb +203 -0
  66. data/lib/ffi/buffer.rb +4 -0
  67. data/lib/ffi/callback.rb +4 -0
  68. data/lib/ffi/enum.rb +296 -0
  69. data/lib/ffi/errno.rb +43 -0
  70. data/lib/ffi/ffi.rb +44 -0
  71. data/lib/ffi/io.rb +62 -0
  72. data/lib/ffi/library.rb +590 -0
  73. data/lib/ffi/managedstruct.rb +84 -0
  74. data/lib/ffi/memorypointer.rb +1 -0
  75. data/lib/ffi/platform.rb +164 -0
  76. data/lib/ffi/platform/aarch64-linux/types.conf +104 -0
  77. data/lib/ffi/platform/arm-linux/types.conf +104 -0
  78. data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
  79. data/lib/ffi/platform/i386-darwin/types.conf +100 -0
  80. data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
  81. data/lib/ffi/platform/i386-gnu/types.conf +107 -0
  82. data/lib/ffi/platform/i386-linux/types.conf +103 -0
  83. data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
  84. data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
  85. data/lib/ffi/platform/i386-solaris/types.conf +122 -0
  86. data/lib/ffi/platform/i386-windows/types.conf +105 -0
  87. data/lib/ffi/platform/ia64-linux/types.conf +104 -0
  88. data/lib/ffi/platform/mips-linux/types.conf +102 -0
  89. data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
  90. data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
  91. data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
  92. data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
  93. data/lib/ffi/platform/powerpc-linux/types.conf +100 -0
  94. data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
  95. data/lib/ffi/platform/s390-linux/types.conf +102 -0
  96. data/lib/ffi/platform/s390x-linux/types.conf +102 -0
  97. data/lib/ffi/platform/sparc-linux/types.conf +102 -0
  98. data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
  99. data/lib/ffi/platform/sparc64-linux/types.conf +102 -0
  100. data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
  101. data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
  102. data/lib/ffi/platform/x86_64-darwin/types.conf +126 -0
  103. data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
  104. data/lib/ffi/platform/x86_64-linux/types.conf +102 -0
  105. data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
  106. data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
  107. data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
  108. data/lib/ffi/platform/x86_64-windows/types.conf +120 -0
  109. data/lib/ffi/pointer.rb +161 -0
  110. data/lib/ffi/struct.rb +371 -0
  111. data/lib/ffi/struct_layout_builder.rb +227 -0
  112. data/lib/ffi/tools/const_generator.rb +229 -0
  113. data/lib/ffi/tools/generator.rb +60 -0
  114. data/lib/ffi/tools/generator_task.rb +36 -0
  115. data/lib/ffi/tools/struct_generator.rb +194 -0
  116. data/lib/ffi/tools/types_generator.rb +134 -0
  117. data/lib/ffi/types.rb +194 -0
  118. data/lib/ffi/union.rb +43 -0
  119. data/lib/ffi/variadic.rb +78 -0
  120. data/lib/ffi/version.rb +4 -0
  121. data/libtest/Benchmark.c +52 -0
  122. data/libtest/BoolTest.c +34 -0
  123. data/libtest/BufferTest.c +31 -0
  124. data/libtest/ClosureTest.c +205 -0
  125. data/libtest/EnumTest.c +51 -0
  126. data/libtest/FunctionTest.c +70 -0
  127. data/libtest/GNUmakefile +149 -0
  128. data/libtest/GlobalVariable.c +62 -0
  129. data/libtest/LastErrorTest.c +21 -0
  130. data/libtest/NumberTest.c +132 -0
  131. data/libtest/PointerTest.c +63 -0
  132. data/libtest/ReferenceTest.c +23 -0
  133. data/libtest/StringTest.c +34 -0
  134. data/libtest/StructTest.c +243 -0
  135. data/libtest/UnionTest.c +43 -0
  136. data/libtest/VariadicTest.c +99 -0
  137. data/spec/ffi/LICENSE.SPECS +22 -0
  138. data/spec/ffi/async_callback_spec.rb +35 -0
  139. data/spec/ffi/bitmask_spec.rb +575 -0
  140. data/spec/ffi/bool_spec.rb +32 -0
  141. data/spec/ffi/buffer_spec.rb +279 -0
  142. data/spec/ffi/callback_spec.rb +773 -0
  143. data/spec/ffi/custom_param_type.rb +37 -0
  144. data/spec/ffi/custom_type_spec.rb +74 -0
  145. data/spec/ffi/dup_spec.rb +52 -0
  146. data/spec/ffi/enum_spec.rb +423 -0
  147. data/spec/ffi/errno_spec.rb +20 -0
  148. data/spec/ffi/ffi_spec.rb +28 -0
  149. data/spec/ffi/fixtures/Benchmark.c +52 -0
  150. data/spec/ffi/fixtures/BitmaskTest.c +51 -0
  151. data/spec/ffi/fixtures/BoolTest.c +34 -0
  152. data/spec/ffi/fixtures/BufferTest.c +31 -0
  153. data/spec/ffi/fixtures/ClosureTest.c +205 -0
  154. data/spec/ffi/fixtures/EnumTest.c +51 -0
  155. data/spec/ffi/fixtures/FunctionTest.c +142 -0
  156. data/spec/ffi/fixtures/GNUmakefile +149 -0
  157. data/spec/ffi/fixtures/GlobalVariable.c +62 -0
  158. data/spec/ffi/fixtures/LastErrorTest.c +21 -0
  159. data/spec/ffi/fixtures/NumberTest.c +132 -0
  160. data/spec/ffi/fixtures/PipeHelper.h +21 -0
  161. data/spec/ffi/fixtures/PipeHelperPosix.c +41 -0
  162. data/spec/ffi/fixtures/PipeHelperWindows.c +72 -0
  163. data/spec/ffi/fixtures/PointerTest.c +63 -0
  164. data/spec/ffi/fixtures/ReferenceTest.c +23 -0
  165. data/spec/ffi/fixtures/StringTest.c +34 -0
  166. data/spec/ffi/fixtures/StructTest.c +243 -0
  167. data/spec/ffi/fixtures/UnionTest.c +43 -0
  168. data/spec/ffi/fixtures/VariadicTest.c +99 -0
  169. data/spec/ffi/fixtures/classes.rb +438 -0
  170. data/spec/ffi/function_spec.rb +97 -0
  171. data/spec/ffi/io_spec.rb +16 -0
  172. data/spec/ffi/library_spec.rb +286 -0
  173. data/spec/ffi/long_double.rb +30 -0
  174. data/spec/ffi/managed_struct_spec.rb +68 -0
  175. data/spec/ffi/memorypointer_spec.rb +78 -0
  176. data/spec/ffi/number_spec.rb +247 -0
  177. data/spec/ffi/platform_spec.rb +114 -0
  178. data/spec/ffi/pointer_spec.rb +285 -0
  179. data/spec/ffi/rbx/attach_function_spec.rb +34 -0
  180. data/spec/ffi/rbx/memory_pointer_spec.rb +198 -0
  181. data/spec/ffi/rbx/spec_helper.rb +6 -0
  182. data/spec/ffi/rbx/struct_spec.rb +18 -0
  183. data/spec/ffi/spec_helper.rb +93 -0
  184. data/spec/ffi/string_spec.rb +118 -0
  185. data/spec/ffi/strptr_spec.rb +50 -0
  186. data/spec/ffi/struct_by_ref_spec.rb +43 -0
  187. data/spec/ffi/struct_callback_spec.rb +69 -0
  188. data/spec/ffi/struct_initialize_spec.rb +35 -0
  189. data/spec/ffi/struct_packed_spec.rb +50 -0
  190. data/spec/ffi/struct_spec.rb +882 -0
  191. data/spec/ffi/typedef_spec.rb +91 -0
  192. data/spec/ffi/union_spec.rb +67 -0
  193. data/spec/ffi/variadic_spec.rb +132 -0
  194. data/spec/spec.opts +4 -0
  195. metadata +309 -0
@@ -0,0 +1,110 @@
1
+ /*
2
+ * Copyright (c) 2009, Wayne Meissner
3
+ * Copyright (c) 2009, Luc Heinrich <luc@honk-honk.com>
4
+ * Copyright (c) 2009, Mike Dalessio <mike.dalessio@gmail.com>
5
+ * Copyright (c) 2009, Aman Gupta.
6
+ *
7
+ * Copyright (c) 2008-2013, Ruby FFI project contributors
8
+ * All rights reserved.
9
+ *
10
+ * Redistribution and use in source and binary forms, with or without
11
+ * modification, are permitted provided that the following conditions are met:
12
+ * * Redistributions of source code must retain the above copyright
13
+ * notice, this list of conditions and the following disclaimer.
14
+ * * Redistributions in binary form must reproduce the above copyright
15
+ * notice, this list of conditions and the following disclaimer in the
16
+ * documentation and/or other materials provided with the distribution.
17
+ * * Neither the name of the Ruby FFI project nor the
18
+ * names of its contributors may be used to endorse or promote products
19
+ * derived from this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
25
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ #ifndef RBFFI_CALL_H
34
+ #define RBFFI_CALL_H
35
+
36
+ #include "Thread.h"
37
+
38
+ #ifdef __cplusplus
39
+ extern "C" {
40
+ #endif
41
+
42
+ #if defined(__i386__) && \
43
+ (defined(HAVE_RAW_API) || defined(USE_INTERNAL_LIBFFI)) && \
44
+ !defined(_WIN32) && !defined(__WIN32__)
45
+ # define USE_RAW
46
+ #endif
47
+
48
+ #if (defined(__i386__) || defined(__x86_64__)) && !(defined(_WIN32) || defined(__WIN32__))
49
+ # define BYPASS_FFI 1
50
+ #endif
51
+
52
+ typedef union {
53
+ #ifdef USE_RAW
54
+ signed int s8, s16, s32;
55
+ unsigned int u8, u16, u32;
56
+ #else
57
+ signed char s8;
58
+ unsigned char u8;
59
+ signed short s16;
60
+ unsigned short u16;
61
+ signed int s32;
62
+ unsigned int u32;
63
+ #endif
64
+ signed long long i64;
65
+ unsigned long long u64;
66
+ signed long sl;
67
+ unsigned long ul;
68
+ void* ptr;
69
+ float f32;
70
+ double f64;
71
+ long double ld;
72
+ } FFIStorage;
73
+
74
+ extern void rbffi_Call_Init(VALUE moduleFFI);
75
+
76
+ extern void rbffi_SetupCallParams(int argc, VALUE* argv, int paramCount, Type** paramTypes,
77
+ FFIStorage* paramStorage, void** ffiValues,
78
+ VALUE* callbackParameters, int callbackCount, VALUE enums);
79
+
80
+ struct FunctionType_;
81
+ extern VALUE rbffi_CallFunction(int argc, VALUE* argv, void* function, struct FunctionType_* fnInfo);
82
+
83
+ typedef VALUE (*Invoker)(int argc, VALUE* argv, void* function, struct FunctionType_* fnInfo);
84
+
85
+ Invoker rbffi_GetInvoker(struct FunctionType_* fnInfo);
86
+
87
+ extern VALUE rbffi_GetEnumValue(VALUE enums, VALUE value);
88
+ extern int rbffi_GetSignedIntValue(VALUE value, int type, int minValue, int maxValue, const char* typeName, VALUE enums);
89
+
90
+ typedef struct rbffi_blocking_call {
91
+ rbffi_frame_t* frame;
92
+ void* function;
93
+ ffi_cif cif;
94
+ void **ffiValues;
95
+ void* retval;
96
+ void* params;
97
+ #if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
98
+ void* stkretval;
99
+ #endif
100
+ } rbffi_blocking_call_t;
101
+
102
+ VALUE rbffi_do_blocking_call(void* data);
103
+ VALUE rbffi_save_frame_exception(void *data, VALUE exc);
104
+
105
+ #ifdef __cplusplus
106
+ }
107
+ #endif
108
+
109
+ #endif /* RBFFI_CALL_H */
110
+
@@ -0,0 +1,283 @@
1
+ /*
2
+ * Copyright (c) 2009, 2010 Wayne Meissner
3
+ * Copyright (c) 2008-2013, Ruby FFI project contributors
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ * * Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * * Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * * Neither the name of the Ruby FFI project nor the
14
+ * names of its contributors may be used to endorse or promote products
15
+ * derived from this software without specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
29
+ #ifndef _MSC_VER
30
+ #include <sys/param.h>
31
+ #endif
32
+ #include <sys/types.h>
33
+ #if defined(__CYGWIN__) || !defined(_WIN32)
34
+ # include <sys/mman.h>
35
+ #endif
36
+ #include <stdio.h>
37
+ #ifndef _MSC_VER
38
+ # include <stdint.h>
39
+ # include <stdbool.h>
40
+ #else
41
+ # include "win32/stdbool.h"
42
+ # include "win32/stdint.h"
43
+ #endif
44
+ #if defined(__CYGWIN__) || !defined(_WIN32)
45
+ # include <unistd.h>
46
+ #else
47
+ # include <winsock2.h>
48
+ # define _WINSOCKAPI_
49
+ # include <windows.h>
50
+ #endif
51
+ #include <errno.h>
52
+ #include <ruby.h>
53
+
54
+ #if defined(_MSC_VER) && !defined(INT8_MIN)
55
+ # include "win32/stdint.h"
56
+ #endif
57
+ #include <ffi.h>
58
+ #include "rbffi.h"
59
+ #include "compat.h"
60
+
61
+ #include "Function.h"
62
+ #include "Types.h"
63
+ #include "Type.h"
64
+ #include "LastError.h"
65
+ #include "Call.h"
66
+
67
+ #include "ClosurePool.h"
68
+
69
+
70
+ #ifndef roundup
71
+ # define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
72
+ #endif
73
+ #ifdef _WIN32
74
+ typedef char* caddr_t;
75
+ #endif
76
+
77
+ typedef struct Memory {
78
+ void* code;
79
+ void* data;
80
+ struct Memory* next;
81
+ } Memory;
82
+
83
+ struct ClosurePool_ {
84
+ void* ctx;
85
+ int closureSize;
86
+ bool (*prep)(void* ctx, void *code, Closure* closure, char* errbuf, size_t errbufsize);
87
+ struct Memory* blocks; /* Keeps track of all the allocated memory for this pool */
88
+ Closure* list;
89
+ long refcnt;
90
+ };
91
+
92
+ static long pageSize;
93
+
94
+ static void* allocatePage(void);
95
+ static bool freePage(void *);
96
+ static bool protectPage(void *);
97
+
98
+ ClosurePool*
99
+ rbffi_ClosurePool_New(int closureSize,
100
+ bool (*prep)(void* ctx, void *code, Closure* closure, char* errbuf, size_t errbufsize),
101
+ void* ctx)
102
+ {
103
+ ClosurePool* pool;
104
+
105
+ pool = xcalloc(1, sizeof(*pool));
106
+ pool->closureSize = closureSize;
107
+ pool->ctx = ctx;
108
+ pool->prep = prep;
109
+ pool->refcnt = 1;
110
+
111
+ return pool;
112
+ }
113
+
114
+ void
115
+ cleanup_closure_pool(ClosurePool* pool)
116
+ {
117
+ Memory* memory;
118
+
119
+ for (memory = pool->blocks; memory != NULL; ) {
120
+ Memory* next = memory->next;
121
+ freePage(memory->code);
122
+ free(memory->data);
123
+ free(memory);
124
+ memory = next;
125
+ }
126
+ xfree(pool);
127
+ }
128
+
129
+ void
130
+ rbffi_ClosurePool_Free(ClosurePool* pool)
131
+ {
132
+ if (pool != NULL) {
133
+ long refcnt = --(pool->refcnt);
134
+ if (refcnt == 0) {
135
+ cleanup_closure_pool(pool);
136
+ }
137
+ }
138
+ }
139
+
140
+ Closure*
141
+ rbffi_Closure_Alloc(ClosurePool* pool)
142
+ {
143
+ Closure *list = NULL;
144
+ Memory* block = NULL;
145
+ caddr_t code = NULL;
146
+ char errmsg[256];
147
+ int nclosures;
148
+ long trampolineSize;
149
+ int i;
150
+
151
+ if (pool->list != NULL) {
152
+ Closure* closure = pool->list;
153
+ pool->list = pool->list->next;
154
+ pool->refcnt++;
155
+
156
+ return closure;
157
+ }
158
+
159
+ trampolineSize = roundup(pool->closureSize, 8);
160
+ nclosures = (int) (pageSize / trampolineSize);
161
+ block = calloc(1, sizeof(*block));
162
+ list = calloc(nclosures, sizeof(*list));
163
+ code = allocatePage();
164
+
165
+ if (block == NULL || list == NULL || code == NULL) {
166
+ snprintf(errmsg, sizeof(errmsg), "failed to allocate a page. errno=%d (%s)", errno, strerror(errno));
167
+ goto error;
168
+ }
169
+
170
+ for (i = 0; i < nclosures; ++i) {
171
+ Closure* closure = &list[i];
172
+ closure->next = &list[i + 1];
173
+ closure->pool = pool;
174
+ closure->code = (code + (i * trampolineSize));
175
+
176
+ if (!(*pool->prep)(pool->ctx, closure->code, closure, errmsg, sizeof(errmsg))) {
177
+ goto error;
178
+ }
179
+ }
180
+
181
+ if (!protectPage(code)) {
182
+ goto error;
183
+ }
184
+
185
+ /* Track the allocated page + Closure memory area */
186
+ block->data = list;
187
+ block->code = code;
188
+ block->next = pool->blocks;
189
+ pool->blocks = block;
190
+
191
+ /* Thread the new block onto the free list, apart from the first one. */
192
+ list[nclosures - 1].next = pool->list;
193
+ pool->list = list->next;
194
+ pool->refcnt++;
195
+
196
+ /* Use the first one as the new handle */
197
+ return list;
198
+
199
+ error:
200
+ free(block);
201
+ free(list);
202
+ if (code != NULL) {
203
+ freePage(code);
204
+ }
205
+
206
+
207
+ rb_raise(rb_eRuntimeError, "%s", errmsg);
208
+ return NULL;
209
+ }
210
+
211
+ void
212
+ rbffi_Closure_Free(Closure* closure)
213
+ {
214
+ if (closure != NULL) {
215
+ ClosurePool* pool = closure->pool;
216
+ long refcnt;
217
+ /* Just push it on the front of the free list */
218
+ closure->next = pool->list;
219
+ pool->list = closure;
220
+ refcnt = --(pool->refcnt);
221
+ if (refcnt == 0) {
222
+ cleanup_closure_pool(pool);
223
+ }
224
+ }
225
+ }
226
+
227
+ void*
228
+ rbffi_Closure_CodeAddress(Closure* handle)
229
+ {
230
+ return handle->code;
231
+ }
232
+
233
+
234
+ static long
235
+ getPageSize()
236
+ {
237
+ #if !defined(__CYGWIN__) && (defined(_WIN32) || defined(__WIN32__))
238
+ SYSTEM_INFO si;
239
+ GetSystemInfo(&si);
240
+ return si.dwPageSize;
241
+ #else
242
+ return sysconf(_SC_PAGESIZE);
243
+ #endif
244
+ }
245
+
246
+ static void*
247
+ allocatePage(void)
248
+ {
249
+ #if !defined(__CYGWIN__) && (defined(_WIN32) || defined(__WIN32__))
250
+ return VirtualAlloc(NULL, pageSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
251
+ #else
252
+ caddr_t page = mmap(NULL, pageSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
253
+ return (page != (caddr_t) -1) ? page : NULL;
254
+ #endif
255
+ }
256
+
257
+ static bool
258
+ freePage(void *addr)
259
+ {
260
+ #if !defined(__CYGWIN__) && (defined(_WIN32) || defined(__WIN32__))
261
+ return VirtualFree(addr, 0, MEM_RELEASE);
262
+ #else
263
+ return munmap(addr, pageSize) == 0;
264
+ #endif
265
+ }
266
+
267
+ static bool
268
+ protectPage(void* page)
269
+ {
270
+ #if !defined(__CYGWIN__) && (defined(_WIN32) || defined(__WIN32__))
271
+ DWORD oldProtect;
272
+ return VirtualProtect(page, pageSize, PAGE_EXECUTE_READ, &oldProtect);
273
+ #else
274
+ return mprotect(page, pageSize, PROT_READ | PROT_EXEC) == 0;
275
+ #endif
276
+ }
277
+
278
+ void
279
+ rbffi_ClosurePool_Init(VALUE module)
280
+ {
281
+ pageSize = getPageSize();
282
+ }
283
+
@@ -0,0 +1,57 @@
1
+ /*
2
+ * Copyright (c) 2009, 2010 Wayne Meissner
3
+ * Copyright (c) 2008-2013, Ruby FFI project contributors
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ * * Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * * Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * * Neither the name of the Ruby FFI project nor the
14
+ * names of its contributors may be used to endorse or promote products
15
+ * derived from this software without specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
29
+ #ifndef RUBYFFI_CLOSUREPOOL_H
30
+ #define RUBYFFI_CLOSUREPOOL_H
31
+
32
+ typedef struct ClosurePool_ ClosurePool;
33
+ typedef struct Closure_ Closure;
34
+
35
+ struct Closure_ {
36
+ void* info; /* opaque handle for storing closure-instance specific data */
37
+ void* function; /* closure-instance specific function, called by custom trampoline */
38
+ void* code; /* The native trampoline code location */
39
+ struct ClosurePool_* pool;
40
+ Closure* next;
41
+ };
42
+
43
+ void rbffi_ClosurePool_Init(VALUE module);
44
+
45
+ ClosurePool* rbffi_ClosurePool_New(int closureSize,
46
+ bool (*prep)(void* ctx, void *code, Closure* closure, char* errbuf, size_t errbufsize),
47
+ void* ctx);
48
+
49
+ void rbffi_ClosurePool_Free(ClosurePool *);
50
+
51
+ Closure* rbffi_Closure_Alloc(ClosurePool *);
52
+ void rbffi_Closure_Free(Closure *);
53
+
54
+ void* rbffi_Closure_GetCodeAddress(Closure *);
55
+
56
+ #endif /* RUBYFFI_CLOSUREPOOL_H */
57
+