ffi 1.0.9-x86-mingw32 → 1.0.12.pre-x86-mingw32

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.

Files changed (218) hide show
  1. data/Rakefile +16 -16
  2. data/ext/ffi_c/AbstractMemory.c +376 -14
  3. data/ext/ffi_c/AbstractMemory.h +8 -0
  4. data/ext/ffi_c/ArrayType.c +28 -0
  5. data/ext/ffi_c/Buffer.c +109 -25
  6. data/ext/ffi_c/Call.c +16 -5
  7. data/ext/ffi_c/ClosurePool.c +21 -8
  8. data/ext/ffi_c/DataConverter.c +29 -0
  9. data/ext/ffi_c/DynamicLibrary.c +71 -2
  10. data/ext/ffi_c/Function.c +122 -11
  11. data/ext/ffi_c/Function.h +6 -0
  12. data/ext/ffi_c/FunctionInfo.c +21 -1
  13. data/ext/ffi_c/LastError.c +24 -0
  14. data/ext/ffi_c/MappedType.c +22 -0
  15. data/ext/ffi_c/MemoryPointer.c +19 -1
  16. data/ext/ffi_c/MemoryPointer.h +6 -0
  17. data/ext/ffi_c/MethodHandle.c +26 -11
  18. data/ext/ffi_c/Platform.c +17 -3
  19. data/ext/ffi_c/Pointer.c +106 -0
  20. data/ext/ffi_c/Pointer.h +6 -0
  21. data/ext/ffi_c/Struct.c +10 -4
  22. data/ext/ffi_c/Struct.h +2 -1
  23. data/ext/ffi_c/StructByReference.c +8 -0
  24. data/ext/ffi_c/StructByValue.c +8 -0
  25. data/ext/ffi_c/StructLayout.c +8 -2
  26. data/ext/ffi_c/Thread.c +131 -1
  27. data/ext/ffi_c/Thread.h +6 -0
  28. data/ext/ffi_c/Type.c +111 -17
  29. data/ext/ffi_c/Types.c +9 -2
  30. data/ext/ffi_c/Types.h +4 -0
  31. data/ext/ffi_c/Variadic.c +13 -4
  32. data/ext/ffi_c/compat.h +8 -0
  33. data/ext/ffi_c/endian.h +10 -1
  34. data/ext/ffi_c/extconf.rb +51 -35
  35. data/ext/ffi_c/ffi.c +5 -0
  36. data/ext/ffi_c/libffi.darwin.mk +15 -15
  37. data/ext/ffi_c/libffi.gnu.mk +3 -3
  38. data/ext/ffi_c/libffi.mk +4 -4
  39. data/ext/ffi_c/libffi.vc.mk +26 -0
  40. data/ext/ffi_c/libffi.vc64.mk +26 -0
  41. data/ext/ffi_c/libffi/ChangeLog +541 -0
  42. data/ext/ffi_c/libffi/ChangeLog.libffi +13 -87
  43. data/ext/ffi_c/libffi/LICENSE +3 -3
  44. data/ext/ffi_c/libffi/Makefile.am +41 -32
  45. data/ext/ffi_c/libffi/Makefile.in +95 -66
  46. data/ext/ffi_c/libffi/Makefile.vc +141 -0
  47. data/ext/ffi_c/libffi/Makefile.vc64 +141 -0
  48. data/ext/ffi_c/libffi/README +40 -4
  49. data/ext/ffi_c/libffi/aclocal.m4 +729 -7854
  50. data/ext/ffi_c/libffi/build-ios.sh +67 -0
  51. data/ext/ffi_c/libffi/compile +11 -10
  52. data/ext/ffi_c/libffi/config.guess +4 -1
  53. data/ext/ffi_c/libffi/config.sub +6 -3
  54. data/ext/ffi_c/libffi/configure +6264 -6354
  55. data/ext/ffi_c/libffi/configure.ac +155 -63
  56. data/ext/ffi_c/libffi/depcomp +81 -35
  57. data/ext/ffi_c/libffi/doc/libffi.info +78 -18
  58. data/ext/ffi_c/libffi/doc/libffi.texi +64 -5
  59. data/ext/ffi_c/libffi/doc/stamp-vti +4 -4
  60. data/ext/ffi_c/libffi/doc/version.texi +4 -4
  61. data/ext/ffi_c/libffi/fficonfig.h.in +18 -0
  62. data/ext/ffi_c/libffi/fficonfig.hw +57 -0
  63. data/ext/ffi_c/libffi/include/Makefile.in +21 -3
  64. data/ext/ffi_c/libffi/include/ffi.h.in +42 -14
  65. data/ext/ffi_c/libffi/include/ffi.h.vc +427 -0
  66. data/ext/ffi_c/libffi/include/ffi.h.vc64 +427 -0
  67. data/ext/ffi_c/libffi/include/ffi_common.h +9 -5
  68. data/ext/ffi_c/libffi/install-sh +364 -167
  69. data/ext/ffi_c/libffi/ltmain.sh +2599 -1369
  70. data/ext/ffi_c/libffi/m4/ax_cc_maxopt.m4 +176 -0
  71. data/ext/ffi_c/libffi/m4/ax_cflags_warn_all.m4 +195 -0
  72. data/ext/ffi_c/libffi/m4/ax_check_compiler_flags.m4 +76 -0
  73. data/ext/ffi_c/libffi/m4/ax_compiler_vendor.m4 +63 -0
  74. data/ext/ffi_c/libffi/m4/ax_configure_args.m4 +70 -0
  75. data/ext/ffi_c/libffi/m4/ax_enable_builddir.m4 +300 -0
  76. data/ext/ffi_c/libffi/m4/ax_gcc_archflag.m4 +215 -0
  77. data/ext/ffi_c/libffi/m4/ax_gcc_x86_cpuid.m4 +79 -0
  78. data/ext/ffi_c/libffi/m4/libtool.m4 +1239 -768
  79. data/ext/ffi_c/libffi/m4/ltoptions.m4 +7 -6
  80. data/ext/ffi_c/libffi/m4/ltversion.m4 +6 -6
  81. data/ext/ffi_c/libffi/m4/lt~obsolete.m4 +9 -3
  82. data/ext/ffi_c/libffi/man/Makefile.in +21 -3
  83. data/ext/ffi_c/libffi/mdate-sh +0 -0
  84. data/ext/ffi_c/libffi/missing +60 -44
  85. data/ext/ffi_c/libffi/msvcc.sh +197 -0
  86. data/ext/ffi_c/libffi/src/alpha/osf.S +39 -18
  87. data/ext/ffi_c/libffi/src/arm/ffi.c +443 -24
  88. data/ext/ffi_c/libffi/src/arm/ffitarget.h +17 -1
  89. data/ext/ffi_c/libffi/src/arm/gentramp.sh +118 -0
  90. data/ext/ffi_c/libffi/src/arm/sysv.S +206 -15
  91. data/ext/ffi_c/libffi/src/arm/trampoline.S +4450 -0
  92. data/ext/ffi_c/libffi/src/avr32/ffi.c +4 -2
  93. data/ext/ffi_c/libffi/src/avr32/ffitarget.h +2 -2
  94. data/ext/ffi_c/libffi/src/closures.c +17 -35
  95. data/ext/ffi_c/libffi/src/cris/ffi.c +1 -1
  96. data/ext/ffi_c/libffi/src/cris/ffitarget.h +2 -2
  97. data/ext/ffi_c/libffi/src/dlmalloc.c +66 -4
  98. data/ext/ffi_c/libffi/src/frv/ffitarget.h +2 -6
  99. data/ext/ffi_c/libffi/src/ia64/ffi.c +7 -5
  100. data/ext/ffi_c/libffi/src/ia64/ffitarget.h +2 -2
  101. data/ext/ffi_c/libffi/src/java_raw_api.c +1 -1
  102. data/ext/ffi_c/libffi/src/m32r/ffitarget.h +2 -2
  103. data/ext/ffi_c/libffi/src/m68k/ffi.c +10 -0
  104. data/ext/ffi_c/libffi/src/m68k/ffitarget.h +2 -2
  105. data/ext/ffi_c/libffi/src/m68k/sysv.S +36 -0
  106. data/ext/ffi_c/libffi/src/mips/ffi.c +12 -5
  107. data/ext/ffi_c/libffi/src/mips/ffitarget.h +18 -11
  108. data/ext/ffi_c/libffi/src/mips/n32.S +4 -4
  109. data/ext/ffi_c/libffi/src/moxie/eabi.S +128 -0
  110. data/ext/ffi_c/libffi/src/moxie/ffi.c +276 -0
  111. data/ext/ffi_c/libffi/src/pa/ffi.c +7 -4
  112. data/ext/ffi_c/libffi/src/pa/ffitarget.h +6 -5
  113. data/ext/ffi_c/libffi/src/powerpc/aix.S +5 -1
  114. data/ext/ffi_c/libffi/src/powerpc/aix_closure.S +2 -0
  115. data/ext/ffi_c/libffi/src/powerpc/asm.h +1 -1
  116. data/ext/ffi_c/libffi/src/powerpc/darwin.S +215 -77
  117. data/ext/ffi_c/libffi/src/powerpc/darwin_closure.S +358 -100
  118. data/ext/ffi_c/libffi/src/powerpc/ffi.c +11 -5
  119. data/ext/ffi_c/libffi/src/powerpc/ffi_darwin.c +603 -172
  120. data/ext/ffi_c/libffi/src/powerpc/ffitarget.h +17 -4
  121. data/ext/ffi_c/libffi/src/prep_cif.c +16 -13
  122. data/ext/ffi_c/libffi/src/s390/ffitarget.h +4 -2
  123. data/ext/ffi_c/libffi/src/sh/ffitarget.h +2 -2
  124. data/ext/ffi_c/libffi/src/sh64/ffitarget.h +2 -2
  125. data/ext/ffi_c/libffi/src/sparc/ffi.c +55 -11
  126. data/ext/ffi_c/libffi/src/sparc/ffitarget.h +5 -3
  127. data/ext/ffi_c/libffi/src/x86/ffi.c +54 -92
  128. data/ext/ffi_c/libffi/src/x86/ffi64.c +17 -8
  129. data/ext/ffi_c/libffi/src/x86/ffitarget.h +15 -14
  130. data/ext/ffi_c/libffi/src/x86/sysv.S +40 -26
  131. data/ext/ffi_c/libffi/src/x86/unix64.S +4 -0
  132. data/ext/ffi_c/libffi/src/x86/win32.S +379 -191
  133. data/ext/ffi_c/libffi/src/x86/win64.S +15 -7
  134. data/ext/ffi_c/libffi/testsuite/Makefile.am +1 -1
  135. data/ext/ffi_c/libffi/testsuite/Makefile.in +22 -4
  136. data/ext/ffi_c/libffi/testsuite/lib/libffi.exp +350 -0
  137. data/ext/ffi_c/libffi/testsuite/libffi.call/call.exp +1 -5
  138. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_longdouble_split.c +1 -1
  139. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_longdouble_split2.c +1 -1
  140. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_sint64.c +1 -0
  141. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_uint64.c +1 -0
  142. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_double_va.c +3 -0
  143. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_longdouble.c +2 -2
  144. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_longdouble_va.c +3 -0
  145. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_pointer.c +1 -1
  146. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_pointer_stack.c +1 -1
  147. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_ulonglong.c +1 -0
  148. data/ext/ffi_c/libffi/testsuite/libffi.call/err_bad_abi.c +2 -3
  149. data/ext/ffi_c/libffi/testsuite/libffi.call/err_bad_typedef.c +2 -1
  150. data/ext/ffi_c/libffi/testsuite/libffi.call/ffitest.h +36 -0
  151. data/ext/ffi_c/libffi/testsuite/libffi.call/huge_struct.c +17 -17
  152. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ll1.c +1 -0
  153. data/ext/ffi_c/libffi/testsuite/libffi.call/stret_medium2.c +1 -0
  154. data/ext/ffi_c/libffi/testsuite/libffi.special/ffitestcxx.h +1 -1
  155. data/ext/ffi_c/libffi/testsuite/libffi.special/special.exp +1 -3
  156. data/ext/ffi_c/win32/stdint.h +199 -0
  157. data/gen/Rakefile +18 -2
  158. data/lib/1.8/ffi_c.so +0 -0
  159. data/lib/ffi.rb +13 -9
  160. data/lib/ffi/autopointer.rb +88 -26
  161. data/lib/ffi/enum.rb +42 -0
  162. data/lib/ffi/errno.rb +6 -1
  163. data/lib/ffi/ffi.rb +1 -0
  164. data/lib/ffi/io.rb +13 -2
  165. data/lib/ffi/library.rb +219 -24
  166. data/lib/ffi/memorypointer.rb +1 -33
  167. data/lib/ffi/platform.rb +21 -7
  168. data/lib/ffi/platform/arm-linux/types.conf +102 -0
  169. data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
  170. data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
  171. data/lib/ffi/platform/i486-gnu/types.conf +107 -0
  172. data/lib/ffi/platform/ia64-linux/types.conf +102 -0
  173. data/lib/ffi/platform/mips-linux/types.conf +102 -0
  174. data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
  175. data/lib/ffi/platform/powerpc-linux/types.conf +100 -0
  176. data/lib/ffi/platform/s390-linux/types.conf +102 -0
  177. data/lib/ffi/platform/s390x-linux/types.conf +102 -0
  178. data/lib/ffi/platform/sparc-linux/types.conf +102 -0
  179. data/lib/ffi/platform/x86_64-freebsd/types.conf +126 -0
  180. data/lib/ffi/platform/x86_64-netbsd/types.conf +126 -0
  181. data/lib/ffi/pointer.rb +44 -0
  182. data/lib/ffi/struct.rb +2 -6
  183. data/lib/ffi/struct_layout_builder.rb +2 -1
  184. data/lib/ffi/tools/const_generator.rb +78 -26
  185. data/lib/ffi/tools/types_generator.rb +8 -1
  186. data/lib/ffi/types.rb +21 -1
  187. data/spec/ffi/async_callback_spec.rb +2 -2
  188. data/spec/ffi/bool_spec.rb +6 -6
  189. data/spec/ffi/buffer_spec.rb +23 -23
  190. data/spec/ffi/callback_spec.rb +101 -102
  191. data/spec/ffi/custom_type_spec.rb +20 -24
  192. data/spec/ffi/dup_spec.rb +7 -7
  193. data/spec/ffi/enum_spec.rb +127 -127
  194. data/spec/ffi/errno_spec.rb +2 -2
  195. data/spec/ffi/ffi_spec.rb +3 -3
  196. data/spec/ffi/function_spec.rb +10 -10
  197. data/spec/ffi/library_spec.rb +30 -12
  198. data/spec/ffi/managed_struct_spec.rb +4 -4
  199. data/spec/ffi/number_spec.rb +40 -40
  200. data/spec/ffi/pointer_spec.rb +21 -24
  201. data/spec/ffi/rbx/memory_pointer_spec.rb +17 -15
  202. data/spec/ffi/rbx/struct_spec.rb +2 -2
  203. data/spec/ffi/spec_helper.rb +1 -1
  204. data/spec/ffi/string_spec.rb +9 -9
  205. data/spec/ffi/strptr_spec.rb +3 -3
  206. data/spec/ffi/struct_callback_spec.rb +7 -7
  207. data/spec/ffi/struct_initialize_spec.rb +2 -2
  208. data/spec/ffi/struct_packed_spec.rb +6 -6
  209. data/spec/ffi/struct_spec.rb +94 -84
  210. data/spec/ffi/typedef_spec.rb +15 -4
  211. data/spec/ffi/union_spec.rb +3 -3
  212. data/spec/ffi/variadic_spec.rb +17 -14
  213. data/tasks/extension.rake +0 -1
  214. data/tasks/gem.rake +0 -1
  215. data/tasks/rdoc.rake +1 -1
  216. data/tasks/yard.rake +11 -0
  217. metadata +50 -11
  218. data/lib/1.9/ffi_c.so +0 -0
@@ -21,7 +21,13 @@
21
21
  #ifndef RBFFI_POINTER_H
22
22
  #define RBFFI_POINTER_H
23
23
 
24
+ #ifndef _MSC_VER
24
25
  #include <stdbool.h>
26
+ #else
27
+ typedef int bool;
28
+ #define true 1
29
+ #define false 0
30
+ #endif
25
31
 
26
32
  #ifdef __cplusplus
27
33
  extern "C" {
@@ -22,9 +22,15 @@
22
22
  #include <sys/types.h>
23
23
 
24
24
  #include "Function.h"
25
+ #ifndef _MSC_VER
25
26
  #include <sys/param.h>
26
27
  #include <stdint.h>
27
28
  #include <stdbool.h>
29
+ #else
30
+ typedef int bool;
31
+ #define true 1
32
+ #define false 0
33
+ #endif
28
34
  #include <ruby.h>
29
35
  #include "rbffi.h"
30
36
  #include "compat.h"
@@ -47,7 +53,7 @@ typedef struct InlineArray_ {
47
53
  MemoryOp *op;
48
54
  Type* componentType;
49
55
  ArrayType* arrayType;
50
- unsigned int length;
56
+ int length;
51
57
  } InlineArray;
52
58
 
53
59
 
@@ -97,7 +103,7 @@ struct_initialize(int argc, VALUE* argv, VALUE self)
97
103
 
98
104
  /* Call up into ruby code to adjust the layout */
99
105
  if (nargs > 1) {
100
- s->rbLayout = rb_funcall2(CLASS_OF(self), id_layout, RARRAY_LEN(rest), RARRAY_PTR(rest));
106
+ s->rbLayout = rb_funcall2(CLASS_OF(self), id_layout, (int) RARRAY_LEN(rest), RARRAY_PTR(rest));
101
107
  } else {
102
108
  s->rbLayout = struct_class_layout(klass);
103
109
  }
@@ -354,7 +360,7 @@ struct_set_pointer(VALUE self, VALUE pointer)
354
360
  Data_Get_Struct(pointer, AbstractMemory, memory);
355
361
  layout = struct_layout(self);
356
362
 
357
- if (layout->base.ffiType->size > memory->size) {
363
+ if ((int) layout->base.ffiType->size > memory->size) {
358
364
  rb_raise(rb_eArgError, "memory of %ld bytes too small for struct %s (expected at least %ld)",
359
365
  memory->size, rb_obj_classname(self), (long) layout->base.ffiType->size);
360
366
  }
@@ -495,7 +501,7 @@ inline_array_offset(InlineArray* array, int index)
495
501
  rb_raise(rb_eIndexError, "index %d out of bounds", index);
496
502
  }
497
503
 
498
- return array->field->offset + (index * array->componentType->ffiType->size);
504
+ return (int) array->field->offset + (index * (int) array->componentType->ffiType->size);
499
505
  }
500
506
 
501
507
  static VALUE
@@ -22,6 +22,7 @@
22
22
  #ifndef RBFFI_STRUCT_H
23
23
  #define RBFFI_STRUCT_H
24
24
 
25
+ #include "extconf.h"
25
26
  #include "AbstractMemory.h"
26
27
  #include "Type.h"
27
28
  #ifdef RUBY_1_9
@@ -59,7 +60,7 @@ extern "C" {
59
60
  struct StructLayout_ {
60
61
  Type base;
61
62
  StructField** fields;
62
- unsigned int fieldCount;
63
+ int fieldCount;
63
64
  int size;
64
65
  int align;
65
66
  ffi_type** ffiTypes;
@@ -25,11 +25,19 @@
25
25
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
26
  */
27
27
 
28
+ #ifndef _MSC_VER
28
29
  #include <sys/param.h>
30
+ #endif
29
31
  #include <sys/types.h>
30
32
  #include <stdio.h>
33
+ #ifndef _MSC_VER
31
34
  #include <stdint.h>
32
35
  #include <stdbool.h>
36
+ #else
37
+ typedef int bool;
38
+ #define true 1
39
+ #define false 0
40
+ #endif
33
41
  #include <errno.h>
34
42
  #include <ruby.h>
35
43
 
@@ -18,11 +18,19 @@
18
18
  * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
19
19
  */
20
20
 
21
+ #ifndef _MSC_VER
21
22
  #include <sys/param.h>
23
+ #endif
22
24
  #include <sys/types.h>
23
25
  #include <stdio.h>
26
+ #ifndef _MSC_VER
24
27
  #include <stdint.h>
25
28
  #include <stdbool.h>
29
+ #else
30
+ typedef int bool;
31
+ #define true 1
32
+ #define false 0
33
+ #endif
26
34
  #include <errno.h>
27
35
  #include <ruby.h>
28
36
 
@@ -22,9 +22,15 @@
22
22
  #include <sys/types.h>
23
23
 
24
24
  #include "Function.h"
25
+ #ifndef _MSC_VER
25
26
  #include <sys/param.h>
26
27
  #include <stdint.h>
27
28
  #include <stdbool.h>
29
+ #else
30
+ typedef int bool;
31
+ #define true 1
32
+ #define false 0
33
+ #endif
28
34
  #include <ruby.h>
29
35
  #include "rbffi.h"
30
36
  #include "compat.h"
@@ -348,7 +354,7 @@ struct_layout_initialize(VALUE self, VALUE fields, VALUE size, VALUE align)
348
354
  int i;
349
355
 
350
356
  Data_Get_Struct(self, StructLayout, layout);
351
- layout->fieldCount = RARRAY_LEN(fields);
357
+ layout->fieldCount = (int) RARRAY_LEN(fields);
352
358
  layout->rbFieldMap = rb_hash_new();
353
359
  layout->rbFieldNames = rb_ary_new2(layout->fieldCount);
354
360
  layout->size = NUM2INT(size);
@@ -374,8 +380,8 @@ struct_layout_initialize(VALUE self, VALUE fields, VALUE size, VALUE align)
374
380
  }
375
381
  rbName = rb_funcall2(rbField, rb_intern("name"), 0, NULL);
376
382
 
377
- field = layout->fields[i];
378
383
  Data_Get_Struct(rbField, StructField, field);
384
+ layout->fields[i] = field;
379
385
 
380
386
  if (field->type == NULL || field->type->ffiType == NULL) {
381
387
  rb_raise(rb_eRuntimeError, "type of field %d not supported", i);
@@ -18,13 +18,21 @@
18
18
  * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
19
19
  */
20
20
 
21
+ #ifndef _MSC_VER
21
22
  #include <stdbool.h>
23
+ #else
24
+ typedef int bool;
25
+ #define true 1
26
+ #define false 0
27
+ #endif
22
28
 
23
29
  #ifndef _WIN32
24
30
  # include <pthread.h>
25
31
  # include <errno.h>
26
32
  # include <signal.h>
27
33
  #else
34
+ # include <winsock2.h>
35
+ # define _WINSOCKAPI_
28
36
  # include <windows.h>
29
37
  #endif
30
38
  #include <fcntl.h>
@@ -179,6 +187,113 @@ rbffi_thread_blocking_region(VALUE (*func)(void *), void *data1, void (*ubf)(voi
179
187
  }
180
188
 
181
189
  #else
190
+ /* win32 implementation */
191
+
192
+ struct BlockingThread {
193
+ HANDLE tid;
194
+ VALUE (*fn)(void *);
195
+ void *data;
196
+ void (*ubf)(void *);
197
+ void *data2;
198
+ VALUE retval;
199
+ int wrfd;
200
+ int rdfd;
201
+ };
202
+
203
+ static DWORD __stdcall
204
+ rbffi_blocking_thread(LPVOID args)
205
+ {
206
+ struct BlockingThread* thr = (struct BlockingThread *) args;
207
+ char c = 1;
208
+ VALUE retval;
209
+
210
+ retval = (*thr->fn)(thr->data);
211
+ thr->retval = retval;
212
+
213
+ write(thr->wrfd, &c, sizeof(c));
214
+
215
+ return 0;
216
+ }
217
+
218
+ static VALUE
219
+ wait_for_thread(void *data)
220
+ {
221
+ struct BlockingThread* thr = (struct BlockingThread *) data;
222
+ char c, res;
223
+ fd_set rfds;
224
+
225
+ FD_ZERO(&rfds);
226
+ FD_SET(thr->rdfd, &rfds);
227
+ rb_thread_select(thr->rdfd + 1, &rfds, NULL, NULL, NULL);
228
+ read(thr->rdfd, &c, 1);
229
+ return Qnil;
230
+ }
231
+
232
+ static VALUE
233
+ cleanup_blocking_thread(void *data, VALUE exc)
234
+ {
235
+ struct BlockingThread* thr = (struct BlockingThread *) data;
236
+
237
+ if (thr->ubf != (void (*)(void *)) -1) {
238
+ (*thr->ubf)(thr->data2);
239
+ } else {
240
+ TerminateThread(thr->tid, 0);
241
+ }
242
+
243
+ return exc;
244
+ }
245
+
246
+ VALUE
247
+ rbffi_thread_blocking_region(VALUE (*func)(void *), void *data1, void (*ubf)(void *), void *data2)
248
+ {
249
+ struct BlockingThread* thr;
250
+ int fd[2];
251
+ VALUE exc;
252
+ DWORD state;
253
+ DWORD res;
254
+
255
+ if (_pipe(fd, 1024, O_BINARY) == -1) {
256
+ rb_raise(rb_eSystemCallError, "_pipe() failed");
257
+ return Qnil;
258
+ }
259
+
260
+ thr = ALLOC_N(struct BlockingThread, 1);
261
+ thr->rdfd = fd[0];
262
+ thr->wrfd = fd[1];
263
+ thr->fn = func;
264
+ thr->data = data1;
265
+ thr->ubf = ubf;
266
+ thr->data2 = data2;
267
+ thr->retval = Qnil;
268
+
269
+ thr->tid = CreateThread(NULL, 0, rbffi_blocking_thread, thr, 0, NULL);
270
+ if (!thr->tid) {
271
+ close(fd[0]);
272
+ close(fd[1]);
273
+ xfree(thr);
274
+ rb_raise(rb_eSystemCallError, "CreateThread() failed");
275
+ return Qnil;
276
+ }
277
+
278
+ exc = rb_rescue2(wait_for_thread, (VALUE) thr, cleanup_blocking_thread, (VALUE) thr,
279
+ rb_eException);
280
+
281
+ /* The thread should be finished, already. */
282
+ WaitForSingleObject(thr->tid, INFINITE);
283
+ CloseHandle(thr->tid);
284
+ close(fd[1]);
285
+ close(fd[0]);
286
+ xfree(thr);
287
+
288
+ if (exc != Qnil) {
289
+ rb_exc_raise(exc);
290
+ }
291
+
292
+ return thr->retval;
293
+ }
294
+
295
+
296
+ #if 0
182
297
 
183
298
  /*
184
299
  * FIXME: someone needs to implement something similar to the posix pipe based
@@ -187,8 +302,23 @@ rbffi_thread_blocking_region(VALUE (*func)(void *), void *data1, void (*ubf)(voi
187
302
  VALUE
188
303
  rbffi_thread_blocking_region(VALUE (*func)(void *), void *data1, void (*ubf)(void *), void *data2)
189
304
  {
190
- return (*func)(data1);
305
+ #if !defined(HAVE_RUBY_THREAD_HAS_GVL_P)
306
+ rbffi_thread_t oldThread;
307
+ #endif
308
+ VALUE res;
309
+ #if !defined(HAVE_RUBY_THREAD_HAS_GVL_P)
310
+ oldThread = rbffi_active_thread;
311
+ rbffi_active_thread = rbffi_thread_self();
312
+ #endif
313
+
314
+ res = (*func)(data1);
315
+
316
+ #if !defined(HAVE_RUBY_THREAD_HAS_GVL_P)
317
+ rbffi_active_thread = oldThread;
318
+ #endif
319
+ return res;
191
320
  }
321
+ #endif
192
322
 
193
323
  #endif /* !_WIN32 */
194
324
 
@@ -21,7 +21,13 @@
21
21
  #ifndef RBFFI_THREAD_H
22
22
  #define RBFFI_THREAD_H
23
23
 
24
+ #ifndef _MSC_VER
24
25
  #include <stdbool.h>
26
+ #else
27
+ typedef int bool;
28
+ #define true 1
29
+ #define false 0
30
+ #endif
25
31
  #include <ruby.h>
26
32
  #include "extconf.h"
27
33
 
@@ -16,7 +16,10 @@
16
16
  * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
+ #ifndef _MSC_VER
19
20
  #include <sys/param.h>
21
+ #endif
22
+
20
23
  #include <sys/types.h>
21
24
  #include <ruby.h>
22
25
  #include <ffi.h>
@@ -51,6 +54,12 @@ type_allocate(VALUE klass)
51
54
  return obj;
52
55
  }
53
56
 
57
+ /*
58
+ * Document-method: initialize
59
+ * call-seq: initialize(value)
60
+ * @param [Fixnum,Type] value
61
+ * @return [self]
62
+ */
54
63
  static VALUE
55
64
  type_initialize(VALUE self, VALUE value)
56
65
  {
@@ -72,6 +81,11 @@ type_initialize(VALUE self, VALUE value)
72
81
  return self;
73
82
  }
74
83
 
84
+ /*
85
+ * call-seq: type.size
86
+ * @return [Fixnum]
87
+ * Return type's size, in bytes.
88
+ */
75
89
  static VALUE
76
90
  type_size(VALUE self)
77
91
  {
@@ -82,6 +96,10 @@ type_size(VALUE self)
82
96
  return INT2FIX(type->ffiType->size);
83
97
  }
84
98
 
99
+ /*
100
+ * call-seq: type.alignment
101
+ * @return [Fixnum]
102
+ */
85
103
  static VALUE
86
104
  type_alignment(VALUE self)
87
105
  {
@@ -92,6 +110,11 @@ type_alignment(VALUE self)
92
110
  return INT2FIX(type->ffiType->alignment);
93
111
  }
94
112
 
113
+ /*
114
+ * call-seq: type.inspect
115
+ * @return [String]
116
+ * Inspect {Type} object.
117
+ */
95
118
  static VALUE
96
119
  type_inspect(VALUE self)
97
120
  {
@@ -128,6 +151,11 @@ builtin_type_free(BuiltinType *type)
128
151
  xfree(type);
129
152
  }
130
153
 
154
+ /*
155
+ * call-seq: type.inspect
156
+ * @return [String]
157
+ * Inspect {Type::Builtin} object.
158
+ */
131
159
  static VALUE
132
160
  builtin_type_inspect(VALUE self)
133
161
  {
@@ -145,21 +173,29 @@ int
145
173
  rbffi_type_size(VALUE type)
146
174
  {
147
175
  int t = TYPE(type);
176
+
148
177
  if (t == T_FIXNUM || t == T_BIGNUM) {
149
178
  return NUM2INT(type);
179
+
150
180
  } else if (t == T_SYMBOL) {
151
181
  /*
152
182
  * Try looking up directly in the type and size maps
153
183
  */
154
184
  VALUE nType;
155
- if ((nType = rb_hash_aref(typeMap, type)) != Qnil) {
156
- VALUE nSize = rb_hash_aref(sizeMap, nType);
157
- if (TYPE(nSize) == T_FIXNUM) {
158
- return FIX2INT(nSize);
185
+ if ((nType = rb_hash_lookup(typeMap, type)) != Qnil) {
186
+ if (rb_obj_is_kind_of(nType, rbffi_TypeClass)) {
187
+ Type* type;
188
+ Data_Get_Struct(nType, Type, type);
189
+ return (int) type->ffiType->size;
190
+
191
+ } else if (rb_respond_to(nType, id_size)) {
192
+ return NUM2INT(rb_funcall2(nType, id_size, 0, NULL));
159
193
  }
160
194
  }
195
+
161
196
  // Not found - call up to the ruby version to resolve
162
197
  return NUM2INT(rb_funcall2(rbffi_FFIModule, id_type_size, 1, &type));
198
+
163
199
  } else {
164
200
  return NUM2INT(rb_funcall2(type, id_size, 0, NULL));
165
201
  }
@@ -174,7 +210,7 @@ rbffi_Type_Lookup(VALUE name)
174
210
  * Try looking up directly in the type Map
175
211
  */
176
212
  VALUE nType;
177
- if ((nType = rb_hash_aref(typeMap, name)) != Qnil && rb_obj_is_kind_of(nType, rbffi_TypeClass)) {
213
+ if ((nType = rb_hash_lookup(typeMap, name)) != Qnil && rb_obj_is_kind_of(nType, rbffi_TypeClass)) {
178
214
  return nType;
179
215
  }
180
216
  } else if (rb_obj_is_kind_of(name, rbffi_TypeClass)) {
@@ -196,7 +232,9 @@ rbffi_Type_Find(VALUE name)
196
232
  VALUE rbType = rbffi_Type_Lookup(name);
197
233
 
198
234
  if (!RTEST(rbType)) {
199
- rb_raise(rb_eTypeError, "invalid type, %s", RSTRING_PTR(rb_inspect(name)));
235
+ VALUE s = rb_inspect(name);
236
+ rb_raise(rb_eTypeError, "invalid type, %s", RSTRING_PTR(s));
237
+ RB_GC_GUARD(s);
200
238
  }
201
239
 
202
240
  return rbType;
@@ -206,8 +244,18 @@ void
206
244
  rbffi_Type_Init(VALUE moduleFFI)
207
245
  {
208
246
  VALUE moduleNativeType;
209
- VALUE classType = rbffi_TypeClass = rb_define_class_under(moduleFFI, "Type", rb_cObject);
210
-
247
+ /*
248
+ * Document-class: FFI::Type
249
+ * This class manages C types.
250
+ *
251
+ * It embbed {FFI::Type::Builtin} objects as constants (for names,
252
+ * see {FFI::NativeType}).
253
+ */
254
+ rbffi_TypeClass = rb_define_class_under(moduleFFI, "Type", rb_cObject);
255
+
256
+ /*
257
+ * Document-constant: FFI::TypeDefs
258
+ */
211
259
  rb_define_const(moduleFFI, "TypeDefs", typeMap = rb_hash_new());
212
260
  rb_define_const(moduleFFI, "SizeTypes", sizeMap = rb_hash_new());
213
261
  rb_global_variable(&typeMap);
@@ -216,19 +264,62 @@ rbffi_Type_Init(VALUE moduleFFI)
216
264
  id_type_size = rb_intern("type_size");
217
265
  id_size = rb_intern("size");
218
266
 
219
-
267
+ /*
268
+ * Document-class: FFI::Type::Builtin
269
+ * Class for Built-in types.
270
+ */
220
271
  classBuiltinType = rb_define_class_under(rbffi_TypeClass, "Builtin", rbffi_TypeClass);
272
+ /*
273
+ * Document-module: FFI::NativeType
274
+ * This module defines constants for native (C) types.
275
+ *
276
+ * ==Native type constants
277
+ * Native types are defined by constants :
278
+ * * INT8, SCHAR, CHAR
279
+ * * UINT8, UCHAR
280
+ * * INT16, SHORT, SSHORT
281
+ * * UINT16, USHORT
282
+ * * INT32,, INT, SINT
283
+ * * UINT32, UINT
284
+ * * INT64, LONG_LONG, SLONG_LONG
285
+ * * UINT64, ULONG_LONG
286
+ * * LONG, SLONG
287
+ * * ULONG
288
+ * * FLOAT32, FLOAT
289
+ * * FLOAT64, DOUBLE
290
+ * * POINTER
291
+ * * CALLBACK
292
+ * * FUNCTION
293
+ * * CHAR_ARRAY
294
+ * * BOOL
295
+ * * STRING (immutable string, nul terminated)
296
+ * * STRUCT (struct-b-value param or result)
297
+ * * ARRAY (array type definition)
298
+ * * MAPPED (custom native type)
299
+ * For function return type only :
300
+ * * VOID
301
+ * For function argument type only :
302
+ * * BUFFER_IN
303
+ * * BUFFER_OUT
304
+ * * VARARGS (function takes a variable number of arguments)
305
+ *
306
+ * All these constants are exported to {FFI} module prefixed with "TYPE_".
307
+ * They are objets from {FFI::Type::Builtin} class.
308
+ */
221
309
  moduleNativeType = rb_define_module_under(moduleFFI, "NativeType");
222
310
 
311
+ /*
312
+ * Document-global: FFI::Type
313
+ */
223
314
  rb_global_variable(&rbffi_TypeClass);
224
315
  rb_global_variable(&classBuiltinType);
225
316
  rb_global_variable(&moduleNativeType);
226
317
 
227
- rb_define_alloc_func(classType, type_allocate);
228
- rb_define_method(classType, "initialize", type_initialize, 1);
229
- rb_define_method(classType, "size", type_size, 0);
230
- rb_define_method(classType, "alignment", type_alignment, 0);
231
- rb_define_method(classType, "inspect", type_inspect, 0);
318
+ rb_define_alloc_func(rbffi_TypeClass, type_allocate);
319
+ rb_define_method(rbffi_TypeClass, "initialize", type_initialize, 1);
320
+ rb_define_method(rbffi_TypeClass, "size", type_size, 0);
321
+ rb_define_method(rbffi_TypeClass, "alignment", type_alignment, 0);
322
+ rb_define_method(rbffi_TypeClass, "inspect", type_inspect, 0);
232
323
 
233
324
  // Make Type::Builtin non-allocatable
234
325
  rb_undef_method(CLASS_OF(classBuiltinType), "new");
@@ -240,16 +331,19 @@ rbffi_Type_Init(VALUE moduleFFI)
240
331
  // Define all the builtin types
241
332
  #define T(x, ffiType) do { \
242
333
  VALUE t = Qnil; \
243
- rb_define_const(classType, #x, t = builtin_type_new(classBuiltinType, NATIVE_##x, ffiType, #x)); \
334
+ rb_define_const(rbffi_TypeClass, #x, t = builtin_type_new(classBuiltinType, NATIVE_##x, ffiType, #x)); \
244
335
  rb_define_const(moduleNativeType, #x, t); \
245
336
  rb_define_const(moduleFFI, "TYPE_" #x, t); \
246
337
  } while(0)
247
338
 
248
339
  #define A(old_type, new_type) do { \
249
- VALUE t = rb_const_get(classType, rb_intern(#old_type)); \
250
- rb_const_set(classType, rb_intern(#new_type), t); \
340
+ VALUE t = rb_const_get(rbffi_TypeClass, rb_intern(#old_type)); \
341
+ rb_const_set(rbffi_TypeClass, rb_intern(#new_type), t); \
251
342
  } while(0)
252
343
 
344
+ /*
345
+ * Document-constant: FFI::Type::Builtin::VOID
346
+ */
253
347
  T(VOID, &ffi_type_void);
254
348
  T(INT8, &ffi_type_sint8);
255
349
  A(INT8, SCHAR);