ffi 1.9.3-x64-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 (436) hide show
  1. data/COPYING +49 -0
  2. data/LICENSE +24 -0
  3. data/README.md +109 -0
  4. data/Rakefile +220 -0
  5. data/ext/ffi_c/AbstractMemory.c +1032 -0
  6. data/ext/ffi_c/AbstractMemory.h +175 -0
  7. data/ext/ffi_c/ArrayType.c +162 -0
  8. data/ext/ffi_c/ArrayType.h +59 -0
  9. data/ext/ffi_c/Buffer.c +365 -0
  10. data/ext/ffi_c/Call.c +465 -0
  11. data/ext/ffi_c/Call.h +93 -0
  12. data/ext/ffi_c/ClosurePool.c +283 -0
  13. data/ext/ffi_c/ClosurePool.h +57 -0
  14. data/ext/ffi_c/DataConverter.c +91 -0
  15. data/ext/ffi_c/DynamicLibrary.c +333 -0
  16. data/ext/ffi_c/DynamicLibrary.h +49 -0
  17. data/ext/ffi_c/Function.c +999 -0
  18. data/ext/ffi_c/Function.h +87 -0
  19. data/ext/ffi_c/FunctionInfo.c +271 -0
  20. data/ext/ffi_c/LastError.c +184 -0
  21. data/ext/ffi_c/LastError.h +47 -0
  22. data/ext/ffi_c/LongDouble.c +63 -0
  23. data/ext/ffi_c/LongDouble.h +51 -0
  24. data/ext/ffi_c/MappedType.c +168 -0
  25. data/ext/ffi_c/MappedType.h +59 -0
  26. data/ext/ffi_c/MemoryPointer.c +197 -0
  27. data/ext/ffi_c/MemoryPointer.h +53 -0
  28. data/ext/ffi_c/MethodHandle.c +360 -0
  29. data/ext/ffi_c/MethodHandle.h +55 -0
  30. data/ext/ffi_c/Platform.c +121 -0
  31. data/ext/ffi_c/Platform.h +45 -0
  32. data/ext/ffi_c/Pointer.c +508 -0
  33. data/ext/ffi_c/Pointer.h +63 -0
  34. data/ext/ffi_c/Struct.c +828 -0
  35. data/ext/ffi_c/Struct.h +106 -0
  36. data/ext/ffi_c/StructByReference.c +190 -0
  37. data/ext/ffi_c/StructByReference.h +50 -0
  38. data/ext/ffi_c/StructByValue.c +150 -0
  39. data/ext/ffi_c/StructByValue.h +55 -0
  40. data/ext/ffi_c/StructLayout.c +698 -0
  41. data/ext/ffi_c/Thread.c +352 -0
  42. data/ext/ffi_c/Thread.h +95 -0
  43. data/ext/ffi_c/Type.c +397 -0
  44. data/ext/ffi_c/Type.h +62 -0
  45. data/ext/ffi_c/Types.c +139 -0
  46. data/ext/ffi_c/Types.h +89 -0
  47. data/ext/ffi_c/Variadic.c +276 -0
  48. data/ext/ffi_c/compat.h +83 -0
  49. data/ext/ffi_c/extconf.rb +64 -0
  50. data/ext/ffi_c/ffi.c +98 -0
  51. data/ext/ffi_c/libffi.bsd.mk +34 -0
  52. data/ext/ffi_c/libffi.darwin.mk +95 -0
  53. data/ext/ffi_c/libffi.gnu.mk +31 -0
  54. data/ext/ffi_c/libffi.mk +13 -0
  55. data/ext/ffi_c/libffi.vc.mk +26 -0
  56. data/ext/ffi_c/libffi.vc64.mk +26 -0
  57. data/ext/ffi_c/libffi/ChangeLog +4600 -0
  58. data/ext/ffi_c/libffi/ChangeLog.libffi +584 -0
  59. data/ext/ffi_c/libffi/ChangeLog.libgcj +40 -0
  60. data/ext/ffi_c/libffi/ChangeLog.v1 +764 -0
  61. data/ext/ffi_c/libffi/LICENSE +21 -0
  62. data/ext/ffi_c/libffi/Makefile.am +196 -0
  63. data/ext/ffi_c/libffi/Makefile.in +1820 -0
  64. data/ext/ffi_c/libffi/Makefile.vc +141 -0
  65. data/ext/ffi_c/libffi/Makefile.vc64 +141 -0
  66. data/ext/ffi_c/libffi/README +342 -0
  67. data/ext/ffi_c/libffi/acinclude.m4 +92 -0
  68. data/ext/ffi_c/libffi/aclocal.m4 +1873 -0
  69. data/ext/ffi_c/libffi/build-ios.sh +67 -0
  70. data/ext/ffi_c/libffi/compile +143 -0
  71. data/ext/ffi_c/libffi/config.guess +1501 -0
  72. data/ext/ffi_c/libffi/config.sub +1705 -0
  73. data/ext/ffi_c/libffi/configure +17191 -0
  74. data/ext/ffi_c/libffi/configure.ac +496 -0
  75. data/ext/ffi_c/libffi/configure.host +11 -0
  76. data/ext/ffi_c/libffi/depcomp +630 -0
  77. data/ext/ffi_c/libffi/doc/libffi.info +593 -0
  78. data/ext/ffi_c/libffi/doc/libffi.texi +600 -0
  79. data/ext/ffi_c/libffi/doc/stamp-vti +4 -0
  80. data/ext/ffi_c/libffi/doc/version.texi +4 -0
  81. data/ext/ffi_c/libffi/fficonfig.h.in +199 -0
  82. data/ext/ffi_c/libffi/fficonfig.hw +57 -0
  83. data/ext/ffi_c/libffi/include/Makefile.am +9 -0
  84. data/ext/ffi_c/libffi/include/Makefile.in +487 -0
  85. data/ext/ffi_c/libffi/include/ffi.h.in +427 -0
  86. data/ext/ffi_c/libffi/include/ffi.h.vc +427 -0
  87. data/ext/ffi_c/libffi/include/ffi.h.vc64 +427 -0
  88. data/ext/ffi_c/libffi/include/ffi_common.h +126 -0
  89. data/ext/ffi_c/libffi/install-sh +520 -0
  90. data/ext/ffi_c/libffi/libffi.pc.in +10 -0
  91. data/ext/ffi_c/libffi/libtool-version +29 -0
  92. data/ext/ffi_c/libffi/ltmain.sh +9636 -0
  93. data/ext/ffi_c/libffi/m4/ax_cc_maxopt.m4 +176 -0
  94. data/ext/ffi_c/libffi/m4/ax_cflags_warn_all.m4 +195 -0
  95. data/ext/ffi_c/libffi/m4/ax_check_compiler_flags.m4 +76 -0
  96. data/ext/ffi_c/libffi/m4/ax_compiler_vendor.m4 +63 -0
  97. data/ext/ffi_c/libffi/m4/ax_configure_args.m4 +70 -0
  98. data/ext/ffi_c/libffi/m4/ax_enable_builddir.m4 +300 -0
  99. data/ext/ffi_c/libffi/m4/ax_gcc_archflag.m4 +215 -0
  100. data/ext/ffi_c/libffi/m4/ax_gcc_x86_cpuid.m4 +79 -0
  101. data/ext/ffi_c/libffi/m4/libtool.m4 +7831 -0
  102. data/ext/ffi_c/libffi/m4/ltoptions.m4 +369 -0
  103. data/ext/ffi_c/libffi/m4/ltsugar.m4 +123 -0
  104. data/ext/ffi_c/libffi/m4/ltversion.m4 +23 -0
  105. data/ext/ffi_c/libffi/m4/lt~obsolete.m4 +98 -0
  106. data/ext/ffi_c/libffi/man/Makefile.am +8 -0
  107. data/ext/ffi_c/libffi/man/Makefile.in +466 -0
  108. data/ext/ffi_c/libffi/man/ffi.3 +31 -0
  109. data/ext/ffi_c/libffi/man/ffi_call.3 +103 -0
  110. data/ext/ffi_c/libffi/man/ffi_prep_cif.3 +66 -0
  111. data/ext/ffi_c/libffi/mdate-sh +201 -0
  112. data/ext/ffi_c/libffi/missing +376 -0
  113. data/ext/ffi_c/libffi/msvcc.sh +197 -0
  114. data/ext/ffi_c/libffi/src/alpha/ffi.c +284 -0
  115. data/ext/ffi_c/libffi/src/alpha/ffitarget.h +48 -0
  116. data/ext/ffi_c/libffi/src/alpha/osf.S +387 -0
  117. data/ext/ffi_c/libffi/src/arm/ffi.c +728 -0
  118. data/ext/ffi_c/libffi/src/arm/ffitarget.h +65 -0
  119. data/ext/ffi_c/libffi/src/arm/gentramp.sh +118 -0
  120. data/ext/ffi_c/libffi/src/arm/sysv.S +497 -0
  121. data/ext/ffi_c/libffi/src/arm/trampoline.S +4450 -0
  122. data/ext/ffi_c/libffi/src/avr32/ffi.c +423 -0
  123. data/ext/ffi_c/libffi/src/avr32/ffitarget.h +50 -0
  124. data/ext/ffi_c/libffi/src/avr32/sysv.S +208 -0
  125. data/ext/ffi_c/libffi/src/closures.c +615 -0
  126. data/ext/ffi_c/libffi/src/cris/ffi.c +383 -0
  127. data/ext/ffi_c/libffi/src/cris/ffitarget.h +51 -0
  128. data/ext/ffi_c/libffi/src/cris/sysv.S +215 -0
  129. data/ext/ffi_c/libffi/src/debug.c +59 -0
  130. data/ext/ffi_c/libffi/src/dlmalloc.c +5161 -0
  131. data/ext/ffi_c/libffi/src/frv/eabi.S +128 -0
  132. data/ext/ffi_c/libffi/src/frv/ffi.c +292 -0
  133. data/ext/ffi_c/libffi/src/frv/ffitarget.h +57 -0
  134. data/ext/ffi_c/libffi/src/ia64/ffi.c +582 -0
  135. data/ext/ffi_c/libffi/src/ia64/ffitarget.h +50 -0
  136. data/ext/ffi_c/libffi/src/ia64/ia64_flags.h +40 -0
  137. data/ext/ffi_c/libffi/src/ia64/unix.S +560 -0
  138. data/ext/ffi_c/libffi/src/java_raw_api.c +356 -0
  139. data/ext/ffi_c/libffi/src/m32r/ffi.c +232 -0
  140. data/ext/ffi_c/libffi/src/m32r/ffitarget.h +48 -0
  141. data/ext/ffi_c/libffi/src/m32r/sysv.S +121 -0
  142. data/ext/ffi_c/libffi/src/m68k/ffi.c +288 -0
  143. data/ext/ffi_c/libffi/src/m68k/ffitarget.h +49 -0
  144. data/ext/ffi_c/libffi/src/m68k/sysv.S +270 -0
  145. data/ext/ffi_c/libffi/src/mips/ffi.c +1036 -0
  146. data/ext/ffi_c/libffi/src/mips/ffitarget.h +242 -0
  147. data/ext/ffi_c/libffi/src/mips/n32.S +591 -0
  148. data/ext/ffi_c/libffi/src/mips/o32.S +381 -0
  149. data/ext/ffi_c/libffi/src/moxie/eabi.S +128 -0
  150. data/ext/ffi_c/libffi/src/moxie/ffi.c +276 -0
  151. data/ext/ffi_c/libffi/src/pa/ffi.c +719 -0
  152. data/ext/ffi_c/libffi/src/pa/ffitarget.h +78 -0
  153. data/ext/ffi_c/libffi/src/pa/hpux32.S +368 -0
  154. data/ext/ffi_c/libffi/src/pa/linux.S +357 -0
  155. data/ext/ffi_c/libffi/src/powerpc/aix.S +328 -0
  156. data/ext/ffi_c/libffi/src/powerpc/aix_closure.S +445 -0
  157. data/ext/ffi_c/libffi/src/powerpc/asm.h +125 -0
  158. data/ext/ffi_c/libffi/src/powerpc/darwin.S +383 -0
  159. data/ext/ffi_c/libffi/src/powerpc/darwin_closure.S +575 -0
  160. data/ext/ffi_c/libffi/src/powerpc/ffi.c +1448 -0
  161. data/ext/ffi_c/libffi/src/powerpc/ffi_darwin.c +1359 -0
  162. data/ext/ffi_c/libffi/src/powerpc/ffitarget.h +139 -0
  163. data/ext/ffi_c/libffi/src/powerpc/linux64.S +187 -0
  164. data/ext/ffi_c/libffi/src/powerpc/linux64_closure.S +236 -0
  165. data/ext/ffi_c/libffi/src/powerpc/ppc_closure.S +327 -0
  166. data/ext/ffi_c/libffi/src/powerpc/sysv.S +219 -0
  167. data/ext/ffi_c/libffi/src/prep_cif.c +177 -0
  168. data/ext/ffi_c/libffi/src/raw_api.c +254 -0
  169. data/ext/ffi_c/libffi/src/s390/ffi.c +780 -0
  170. data/ext/ffi_c/libffi/src/s390/ffitarget.h +62 -0
  171. data/ext/ffi_c/libffi/src/s390/sysv.S +434 -0
  172. data/ext/ffi_c/libffi/src/sh/ffi.c +716 -0
  173. data/ext/ffi_c/libffi/src/sh/ffitarget.h +49 -0
  174. data/ext/ffi_c/libffi/src/sh/sysv.S +850 -0
  175. data/ext/ffi_c/libffi/src/sh64/ffi.c +468 -0
  176. data/ext/ffi_c/libffi/src/sh64/ffitarget.h +53 -0
  177. data/ext/ffi_c/libffi/src/sh64/sysv.S +539 -0
  178. data/ext/ffi_c/libffi/src/sparc/ffi.c +669 -0
  179. data/ext/ffi_c/libffi/src/sparc/ffitarget.h +68 -0
  180. data/ext/ffi_c/libffi/src/sparc/v8.S +313 -0
  181. data/ext/ffi_c/libffi/src/sparc/v9.S +307 -0
  182. data/ext/ffi_c/libffi/src/types.c +77 -0
  183. data/ext/ffi_c/libffi/src/x86/darwin.S +444 -0
  184. data/ext/ffi_c/libffi/src/x86/darwin64.S +416 -0
  185. data/ext/ffi_c/libffi/src/x86/ffi.c +644 -0
  186. data/ext/ffi_c/libffi/src/x86/ffi64.c +635 -0
  187. data/ext/ffi_c/libffi/src/x86/ffitarget.h +121 -0
  188. data/ext/ffi_c/libffi/src/x86/freebsd.S +458 -0
  189. data/ext/ffi_c/libffi/src/x86/sysv.S +468 -0
  190. data/ext/ffi_c/libffi/src/x86/unix64.S +426 -0
  191. data/ext/ffi_c/libffi/src/x86/win32.S +1065 -0
  192. data/ext/ffi_c/libffi/src/x86/win64.S +468 -0
  193. data/ext/ffi_c/libffi/testsuite/Makefile.am +80 -0
  194. data/ext/ffi_c/libffi/testsuite/Makefile.in +500 -0
  195. data/ext/ffi_c/libffi/testsuite/config/default.exp +1 -0
  196. data/ext/ffi_c/libffi/testsuite/lib/libffi-dg.exp +300 -0
  197. data/ext/ffi_c/libffi/testsuite/lib/libffi.exp +350 -0
  198. data/ext/ffi_c/libffi/testsuite/lib/target-libpath.exp +263 -0
  199. data/ext/ffi_c/libffi/testsuite/lib/wrapper.exp +45 -0
  200. data/ext/ffi_c/libffi/testsuite/libffi.call/call.exp +32 -0
  201. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn0.c +89 -0
  202. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn1.c +81 -0
  203. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn2.c +81 -0
  204. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn3.c +82 -0
  205. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn4.c +89 -0
  206. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn5.c +92 -0
  207. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn6.c +90 -0
  208. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_loc_fn0.c +95 -0
  209. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_stdcall.c +64 -0
  210. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_12byte.c +94 -0
  211. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_16byte.c +95 -0
  212. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_18byte.c +96 -0
  213. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_19byte.c +102 -0
  214. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_1_1byte.c +89 -0
  215. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_20byte.c +91 -0
  216. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_20byte1.c +93 -0
  217. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_24byte.c +113 -0
  218. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_2byte.c +90 -0
  219. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_3_1byte.c +95 -0
  220. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_3byte1.c +90 -0
  221. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_3byte2.c +90 -0
  222. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_4_1byte.c +98 -0
  223. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_4byte.c +90 -0
  224. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_5_1_byte.c +109 -0
  225. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_5byte.c +98 -0
  226. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_64byte.c +124 -0
  227. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_6_1_byte.c +113 -0
  228. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_6byte.c +99 -0
  229. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_7_1_byte.c +117 -0
  230. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_7byte.c +97 -0
  231. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_8byte.c +88 -0
  232. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_9byte1.c +90 -0
  233. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_9byte2.c +91 -0
  234. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_double.c +93 -0
  235. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_float.c +91 -0
  236. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_longdouble.c +92 -0
  237. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_longdouble_split.c +134 -0
  238. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_longdouble_split2.c +117 -0
  239. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_pointer.c +95 -0
  240. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_sint16.c +91 -0
  241. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_sint32.c +91 -0
  242. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_sint64.c +92 -0
  243. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_uint16.c +91 -0
  244. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_uint32.c +91 -0
  245. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_uint64.c +93 -0
  246. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_dbls_struct.c +66 -0
  247. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_double.c +43 -0
  248. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_double_va.c +60 -0
  249. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_float.c +42 -0
  250. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_longdouble.c +105 -0
  251. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_longdouble_va.c +60 -0
  252. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_schar.c +74 -0
  253. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_sshort.c +74 -0
  254. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_sshortchar.c +86 -0
  255. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_uchar.c +91 -0
  256. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_ushort.c +74 -0
  257. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_ushortchar.c +86 -0
  258. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_pointer.c +74 -0
  259. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_pointer_stack.c +140 -0
  260. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_schar.c +44 -0
  261. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_sint.c +42 -0
  262. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_sshort.c +42 -0
  263. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_uchar.c +42 -0
  264. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_uint.c +43 -0
  265. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_ulonglong.c +47 -0
  266. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_ushort.c +43 -0
  267. data/ext/ffi_c/libffi/testsuite/libffi.call/err_bad_abi.c +36 -0
  268. data/ext/ffi_c/libffi/testsuite/libffi.call/err_bad_typedef.c +26 -0
  269. data/ext/ffi_c/libffi/testsuite/libffi.call/ffitest.h +153 -0
  270. data/ext/ffi_c/libffi/testsuite/libffi.call/float.c +59 -0
  271. data/ext/ffi_c/libffi/testsuite/libffi.call/float1.c +58 -0
  272. data/ext/ffi_c/libffi/testsuite/libffi.call/float2.c +58 -0
  273. data/ext/ffi_c/libffi/testsuite/libffi.call/float3.c +72 -0
  274. data/ext/ffi_c/libffi/testsuite/libffi.call/float4.c +62 -0
  275. data/ext/ffi_c/libffi/testsuite/libffi.call/huge_struct.c +342 -0
  276. data/ext/ffi_c/libffi/testsuite/libffi.call/many.c +69 -0
  277. data/ext/ffi_c/libffi/testsuite/libffi.call/many_win32.c +63 -0
  278. data/ext/ffi_c/libffi/testsuite/libffi.call/negint.c +53 -0
  279. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct.c +152 -0
  280. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct1.c +161 -0
  281. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct10.c +133 -0
  282. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct2.c +110 -0
  283. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct3.c +111 -0
  284. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct4.c +111 -0
  285. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct5.c +112 -0
  286. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct6.c +131 -0
  287. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct7.c +111 -0
  288. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct8.c +131 -0
  289. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct9.c +131 -0
  290. data/ext/ffi_c/libffi/testsuite/libffi.call/problem1.c +90 -0
  291. data/ext/ffi_c/libffi/testsuite/libffi.call/promotion.c +59 -0
  292. data/ext/ffi_c/libffi/testsuite/libffi.call/pyobjc-tc.c +114 -0
  293. data/ext/ffi_c/libffi/testsuite/libffi.call/return_dbl.c +35 -0
  294. data/ext/ffi_c/libffi/testsuite/libffi.call/return_dbl1.c +43 -0
  295. data/ext/ffi_c/libffi/testsuite/libffi.call/return_dbl2.c +42 -0
  296. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl.c +35 -0
  297. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl1.c +36 -0
  298. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl2.c +49 -0
  299. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl3.c +42 -0
  300. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ldl.c +34 -0
  301. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ll.c +41 -0
  302. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ll1.c +43 -0
  303. data/ext/ffi_c/libffi/testsuite/libffi.call/return_sc.c +36 -0
  304. data/ext/ffi_c/libffi/testsuite/libffi.call/return_sl.c +38 -0
  305. data/ext/ffi_c/libffi/testsuite/libffi.call/return_uc.c +38 -0
  306. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ul.c +38 -0
  307. data/ext/ffi_c/libffi/testsuite/libffi.call/stret_large.c +145 -0
  308. data/ext/ffi_c/libffi/testsuite/libffi.call/stret_large2.c +148 -0
  309. data/ext/ffi_c/libffi/testsuite/libffi.call/stret_medium.c +124 -0
  310. data/ext/ffi_c/libffi/testsuite/libffi.call/stret_medium2.c +125 -0
  311. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen.c +44 -0
  312. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen_win32.c +44 -0
  313. data/ext/ffi_c/libffi/testsuite/libffi.call/struct1.c +65 -0
  314. data/ext/ffi_c/libffi/testsuite/libffi.call/struct2.c +67 -0
  315. data/ext/ffi_c/libffi/testsuite/libffi.call/struct3.c +59 -0
  316. data/ext/ffi_c/libffi/testsuite/libffi.call/struct4.c +63 -0
  317. data/ext/ffi_c/libffi/testsuite/libffi.call/struct5.c +65 -0
  318. data/ext/ffi_c/libffi/testsuite/libffi.call/struct6.c +64 -0
  319. data/ext/ffi_c/libffi/testsuite/libffi.call/struct7.c +74 -0
  320. data/ext/ffi_c/libffi/testsuite/libffi.call/struct8.c +80 -0
  321. data/ext/ffi_c/libffi/testsuite/libffi.call/struct9.c +67 -0
  322. data/ext/ffi_c/libffi/testsuite/libffi.call/testclosure.c +70 -0
  323. data/ext/ffi_c/libffi/testsuite/libffi.special/ffitestcxx.h +96 -0
  324. data/ext/ffi_c/libffi/testsuite/libffi.special/special.exp +35 -0
  325. data/ext/ffi_c/libffi/testsuite/libffi.special/unwindtest.cc +124 -0
  326. data/ext/ffi_c/libffi/testsuite/libffi.special/unwindtest_ffi_call.cc +53 -0
  327. data/ext/ffi_c/libffi/texinfo.tex +7210 -0
  328. data/ext/ffi_c/rbffi.h +57 -0
  329. data/ext/ffi_c/rbffi_endian.h +59 -0
  330. data/ext/ffi_c/win32/stdbool.h +8 -0
  331. data/ext/ffi_c/win32/stdint.h +201 -0
  332. data/ffi.gemspec +22 -0
  333. data/gen/Rakefile +30 -0
  334. data/lib/ffi.rb +28 -0
  335. data/lib/ffi/autopointer.rb +194 -0
  336. data/lib/ffi/buffer.rb +4 -0
  337. data/lib/ffi/callback.rb +4 -0
  338. data/lib/ffi/enum.rb +173 -0
  339. data/lib/ffi/errno.rb +43 -0
  340. data/lib/ffi/ffi.rb +44 -0
  341. data/lib/ffi/io.rb +62 -0
  342. data/lib/ffi/library.rb +499 -0
  343. data/lib/ffi/managedstruct.rb +84 -0
  344. data/lib/ffi/memorypointer.rb +1 -0
  345. data/lib/ffi/platform.rb +148 -0
  346. data/lib/ffi/platform/arm-linux/types.conf +104 -0
  347. data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
  348. data/lib/ffi/platform/i386-darwin/types.conf +100 -0
  349. data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
  350. data/lib/ffi/platform/i386-gnu/types.conf +107 -0
  351. data/lib/ffi/platform/i386-linux/types.conf +103 -0
  352. data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
  353. data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
  354. data/lib/ffi/platform/i386-solaris/types.conf +122 -0
  355. data/lib/ffi/platform/i386-windows/types.conf +105 -0
  356. data/lib/ffi/platform/ia64-linux/types.conf +104 -0
  357. data/lib/ffi/platform/mips-linux/types.conf +102 -0
  358. data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
  359. data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
  360. data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
  361. data/lib/ffi/platform/powerpc-linux/types.conf +100 -0
  362. data/lib/ffi/platform/s390-linux/types.conf +102 -0
  363. data/lib/ffi/platform/s390x-linux/types.conf +102 -0
  364. data/lib/ffi/platform/sparc-linux/types.conf +102 -0
  365. data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
  366. data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
  367. data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
  368. data/lib/ffi/platform/x86_64-darwin/types.conf +100 -0
  369. data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
  370. data/lib/ffi/platform/x86_64-linux/types.conf +102 -0
  371. data/lib/ffi/platform/x86_64-netbsd/types.conf +126 -0
  372. data/lib/ffi/platform/x86_64-openbsd/types.conf +128 -0
  373. data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
  374. data/lib/ffi/platform/x86_64-windows/types.conf +27 -0
  375. data/lib/ffi/pointer.rb +134 -0
  376. data/lib/ffi/struct.rb +367 -0
  377. data/lib/ffi/struct_layout_builder.rb +222 -0
  378. data/lib/ffi/tools/const_generator.rb +229 -0
  379. data/lib/ffi/tools/generator.rb +60 -0
  380. data/lib/ffi/tools/generator_task.rb +36 -0
  381. data/lib/ffi/tools/struct_generator.rb +194 -0
  382. data/lib/ffi/tools/types_generator.rb +135 -0
  383. data/lib/ffi/types.rb +190 -0
  384. data/lib/ffi/union.rb +43 -0
  385. data/lib/ffi/variadic.rb +78 -0
  386. data/lib/ffi/version.rb +4 -0
  387. data/lib/ffi_c.so +0 -0
  388. data/libtest/Benchmark.c +52 -0
  389. data/libtest/BoolTest.c +34 -0
  390. data/libtest/BufferTest.c +31 -0
  391. data/libtest/ClosureTest.c +190 -0
  392. data/libtest/EnumTest.c +34 -0
  393. data/libtest/FunctionTest.c +58 -0
  394. data/libtest/GNUmakefile +149 -0
  395. data/libtest/GlobalVariable.c +62 -0
  396. data/libtest/LastErrorTest.c +21 -0
  397. data/libtest/NumberTest.c +132 -0
  398. data/libtest/PointerTest.c +63 -0
  399. data/libtest/ReferenceTest.c +23 -0
  400. data/libtest/StringTest.c +34 -0
  401. data/libtest/StructTest.c +243 -0
  402. data/libtest/UnionTest.c +43 -0
  403. data/libtest/VariadicTest.c +62 -0
  404. data/spec/ffi/async_callback_spec.rb +35 -0
  405. data/spec/ffi/bool_spec.rb +29 -0
  406. data/spec/ffi/buffer_spec.rb +251 -0
  407. data/spec/ffi/callback_spec.rb +667 -0
  408. data/spec/ffi/custom_param_type.rb +36 -0
  409. data/spec/ffi/custom_type_spec.rb +74 -0
  410. data/spec/ffi/dup_spec.rb +54 -0
  411. data/spec/ffi/enum_spec.rb +220 -0
  412. data/spec/ffi/errno_spec.rb +18 -0
  413. data/spec/ffi/ffi_spec.rb +29 -0
  414. data/spec/ffi/function_spec.rb +76 -0
  415. data/spec/ffi/library_spec.rb +216 -0
  416. data/spec/ffi/long_double.rb +30 -0
  417. data/spec/ffi/managed_struct_spec.rb +57 -0
  418. data/spec/ffi/number_spec.rb +236 -0
  419. data/spec/ffi/pointer_spec.rb +265 -0
  420. data/spec/ffi/rbx/attach_function_spec.rb +28 -0
  421. data/spec/ffi/rbx/memory_pointer_spec.rb +123 -0
  422. data/spec/ffi/rbx/spec_helper.rb +1 -0
  423. data/spec/ffi/rbx/struct_spec.rb +13 -0
  424. data/spec/ffi/spec_helper.rb +33 -0
  425. data/spec/ffi/string_spec.rb +108 -0
  426. data/spec/ffi/strptr_spec.rb +50 -0
  427. data/spec/ffi/struct_by_ref_spec.rb +43 -0
  428. data/spec/ffi/struct_callback_spec.rb +69 -0
  429. data/spec/ffi/struct_initialize_spec.rb +35 -0
  430. data/spec/ffi/struct_packed_spec.rb +51 -0
  431. data/spec/ffi/struct_spec.rb +748 -0
  432. data/spec/ffi/typedef_spec.rb +78 -0
  433. data/spec/ffi/union_spec.rb +65 -0
  434. data/spec/ffi/variadic_spec.rb +92 -0
  435. data/spec/spec.opts +4 -0
  436. metadata +562 -0
@@ -0,0 +1,333 @@
1
+ /*
2
+ * Copyright (c) 2008-2010 Wayne Meissner
3
+ *
4
+ * Copyright (c) 2008-2013, Ruby FFI project contributors
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ * * Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * * Neither the name of the Ruby FFI project nor the
15
+ * names of its contributors may be used to endorse or promote products
16
+ * derived from this software without specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
30
+ #include <sys/types.h>
31
+ #include <stdio.h>
32
+ #ifndef _MSC_VER
33
+ # include <stdint.h>
34
+ #endif
35
+ #if (defined(_WIN32) || defined(__WIN32__)) && !defined(__CYGWIN__)
36
+ # include <winsock2.h>
37
+ # define _WINSOCKAPI_
38
+ # include <windows.h>
39
+ #else
40
+ # include <dlfcn.h>
41
+ #endif
42
+ #include <ruby.h>
43
+ #if defined(_MSC_VER) && !defined(INT8_MIN)
44
+ # include "win32/stdint.h"
45
+ #endif
46
+
47
+ #include <ffi.h>
48
+
49
+ #include "rbffi.h"
50
+ #include "compat.h"
51
+ #include "AbstractMemory.h"
52
+ #include "Pointer.h"
53
+ #include "DynamicLibrary.h"
54
+
55
+ typedef struct LibrarySymbol_ {
56
+ Pointer base;
57
+ VALUE library;
58
+ VALUE name;
59
+ } LibrarySymbol;
60
+
61
+ static VALUE library_initialize(VALUE self, VALUE libname, VALUE libflags);
62
+ static void library_free(Library* lib);
63
+
64
+
65
+ static VALUE symbol_allocate(VALUE klass);
66
+ static VALUE symbol_new(VALUE library, void* address, VALUE name);
67
+ static void symbol_mark(LibrarySymbol* sym);
68
+
69
+ static VALUE LibraryClass = Qnil, SymbolClass = Qnil;
70
+
71
+ #if (defined(_WIN32) || defined(__WIN32__)) && !defined(__CYGWIN__)
72
+ static void* dl_open(const char* name, int flags);
73
+ static void dl_error(char* buf, int size);
74
+ #define dl_sym(handle, name) GetProcAddress(handle, name)
75
+ #define dl_close(handle) FreeLibrary(handle)
76
+ enum { RTLD_LAZY=1, RTLD_NOW, RTLD_GLOBAL, RTLD_LOCAL };
77
+ #else
78
+ # define dl_open(name, flags) dlopen(name, flags != 0 ? flags : RTLD_LAZY)
79
+ # define dl_error(buf, size) do { snprintf(buf, size, "%s", dlerror()); } while(0)
80
+ # define dl_sym(handle, name) dlsym(handle, name)
81
+ # define dl_close(handle) dlclose(handle)
82
+ #ifndef RTLD_LOCAL
83
+ # define RTLD_LOCAL 8
84
+ #endif
85
+ #endif
86
+
87
+ static VALUE
88
+ library_allocate(VALUE klass)
89
+ {
90
+ Library* library;
91
+ return Data_Make_Struct(klass, Library, NULL, library_free, library);
92
+ }
93
+
94
+ /*
95
+ * call-seq: DynamicLibrary.open(libname, libflags)
96
+ * @param libname (see #initialize)
97
+ * @param libflags (see #initialize)
98
+ * @return [FFI::DynamicLibrary]
99
+ * @raise {LoadError} if +libname+ cannot be opened
100
+ * Open a library.
101
+ */
102
+ static VALUE
103
+ library_open(VALUE klass, VALUE libname, VALUE libflags)
104
+ {
105
+ return library_initialize(library_allocate(klass), libname, libflags);
106
+ }
107
+
108
+ /*
109
+ * call-seq: initialize(libname, libflags)
110
+ * @param [String] libname name of library to open
111
+ * @param [Fixnum] libflags flags for library to open
112
+ * @return [FFI::DynamicLibrary]
113
+ * @raise {LoadError} if +libname+ cannot be opened
114
+ * A new DynamicLibrary instance.
115
+ */
116
+ static VALUE
117
+ library_initialize(VALUE self, VALUE libname, VALUE libflags)
118
+ {
119
+ Library* library;
120
+ int flags;
121
+
122
+ Check_Type(libflags, T_FIXNUM);
123
+
124
+ Data_Get_Struct(self, Library, library);
125
+ flags = libflags != Qnil ? NUM2UINT(libflags) : 0;
126
+
127
+ library->handle = dl_open(libname != Qnil ? StringValueCStr(libname) : NULL, flags);
128
+ if (library->handle == NULL) {
129
+ char errmsg[1024];
130
+ dl_error(errmsg, sizeof(errmsg));
131
+ rb_raise(rb_eLoadError, "Could not open library '%s': %s",
132
+ libname != Qnil ? StringValueCStr(libname) : "[current process]",
133
+ errmsg);
134
+ }
135
+ #ifdef __CYGWIN__
136
+ // On Cygwin 1.7.17 "dlsym(dlopen(0,0), 'getpid')" fails. (dlerror: "No such process")
137
+ // As a workaround we can use "dlsym(RTLD_DEFAULT, 'getpid')" instead.
138
+ // Since 0 == RTLD_DEFAULT we won't call dl_close later.
139
+ if (libname == Qnil) {
140
+ dl_close(library->handle);
141
+ library->handle = RTLD_DEFAULT;
142
+ }
143
+ #endif
144
+ rb_iv_set(self, "@name", libname != Qnil ? libname : rb_str_new2("[current process]"));
145
+ return self;
146
+ }
147
+
148
+ static VALUE
149
+ library_dlsym(VALUE self, VALUE name)
150
+ {
151
+ Library* library;
152
+ void* address = NULL;
153
+ Check_Type(name, T_STRING);
154
+
155
+ Data_Get_Struct(self, Library, library);
156
+ address = dl_sym(library->handle, StringValueCStr(name));
157
+
158
+ return address != NULL ? symbol_new(self, address, name) : Qnil;
159
+ }
160
+
161
+ /*
162
+ * call-seq: last_error
163
+ * @return [String] library's last error string
164
+ */
165
+ static VALUE
166
+ library_dlerror(VALUE self)
167
+ {
168
+ char errmsg[1024];
169
+ dl_error(errmsg, sizeof(errmsg));
170
+ return rb_tainted_str_new2(errmsg);
171
+ }
172
+
173
+ static void
174
+ library_free(Library* library)
175
+ {
176
+ /* dlclose() on MacOS tends to segfault - avoid it */
177
+ #ifndef __APPLE__
178
+ if (library->handle != NULL) {
179
+ dl_close(library->handle);
180
+ }
181
+ #endif
182
+ xfree(library);
183
+ }
184
+
185
+ #if (defined(_WIN32) || defined(__WIN32__)) && !defined(__CYGWIN__)
186
+ static void*
187
+ dl_open(const char* name, int flags)
188
+ {
189
+ if (name == NULL) {
190
+ return GetModuleHandle(NULL);
191
+ } else {
192
+ return LoadLibraryExA(name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
193
+ }
194
+ }
195
+
196
+ static void
197
+ dl_error(char* buf, int size)
198
+ {
199
+ FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
200
+ 0, buf, size, NULL);
201
+ }
202
+ #endif
203
+
204
+ static VALUE
205
+ symbol_allocate(VALUE klass)
206
+ {
207
+ LibrarySymbol* sym;
208
+ VALUE obj = Data_Make_Struct(klass, LibrarySymbol, NULL, -1, sym);
209
+ sym->name = Qnil;
210
+ sym->library = Qnil;
211
+ sym->base.rbParent = Qnil;
212
+
213
+ return obj;
214
+ }
215
+
216
+
217
+ /*
218
+ * call-seq: initialize_copy(other)
219
+ * @param [Object] other
220
+ * @return [nil]
221
+ * DO NOT CALL THIS METHOD
222
+ */
223
+ static VALUE
224
+ symbol_initialize_copy(VALUE self, VALUE other)
225
+ {
226
+ rb_raise(rb_eRuntimeError, "cannot duplicate symbol");
227
+ return Qnil;
228
+ }
229
+
230
+ static VALUE
231
+ symbol_new(VALUE library, void* address, VALUE name)
232
+ {
233
+ LibrarySymbol* sym;
234
+ VALUE obj = Data_Make_Struct(SymbolClass, LibrarySymbol, symbol_mark, -1, sym);
235
+
236
+ sym->base.memory.address = address;
237
+ sym->base.memory.size = LONG_MAX;
238
+ sym->base.memory.typeSize = 1;
239
+ sym->base.memory.flags = MEM_RD | MEM_WR;
240
+ sym->library = library;
241
+ sym->name = name;
242
+
243
+ return obj;
244
+ }
245
+
246
+ static void
247
+ symbol_mark(LibrarySymbol* sym)
248
+ {
249
+ rb_gc_mark(sym->library);
250
+ rb_gc_mark(sym->name);
251
+ }
252
+
253
+ /*
254
+ * call-seq: inspect
255
+ * @return [String]
256
+ * Inspect.
257
+ */
258
+ static VALUE
259
+ symbol_inspect(VALUE self)
260
+ {
261
+ LibrarySymbol* sym;
262
+ char buf[256];
263
+
264
+ Data_Get_Struct(self, LibrarySymbol, sym);
265
+ snprintf(buf, sizeof(buf), "#<FFI::Library::Symbol name=%s address=%p>",
266
+ StringValueCStr(sym->name), sym->base.memory.address);
267
+ return rb_str_new2(buf);
268
+ }
269
+
270
+ void
271
+ rbffi_DynamicLibrary_Init(VALUE moduleFFI)
272
+ {
273
+ /*
274
+ * Document-class: FFI::DynamicLibrary
275
+ */
276
+ LibraryClass = rb_define_class_under(moduleFFI, "DynamicLibrary", rb_cObject);
277
+ rb_global_variable(&LibraryClass);
278
+ /*
279
+ * Document-class: FFI::DynamicLibrary::Symbol < FFI::Pointer
280
+ *
281
+ * An instance of this class represents a library symbol. It may be a {Pointer pointer} to
282
+ * a function or to a variable.
283
+ */
284
+ SymbolClass = rb_define_class_under(LibraryClass, "Symbol", rbffi_PointerClass);
285
+ rb_global_variable(&SymbolClass);
286
+
287
+ /*
288
+ * Document-const: FFI::NativeLibrary
289
+ * Backward compatibility for FFI::DynamicLibrary
290
+ */
291
+ rb_define_const(moduleFFI, "NativeLibrary", LibraryClass); /* backwards compat library */
292
+ rb_define_alloc_func(LibraryClass, library_allocate);
293
+ rb_define_singleton_method(LibraryClass, "open", library_open, 2);
294
+ rb_define_singleton_method(LibraryClass, "last_error", library_dlerror, 0);
295
+ rb_define_method(LibraryClass, "initialize", library_initialize, 2);
296
+ /*
297
+ * Document-method: find_symbol
298
+ * call-seq: find_symbol(name)
299
+ * @param [String] name library symbol's name
300
+ * @return [FFI::DynamicLibrary::Symbol] library symbol
301
+ */
302
+ rb_define_method(LibraryClass, "find_symbol", library_dlsym, 1);
303
+ /*
304
+ * Document-method: find_function
305
+ * call-seq: find_function(name)
306
+ * @param [String] name library function's name
307
+ * @return [FFI::DynamicLibrary::Symbol] library function symbol
308
+ */
309
+ rb_define_method(LibraryClass, "find_function", library_dlsym, 1);
310
+ /*
311
+ * Document-method: find_variable
312
+ * call-seq: find_variable(name)
313
+ * @param [String] name library variable's name
314
+ * @return [FFI::DynamicLibrary::Symbol] library variable symbol
315
+ */
316
+ rb_define_method(LibraryClass, "find_variable", library_dlsym, 1);
317
+ rb_define_method(LibraryClass, "last_error", library_dlerror, 0);
318
+ rb_define_attr(LibraryClass, "name", 1, 0);
319
+
320
+ rb_define_alloc_func(SymbolClass, symbol_allocate);
321
+ rb_undef_method(SymbolClass, "new");
322
+ rb_define_method(SymbolClass, "inspect", symbol_inspect, 0);
323
+ rb_define_method(SymbolClass, "initialize_copy", symbol_initialize_copy, 1);
324
+
325
+
326
+ #define DEF(x) rb_define_const(LibraryClass, "RTLD_" #x, UINT2NUM(RTLD_##x))
327
+ DEF(LAZY);
328
+ DEF(NOW);
329
+ DEF(GLOBAL);
330
+ DEF(LOCAL);
331
+
332
+ }
333
+
@@ -0,0 +1,49 @@
1
+ /*
2
+ * Copyright (c) 2008-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 _LIBRARY_H
30
+ #define _LIBRARY_H
31
+
32
+ #ifdef __cplusplus
33
+ extern "C" {
34
+ #endif
35
+
36
+
37
+
38
+ typedef struct Library {
39
+ void* handle;
40
+ } Library;
41
+
42
+ extern void rbffi_DynamicLibrary_Init(VALUE ffiModule);
43
+
44
+ #ifdef __cplusplus
45
+ }
46
+ #endif
47
+
48
+ #endif /* _LIBRARY_H */
49
+
@@ -0,0 +1,999 @@
1
+ /*
2
+ * Copyright (c) 2009-2011 Wayne Meissner
3
+ *
4
+ * Copyright (c) 2008-2013, Ruby FFI project contributors
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ * * Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * * Neither the name of the Ruby FFI project nor the
15
+ * names of its contributors may be used to endorse or promote products
16
+ * derived from this software without specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
30
+ #ifndef _MSC_VER
31
+ #include <sys/param.h>
32
+ #endif
33
+ #include <sys/types.h>
34
+ #ifndef _WIN32
35
+ # include <sys/mman.h>
36
+ # include <unistd.h>
37
+ #endif
38
+
39
+ #include <stdio.h>
40
+ #ifndef _MSC_VER
41
+ # include <stdint.h>
42
+ # include <stdbool.h>
43
+ #else
44
+ # include "win32/stdbool.h"
45
+ # if !defined(INT8_MIN)
46
+ # include "win32/stdint.h"
47
+ # endif
48
+ #endif
49
+ #include <ruby.h>
50
+
51
+ #include <ffi.h>
52
+ #if defined(HAVE_NATIVETHREAD) && !defined(_WIN32)
53
+ #include <pthread.h>
54
+ #endif
55
+ #include <fcntl.h>
56
+
57
+ #include "rbffi.h"
58
+ #include "compat.h"
59
+
60
+ #include "AbstractMemory.h"
61
+ #include "Pointer.h"
62
+ #include "Struct.h"
63
+ #include "Platform.h"
64
+ #include "Type.h"
65
+ #include "LastError.h"
66
+ #include "Call.h"
67
+ #include "ClosurePool.h"
68
+ #include "MappedType.h"
69
+ #include "Thread.h"
70
+ #include "LongDouble.h"
71
+ #include "MethodHandle.h"
72
+ #include "Function.h"
73
+
74
+ typedef struct Function_ {
75
+ Pointer base;
76
+ FunctionType* info;
77
+ MethodHandle* methodHandle;
78
+ bool autorelease;
79
+ Closure* closure;
80
+ VALUE rbProc;
81
+ VALUE rbFunctionInfo;
82
+ } Function;
83
+
84
+ static void function_mark(Function *);
85
+ static void function_free(Function *);
86
+ static VALUE function_init(VALUE self, VALUE rbFunctionInfo, VALUE rbProc);
87
+ static void callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data);
88
+ static bool callback_prep(void* ctx, void* code, Closure* closure, char* errmsg, size_t errmsgsize);
89
+ static VALUE callback_with_gvl(void* data);
90
+ static VALUE invoke_callback(void* data);
91
+ static VALUE save_callback_exception(void* data, VALUE exc);
92
+
93
+ #define DEFER_ASYNC_CALLBACK 1
94
+
95
+
96
+ #if defined(DEFER_ASYNC_CALLBACK)
97
+ static VALUE async_cb_event(void *);
98
+ static VALUE async_cb_call(void *);
99
+ #endif
100
+
101
+ #ifdef HAVE_RB_THREAD_CALL_WITH_GVL
102
+ extern void *rb_thread_call_with_gvl(void *(*func)(void *), void *data1);
103
+ #endif
104
+
105
+ VALUE rbffi_FunctionClass = Qnil;
106
+
107
+ #if defined(DEFER_ASYNC_CALLBACK)
108
+ static VALUE async_cb_thread = Qnil;
109
+ #endif
110
+
111
+ static ID id_call = 0, id_to_native = 0, id_from_native = 0, id_cbtable = 0, id_cb_ref = 0;
112
+
113
+ struct gvl_callback {
114
+ Closure* closure;
115
+ void* retval;
116
+ void** parameters;
117
+ bool done;
118
+ rbffi_frame_t *frame;
119
+ #if defined(DEFER_ASYNC_CALLBACK)
120
+ struct gvl_callback* next;
121
+ # ifndef _WIN32
122
+ pthread_cond_t async_cond;
123
+ pthread_mutex_t async_mutex;
124
+ # else
125
+ HANDLE async_event;
126
+ # endif
127
+ #endif
128
+ };
129
+
130
+
131
+ #if defined(DEFER_ASYNC_CALLBACK)
132
+ static struct gvl_callback* async_cb_list = NULL;
133
+ # ifndef _WIN32
134
+ static pthread_mutex_t async_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
135
+ static pthread_cond_t async_cb_cond = PTHREAD_COND_INITIALIZER;
136
+ # if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
137
+ static int async_cb_pipe[2];
138
+ # endif
139
+ # else
140
+ static HANDLE async_cb_cond;
141
+ static CRITICAL_SECTION async_cb_lock;
142
+ # if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
143
+ static int async_cb_pipe[2];
144
+ # endif
145
+ # endif
146
+ #endif
147
+
148
+
149
+ static VALUE
150
+ function_allocate(VALUE klass)
151
+ {
152
+ Function *fn;
153
+ VALUE obj;
154
+
155
+ obj = Data_Make_Struct(klass, Function, function_mark, function_free, fn);
156
+
157
+ fn->base.memory.flags = MEM_RD;
158
+ fn->base.rbParent = Qnil;
159
+ fn->rbProc = Qnil;
160
+ fn->rbFunctionInfo = Qnil;
161
+ fn->autorelease = true;
162
+
163
+ return obj;
164
+ }
165
+
166
+ static void
167
+ function_mark(Function *fn)
168
+ {
169
+ rb_gc_mark(fn->base.rbParent);
170
+ rb_gc_mark(fn->rbProc);
171
+ rb_gc_mark(fn->rbFunctionInfo);
172
+ }
173
+
174
+ static void
175
+ function_free(Function *fn)
176
+ {
177
+ if (fn->methodHandle != NULL) {
178
+ rbffi_MethodHandle_Free(fn->methodHandle);
179
+ }
180
+
181
+ if (fn->closure != NULL && fn->autorelease) {
182
+ rbffi_Closure_Free(fn->closure);
183
+ }
184
+
185
+ xfree(fn);
186
+ }
187
+
188
+ /*
189
+ * @param [Type, Symbol] return_type return type for the function
190
+ * @param [Array<Type, Symbol>] param_types array of parameters types
191
+ * @param [Hash] options see {FFI::FunctionType} for available options
192
+ * @return [self]
193
+ * A new Function instance.
194
+ *
195
+ * Define a function from a Proc or a block.
196
+ *
197
+ * @overload initialize(return_type, param_types, options = {}) { |i| ... }
198
+ * @yieldparam i parameters for the function
199
+ * @overload initialize(return_type, param_types, proc, options = {})
200
+ * @param [Proc] proc
201
+ */
202
+ static VALUE
203
+ function_initialize(int argc, VALUE* argv, VALUE self)
204
+ {
205
+
206
+ VALUE rbReturnType = Qnil, rbParamTypes = Qnil, rbProc = Qnil, rbOptions = Qnil;
207
+ VALUE rbFunctionInfo = Qnil;
208
+ VALUE infoArgv[3];
209
+ int nargs;
210
+
211
+ nargs = rb_scan_args(argc, argv, "22", &rbReturnType, &rbParamTypes, &rbProc, &rbOptions);
212
+
213
+ /*
214
+ * Callback with block,
215
+ * e.g. Function.new(:int, [ :int ]) { |i| blah }
216
+ * or Function.new(:int, [ :int ], { :convention => :stdcall }) { |i| blah }
217
+ */
218
+ if (rb_block_given_p()) {
219
+ if (nargs > 3) {
220
+ rb_raise(rb_eArgError, "cannot create function with both proc/address and block");
221
+ }
222
+ rbOptions = rbProc;
223
+ rbProc = rb_block_proc();
224
+ } else {
225
+ /* Callback with proc, or Function with address
226
+ * e.g. Function.new(:int, [ :int ], Proc.new { |i| })
227
+ * Function.new(:int, [ :int ], Proc.new { |i| }, { :convention => :stdcall })
228
+ * Function.new(:int, [ :int ], addr)
229
+ * Function.new(:int, [ :int ], addr, { :convention => :stdcall })
230
+ */
231
+ }
232
+
233
+ infoArgv[0] = rbReturnType;
234
+ infoArgv[1] = rbParamTypes;
235
+ infoArgv[2] = rbOptions;
236
+ rbFunctionInfo = rb_class_new_instance(rbOptions != Qnil ? 3 : 2, infoArgv, rbffi_FunctionTypeClass);
237
+
238
+ function_init(self, rbFunctionInfo, rbProc);
239
+
240
+ return self;
241
+ }
242
+
243
+ /*
244
+ * call-seq: initialize_copy(other)
245
+ * @return [nil]
246
+ * DO NOT CALL THIS METHOD
247
+ */
248
+ static VALUE
249
+ function_initialize_copy(VALUE self, VALUE other)
250
+ {
251
+ rb_raise(rb_eRuntimeError, "cannot duplicate function instances");
252
+ return Qnil;
253
+ }
254
+
255
+ VALUE
256
+ rbffi_Function_NewInstance(VALUE rbFunctionInfo, VALUE rbProc)
257
+ {
258
+ return function_init(function_allocate(rbffi_FunctionClass), rbFunctionInfo, rbProc);
259
+ }
260
+
261
+ VALUE
262
+ rbffi_Function_ForProc(VALUE rbFunctionInfo, VALUE proc)
263
+ {
264
+ VALUE callback, cbref, cbTable;
265
+ Function* fp;
266
+
267
+ cbref = RTEST(rb_ivar_defined(proc, id_cb_ref)) ? rb_ivar_get(proc, id_cb_ref) : Qnil;
268
+ /* If the first callback reference has the same function function signature, use it */
269
+ if (cbref != Qnil && CLASS_OF(cbref) == rbffi_FunctionClass) {
270
+ Data_Get_Struct(cbref, Function, fp);
271
+ if (fp->rbFunctionInfo == rbFunctionInfo) {
272
+ return cbref;
273
+ }
274
+ }
275
+
276
+ cbTable = RTEST(rb_ivar_defined(proc, id_cbtable)) ? rb_ivar_get(proc, id_cbtable) : Qnil;
277
+ if (cbTable != Qnil && (callback = rb_hash_aref(cbTable, rbFunctionInfo)) != Qnil) {
278
+ return callback;
279
+ }
280
+
281
+ /* No existing function for the proc with that signature, create a new one and cache it */
282
+ callback = rbffi_Function_NewInstance(rbFunctionInfo, proc);
283
+ if (cbref == Qnil) {
284
+ /* If there is no other cb already cached for this proc, we can use the ivar slot */
285
+ rb_ivar_set(proc, id_cb_ref, callback);
286
+ } else {
287
+ /* The proc instance has been used as more than one type of callback, store extras in a hash */
288
+ cbTable = rb_hash_new();
289
+ rb_ivar_set(proc, id_cbtable, cbTable);
290
+ rb_hash_aset(cbTable, rbFunctionInfo, callback);
291
+ }
292
+
293
+ return callback;
294
+ }
295
+
296
+ static VALUE
297
+ function_init(VALUE self, VALUE rbFunctionInfo, VALUE rbProc)
298
+ {
299
+ Function* fn = NULL;
300
+
301
+ Data_Get_Struct(self, Function, fn);
302
+
303
+ fn->rbFunctionInfo = rbFunctionInfo;
304
+
305
+ Data_Get_Struct(fn->rbFunctionInfo, FunctionType, fn->info);
306
+
307
+ if (rb_obj_is_kind_of(rbProc, rbffi_PointerClass)) {
308
+ Pointer* orig;
309
+ Data_Get_Struct(rbProc, Pointer, orig);
310
+ fn->base.memory = orig->memory;
311
+ fn->base.rbParent = rbProc;
312
+
313
+ } else if (rb_obj_is_kind_of(rbProc, rb_cProc) || rb_respond_to(rbProc, id_call)) {
314
+ if (fn->info->closurePool == NULL) {
315
+ fn->info->closurePool = rbffi_ClosurePool_New(sizeof(ffi_closure), callback_prep, fn->info);
316
+ if (fn->info->closurePool == NULL) {
317
+ rb_raise(rb_eNoMemError, "failed to create closure pool");
318
+ }
319
+ }
320
+
321
+ #if defined(DEFER_ASYNC_CALLBACK)
322
+ if (async_cb_thread == Qnil) {
323
+ #if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)) && defined(_WIN32)
324
+ _pipe(async_cb_pipe, 1024, O_BINARY);
325
+ #elif !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
326
+ pipe(async_cb_pipe);
327
+ fcntl(async_cb_pipe[0], F_SETFL, fcntl(async_cb_pipe[0], F_GETFL) | O_NONBLOCK);
328
+ fcntl(async_cb_pipe[1], F_SETFL, fcntl(async_cb_pipe[1], F_GETFL) | O_NONBLOCK);
329
+ #endif
330
+ async_cb_thread = rb_thread_create(async_cb_event, NULL);
331
+ }
332
+
333
+ #endif
334
+
335
+ fn->closure = rbffi_Closure_Alloc(fn->info->closurePool);
336
+ fn->closure->info = fn;
337
+ fn->base.memory.address = fn->closure->code;
338
+ fn->base.memory.size = sizeof(*fn->closure);
339
+ fn->autorelease = true;
340
+
341
+ } else {
342
+ rb_raise(rb_eTypeError, "wrong argument type %s, expected pointer or proc",
343
+ rb_obj_classname(rbProc));
344
+ }
345
+
346
+ fn->rbProc = rbProc;
347
+
348
+ return self;
349
+ }
350
+
351
+ /*
352
+ * call-seq: call(*args)
353
+ * @param [Array] args function arguments
354
+ * @return [FFI::Type]
355
+ * Call the function
356
+ */
357
+ static VALUE
358
+ function_call(int argc, VALUE* argv, VALUE self)
359
+ {
360
+ Function* fn;
361
+
362
+ Data_Get_Struct(self, Function, fn);
363
+
364
+ return (*fn->info->invoke)(argc, argv, fn->base.memory.address, fn->info);
365
+ }
366
+
367
+ /*
368
+ * call-seq: attach(m, name)
369
+ * @param [Module] m
370
+ * @param [String] name
371
+ * @return [self]
372
+ * Attach a Function to the Module +m+ as +name+.
373
+ */
374
+ static VALUE
375
+ function_attach(VALUE self, VALUE module, VALUE name)
376
+ {
377
+ Function* fn;
378
+ char var[1024];
379
+
380
+ Data_Get_Struct(self, Function, fn);
381
+
382
+ if (fn->info->parameterCount == -1) {
383
+ rb_raise(rb_eRuntimeError, "cannot attach variadic functions");
384
+ return Qnil;
385
+ }
386
+
387
+ if (!rb_obj_is_kind_of(module, rb_cModule)) {
388
+ rb_raise(rb_eRuntimeError, "trying to attach function to non-module");
389
+ return Qnil;
390
+ }
391
+
392
+ if (fn->methodHandle == NULL) {
393
+ fn->methodHandle = rbffi_MethodHandle_Alloc(fn->info, fn->base.memory.address);
394
+ }
395
+
396
+ /*
397
+ * Stash the Function in a module variable so it does not get garbage collected
398
+ */
399
+ snprintf(var, sizeof(var), "@@%s", StringValueCStr(name));
400
+ rb_cv_set(module, var, self);
401
+
402
+ rb_define_singleton_method(module, StringValueCStr(name),
403
+ rbffi_MethodHandle_CodeAddress(fn->methodHandle), -1);
404
+
405
+
406
+ rb_define_method(module, StringValueCStr(name),
407
+ rbffi_MethodHandle_CodeAddress(fn->methodHandle), -1);
408
+
409
+ return self;
410
+ }
411
+
412
+ /*
413
+ * call-seq: autorelease = autorelease
414
+ * @param [Boolean] autorelease
415
+ * @return [self]
416
+ * Set +autorelease+ attribute (See {Pointer}).
417
+ */
418
+ static VALUE
419
+ function_set_autorelease(VALUE self, VALUE autorelease)
420
+ {
421
+ Function* fn;
422
+
423
+ Data_Get_Struct(self, Function, fn);
424
+
425
+ fn->autorelease = RTEST(autorelease);
426
+
427
+ return self;
428
+ }
429
+
430
+ static VALUE
431
+ function_autorelease_p(VALUE self)
432
+ {
433
+ Function* fn;
434
+
435
+ Data_Get_Struct(self, Function, fn);
436
+
437
+ return fn->autorelease ? Qtrue : Qfalse;
438
+ }
439
+
440
+ /*
441
+ * call-seq: free
442
+ * @return [self]
443
+ * Free memory allocated by Function.
444
+ */
445
+ static VALUE
446
+ function_release(VALUE self)
447
+ {
448
+ Function* fn;
449
+
450
+ Data_Get_Struct(self, Function, fn);
451
+
452
+ if (fn->closure == NULL) {
453
+ rb_raise(rb_eRuntimeError, "cannot free function which was not allocated");
454
+ }
455
+
456
+ rbffi_Closure_Free(fn->closure);
457
+ fn->closure = NULL;
458
+
459
+ return self;
460
+ }
461
+
462
+ static void
463
+ callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
464
+ {
465
+ struct gvl_callback cb = { 0 };
466
+
467
+ cb.closure = (Closure *) user_data;
468
+ cb.retval = retval;
469
+ cb.parameters = parameters;
470
+ cb.done = false;
471
+ cb.frame = rbffi_frame_current();
472
+
473
+ if (cb.frame != NULL) cb.frame->exc = Qnil;
474
+ if (cb.frame != NULL && cb.frame->has_gvl) {
475
+ callback_with_gvl(&cb);
476
+
477
+ #if defined(HAVE_RB_THREAD_CALL_WITH_GVL)
478
+ } else if (cb.frame != NULL) {
479
+ rb_thread_call_with_gvl(callback_with_gvl, &cb);
480
+ #endif
481
+ #if defined(DEFER_ASYNC_CALLBACK) && !defined(_WIN32)
482
+ } else {
483
+ bool empty = false;
484
+
485
+ pthread_mutex_init(&cb.async_mutex, NULL);
486
+ pthread_cond_init(&cb.async_cond, NULL);
487
+
488
+ /* Now signal the async callback thread */
489
+ pthread_mutex_lock(&async_cb_mutex);
490
+ empty = async_cb_list == NULL;
491
+ cb.next = async_cb_list;
492
+ async_cb_list = &cb;
493
+
494
+ #if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
495
+ pthread_mutex_unlock(&async_cb_mutex);
496
+ /* Only signal if the list was empty */
497
+ if (empty) {
498
+ char c;
499
+ write(async_cb_pipe[1], &c, 1);
500
+ }
501
+ #else
502
+ pthread_cond_signal(&async_cb_cond);
503
+ pthread_mutex_unlock(&async_cb_mutex);
504
+ #endif
505
+
506
+ /* Wait for the thread executing the ruby callback to signal it is done */
507
+ pthread_mutex_lock(&cb.async_mutex);
508
+ while (!cb.done) {
509
+ pthread_cond_wait(&cb.async_cond, &cb.async_mutex);
510
+ }
511
+ pthread_mutex_unlock(&cb.async_mutex);
512
+ pthread_cond_destroy(&cb.async_cond);
513
+ pthread_mutex_destroy(&cb.async_mutex);
514
+
515
+ #elif defined(DEFER_ASYNC_CALLBACK) && defined(_WIN32)
516
+ } else {
517
+ bool empty = false;
518
+
519
+ cb.async_event = CreateEvent(NULL, FALSE, FALSE, NULL);
520
+
521
+ /* Now signal the async callback thread */
522
+ EnterCriticalSection(&async_cb_lock);
523
+ empty = async_cb_list == NULL;
524
+ cb.next = async_cb_list;
525
+ async_cb_list = &cb;
526
+ LeaveCriticalSection(&async_cb_lock);
527
+
528
+ #if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
529
+ /* Only signal if the list was empty */
530
+ if (empty) {
531
+ char c;
532
+ write(async_cb_pipe[1], &c, 1);
533
+ }
534
+ #else
535
+ SetEvent(async_cb_cond);
536
+ #endif
537
+
538
+ /* Wait for the thread executing the ruby callback to signal it is done */
539
+ WaitForSingleObject(cb.async_event, INFINITE);
540
+ CloseHandle(cb.async_event);
541
+ #endif
542
+ }
543
+ }
544
+
545
+ #if defined(DEFER_ASYNC_CALLBACK)
546
+ struct async_wait {
547
+ void* cb;
548
+ bool stop;
549
+ };
550
+
551
+ static VALUE async_cb_wait(void *);
552
+ static void async_cb_stop(void *);
553
+
554
+ #if defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
555
+ static VALUE
556
+ async_cb_event(void* unused)
557
+ {
558
+ struct async_wait w = { 0 };
559
+
560
+ w.stop = false;
561
+ while (!w.stop) {
562
+ #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
563
+ rb_thread_call_without_gvl(async_cb_wait, &w, async_cb_stop, &w);
564
+ #else
565
+ rb_thread_blocking_region(async_cb_wait, &w, async_cb_stop, &w);
566
+ #endif
567
+ if (w.cb != NULL) {
568
+ /* Start up a new ruby thread to run the ruby callback */
569
+ rb_thread_create(async_cb_call, w.cb);
570
+ }
571
+ }
572
+
573
+ return Qnil;
574
+ }
575
+
576
+ #elif defined(_WIN32)
577
+ static VALUE
578
+ async_cb_event(void* unused)
579
+ {
580
+ while (true) {
581
+ struct gvl_callback* cb;
582
+ char buf[64];
583
+ fd_set rfds;
584
+
585
+ FD_ZERO(&rfds);
586
+ FD_SET(async_cb_pipe[0], &rfds);
587
+ rb_thread_select(async_cb_pipe[0] + 1, &rfds, NULL, NULL, NULL);
588
+ read(async_cb_pipe[0], buf, sizeof(buf));
589
+
590
+ EnterCriticalSection(&async_cb_lock);
591
+ cb = async_cb_list;
592
+ async_cb_list = NULL;
593
+ LeaveCriticalSection(&async_cb_lock);
594
+
595
+ while (cb != NULL) {
596
+ struct gvl_callback* next = cb->next;
597
+ /* Start up a new ruby thread to run the ruby callback */
598
+ rb_thread_create(async_cb_call, cb);
599
+ cb = next;
600
+ }
601
+ }
602
+
603
+ return Qnil;
604
+ }
605
+ #else
606
+ static VALUE
607
+ async_cb_event(void* unused)
608
+ {
609
+ while (true) {
610
+ struct gvl_callback* cb;
611
+ char buf[64];
612
+
613
+ if (read(async_cb_pipe[0], buf, sizeof(buf)) < 0) {
614
+ rb_thread_wait_fd(async_cb_pipe[0]);
615
+ while (read(async_cb_pipe[0], buf, sizeof (buf)) < 0) {
616
+ if (rb_io_wait_readable(async_cb_pipe[0]) != Qtrue) {
617
+ return Qfalse;
618
+ }
619
+ }
620
+ }
621
+
622
+ pthread_mutex_lock(&async_cb_mutex);
623
+ cb = async_cb_list;
624
+ async_cb_list = NULL;
625
+ pthread_mutex_unlock(&async_cb_mutex);
626
+
627
+ while (cb != NULL) {
628
+ struct gvl_callback* next = cb->next;
629
+ /* Start up a new ruby thread to run the ruby callback */
630
+ rb_thread_create(async_cb_call, cb);
631
+ cb = next;
632
+ }
633
+ }
634
+
635
+ return Qnil;
636
+ }
637
+ #endif
638
+
639
+ #ifdef _WIN32
640
+ static VALUE
641
+ async_cb_wait(void *data)
642
+ {
643
+ struct async_wait* w = (struct async_wait *) data;
644
+
645
+ w->cb = NULL;
646
+
647
+ EnterCriticalSection(&async_cb_lock);
648
+
649
+ while (!w->stop && async_cb_list == NULL) {
650
+ LeaveCriticalSection(&async_cb_lock);
651
+ WaitForSingleObject(async_cb_cond, INFINITE);
652
+ EnterCriticalSection(&async_cb_lock);
653
+ }
654
+
655
+ if (async_cb_list != NULL) {
656
+ w->cb = async_cb_list;
657
+ async_cb_list = async_cb_list->next;
658
+ }
659
+
660
+ LeaveCriticalSection(&async_cb_lock);
661
+
662
+ return Qnil;
663
+ }
664
+
665
+ static void
666
+ async_cb_stop(void *data)
667
+ {
668
+ struct async_wait* w = (struct async_wait *) data;
669
+
670
+ EnterCriticalSection(&async_cb_lock);
671
+ w->stop = true;
672
+ LeaveCriticalSection(&async_cb_lock);
673
+ SetEvent(async_cb_cond);
674
+ }
675
+
676
+ #else
677
+ static VALUE
678
+ async_cb_wait(void *data)
679
+ {
680
+ struct async_wait* w = (struct async_wait *) data;
681
+
682
+ w->cb = NULL;
683
+
684
+ pthread_mutex_lock(&async_cb_mutex);
685
+
686
+ while (!w->stop && async_cb_list == NULL) {
687
+ pthread_cond_wait(&async_cb_cond, &async_cb_mutex);
688
+ }
689
+
690
+ if (async_cb_list != NULL) {
691
+ w->cb = async_cb_list;
692
+ async_cb_list = async_cb_list->next;
693
+ }
694
+
695
+ pthread_mutex_unlock(&async_cb_mutex);
696
+
697
+ return Qnil;
698
+ }
699
+
700
+ static void
701
+ async_cb_stop(void *data)
702
+ {
703
+ struct async_wait* w = (struct async_wait *) data;
704
+
705
+ pthread_mutex_lock(&async_cb_mutex);
706
+ w->stop = true;
707
+ pthread_cond_signal(&async_cb_cond);
708
+ pthread_mutex_unlock(&async_cb_mutex);
709
+ }
710
+ #endif
711
+
712
+ static VALUE
713
+ async_cb_call(void *data)
714
+ {
715
+ struct gvl_callback* cb = (struct gvl_callback *) data;
716
+
717
+ callback_with_gvl(data);
718
+
719
+ /* Signal the original native thread that the ruby code has completed */
720
+ #ifdef _WIN32
721
+ SetEvent(cb->async_event);
722
+ #else
723
+ pthread_mutex_lock(&cb->async_mutex);
724
+ cb->done = true;
725
+ pthread_cond_signal(&cb->async_cond);
726
+ pthread_mutex_unlock(&cb->async_mutex);
727
+ #endif
728
+
729
+ return Qnil;
730
+ }
731
+
732
+ #endif
733
+
734
+ static VALUE
735
+ callback_with_gvl(void* data)
736
+ {
737
+ rb_rescue2(invoke_callback, (VALUE) data, save_callback_exception, (VALUE) data, rb_eException, (VALUE) 0);
738
+ }
739
+
740
+ static VALUE
741
+ invoke_callback(void* data)
742
+ {
743
+ struct gvl_callback* cb = (struct gvl_callback *) data;
744
+
745
+ Function* fn = (Function *) cb->closure->info;
746
+ FunctionType *cbInfo = fn->info;
747
+ Type* returnType = cbInfo->returnType;
748
+ void* retval = cb->retval;
749
+ void** parameters = cb->parameters;
750
+ VALUE* rbParams;
751
+ VALUE rbReturnType = cbInfo->rbReturnType;
752
+ VALUE rbReturnValue;
753
+ int i;
754
+
755
+ rbParams = ALLOCA_N(VALUE, cbInfo->parameterCount);
756
+ for (i = 0; i < cbInfo->parameterCount; ++i) {
757
+ VALUE param;
758
+ Type* paramType = cbInfo->parameterTypes[i];
759
+ VALUE rbParamType = rb_ary_entry(cbInfo->rbParameterTypes, i);
760
+
761
+ if (unlikely(paramType->nativeType == NATIVE_MAPPED)) {
762
+ paramType = ((MappedType *) paramType)->type;
763
+ rbParamType = ((MappedType *) paramType)->rbType;
764
+ }
765
+
766
+ switch (paramType->nativeType) {
767
+ case NATIVE_INT8:
768
+ param = INT2NUM(*(int8_t *) parameters[i]);
769
+ break;
770
+ case NATIVE_UINT8:
771
+ param = UINT2NUM(*(uint8_t *) parameters[i]);
772
+ break;
773
+ case NATIVE_INT16:
774
+ param = INT2NUM(*(int16_t *) parameters[i]);
775
+ break;
776
+ case NATIVE_UINT16:
777
+ param = UINT2NUM(*(uint16_t *) parameters[i]);
778
+ break;
779
+ case NATIVE_INT32:
780
+ param = INT2NUM(*(int32_t *) parameters[i]);
781
+ break;
782
+ case NATIVE_UINT32:
783
+ param = UINT2NUM(*(uint32_t *) parameters[i]);
784
+ break;
785
+ case NATIVE_INT64:
786
+ param = LL2NUM(*(int64_t *) parameters[i]);
787
+ break;
788
+ case NATIVE_UINT64:
789
+ param = ULL2NUM(*(uint64_t *) parameters[i]);
790
+ break;
791
+ case NATIVE_LONG:
792
+ param = LONG2NUM(*(long *) parameters[i]);
793
+ break;
794
+ case NATIVE_ULONG:
795
+ param = ULONG2NUM(*(unsigned long *) parameters[i]);
796
+ break;
797
+ case NATIVE_FLOAT32:
798
+ param = rb_float_new(*(float *) parameters[i]);
799
+ break;
800
+ case NATIVE_FLOAT64:
801
+ param = rb_float_new(*(double *) parameters[i]);
802
+ break;
803
+ case NATIVE_LONGDOUBLE:
804
+ param = rbffi_longdouble_new(*(long double *) parameters[i]);
805
+ break;
806
+ case NATIVE_STRING:
807
+ param = (*(void **) parameters[i] != NULL) ? rb_tainted_str_new2(*(char **) parameters[i]) : Qnil;
808
+ break;
809
+ case NATIVE_POINTER:
810
+ param = rbffi_Pointer_NewInstance(*(void **) parameters[i]);
811
+ break;
812
+ case NATIVE_BOOL:
813
+ param = (*(uint8_t *) parameters[i]) ? Qtrue : Qfalse;
814
+ break;
815
+
816
+ case NATIVE_FUNCTION:
817
+ case NATIVE_CALLBACK:
818
+ case NATIVE_STRUCT:
819
+ param = rbffi_NativeValue_ToRuby(paramType, rbParamType, parameters[i]);
820
+ break;
821
+
822
+ default:
823
+ param = Qnil;
824
+ break;
825
+ }
826
+
827
+ /* Convert the native value into a custom ruby value */
828
+ if (unlikely(cbInfo->parameterTypes[i]->nativeType == NATIVE_MAPPED)) {
829
+ VALUE values[] = { param, Qnil };
830
+ param = rb_funcall2(((MappedType *) cbInfo->parameterTypes[i])->rbConverter, id_from_native, 2, values);
831
+ }
832
+
833
+ rbParams[i] = param;
834
+ }
835
+
836
+ rbReturnValue = rb_funcall2(fn->rbProc, id_call, cbInfo->parameterCount, rbParams);
837
+ RB_GC_GUARD_PTR(rbParams);
838
+
839
+ if (unlikely(returnType->nativeType == NATIVE_MAPPED)) {
840
+ VALUE values[] = { rbReturnValue, Qnil };
841
+ rbReturnValue = rb_funcall2(((MappedType *) returnType)->rbConverter, id_to_native, 2, values);
842
+ rbReturnType = ((MappedType *) returnType)->rbType;
843
+ returnType = ((MappedType* ) returnType)->type;
844
+ }
845
+
846
+ if (rbReturnValue == Qnil || TYPE(rbReturnValue) == T_NIL) {
847
+ memset(retval, 0, returnType->ffiType->size);
848
+ } else switch (returnType->nativeType) {
849
+ case NATIVE_INT8:
850
+ case NATIVE_INT16:
851
+ case NATIVE_INT32:
852
+ *((ffi_sarg *) retval) = NUM2INT(rbReturnValue);
853
+ break;
854
+ case NATIVE_UINT8:
855
+ case NATIVE_UINT16:
856
+ case NATIVE_UINT32:
857
+ *((ffi_arg *) retval) = NUM2UINT(rbReturnValue);
858
+ break;
859
+ case NATIVE_INT64:
860
+ *((int64_t *) retval) = NUM2LL(rbReturnValue);
861
+ break;
862
+ case NATIVE_UINT64:
863
+ *((uint64_t *) retval) = NUM2ULL(rbReturnValue);
864
+ break;
865
+ case NATIVE_LONG:
866
+ *((ffi_sarg *) retval) = NUM2LONG(rbReturnValue);
867
+ break;
868
+ case NATIVE_ULONG:
869
+ *((ffi_arg *) retval) = NUM2ULONG(rbReturnValue);
870
+ break;
871
+ case NATIVE_FLOAT32:
872
+ *((float *) retval) = (float) NUM2DBL(rbReturnValue);
873
+ break;
874
+ case NATIVE_FLOAT64:
875
+ *((double *) retval) = NUM2DBL(rbReturnValue);
876
+ break;
877
+ case NATIVE_POINTER:
878
+ if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) {
879
+ *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address;
880
+ } else {
881
+ /* Default to returning NULL if not a value pointer object. handles nil case as well */
882
+ *((void **) retval) = NULL;
883
+ }
884
+ break;
885
+
886
+ case NATIVE_BOOL:
887
+ *((ffi_arg *) retval) = rbReturnValue == Qtrue;
888
+ break;
889
+
890
+ case NATIVE_FUNCTION:
891
+ case NATIVE_CALLBACK:
892
+ if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) {
893
+
894
+ *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address;
895
+
896
+ } else if (rb_obj_is_kind_of(rbReturnValue, rb_cProc) || rb_respond_to(rbReturnValue, id_call)) {
897
+ VALUE function;
898
+
899
+ function = rbffi_Function_ForProc(rbReturnType, rbReturnValue);
900
+
901
+ *((void **) retval) = ((AbstractMemory *) DATA_PTR(function))->address;
902
+ } else {
903
+ *((void **) retval) = NULL;
904
+ }
905
+ break;
906
+
907
+ case NATIVE_STRUCT:
908
+ if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_StructClass)) {
909
+ AbstractMemory* memory = ((Struct *) DATA_PTR(rbReturnValue))->pointer;
910
+
911
+ if (memory->address != NULL) {
912
+ memcpy(retval, memory->address, returnType->ffiType->size);
913
+
914
+ } else {
915
+ memset(retval, 0, returnType->ffiType->size);
916
+ }
917
+
918
+ } else {
919
+ memset(retval, 0, returnType->ffiType->size);
920
+ }
921
+ break;
922
+
923
+ default:
924
+ *((ffi_arg *) retval) = 0;
925
+ break;
926
+ }
927
+
928
+ return Qnil;
929
+ }
930
+
931
+ static VALUE
932
+ save_callback_exception(void* data, VALUE exc)
933
+ {
934
+ struct gvl_callback* cb = (struct gvl_callback *) data;
935
+
936
+ memset(cb->retval, 0, ((Function *) cb->closure->info)->info->returnType->ffiType->size);
937
+ if (cb->frame != NULL) cb->frame->exc = exc;
938
+
939
+ return Qnil;
940
+ }
941
+
942
+ static bool
943
+ callback_prep(void* ctx, void* code, Closure* closure, char* errmsg, size_t errmsgsize)
944
+ {
945
+ FunctionType* fnInfo = (FunctionType *) ctx;
946
+ ffi_status ffiStatus;
947
+
948
+ ffiStatus = ffi_prep_closure(code, &fnInfo->ffi_cif, callback_invoke, closure);
949
+ if (ffiStatus != FFI_OK) {
950
+ snprintf(errmsg, errmsgsize, "ffi_prep_closure failed. status=%#x", ffiStatus);
951
+ return false;
952
+ }
953
+
954
+ return true;
955
+ }
956
+
957
+ void
958
+ rbffi_Function_Init(VALUE moduleFFI)
959
+ {
960
+ rbffi_FunctionInfo_Init(moduleFFI);
961
+ /*
962
+ * Document-class: FFI::Function < FFI::Pointer
963
+ */
964
+ rbffi_FunctionClass = rb_define_class_under(moduleFFI, "Function", rbffi_PointerClass);
965
+
966
+ rb_global_variable(&rbffi_FunctionClass);
967
+ rb_define_alloc_func(rbffi_FunctionClass, function_allocate);
968
+
969
+ rb_define_method(rbffi_FunctionClass, "initialize", function_initialize, -1);
970
+ rb_define_method(rbffi_FunctionClass, "initialize_copy", function_initialize_copy, 1);
971
+ rb_define_method(rbffi_FunctionClass, "call", function_call, -1);
972
+ rb_define_method(rbffi_FunctionClass, "attach", function_attach, 2);
973
+ rb_define_method(rbffi_FunctionClass, "free", function_release, 0);
974
+ rb_define_method(rbffi_FunctionClass, "autorelease=", function_set_autorelease, 1);
975
+ /*
976
+ * call-seq: autorelease
977
+ * @return [Boolean]
978
+ * Get +autorelease+ attribute.
979
+ * Synonymous for {#autorelease?}.
980
+ */
981
+ rb_define_method(rbffi_FunctionClass, "autorelease", function_autorelease_p, 0);
982
+ /*
983
+ * call-seq: autorelease?
984
+ * @return [Boolean] +autorelease+ attribute
985
+ * Get +autorelease+ attribute.
986
+ */
987
+ rb_define_method(rbffi_FunctionClass, "autorelease?", function_autorelease_p, 0);
988
+
989
+ id_call = rb_intern("call");
990
+ id_cbtable = rb_intern("@__ffi_callback_table__");
991
+ id_cb_ref = rb_intern("@__ffi_callback__");
992
+ id_to_native = rb_intern("to_native");
993
+ id_from_native = rb_intern("from_native");
994
+ #if defined(_WIN32)
995
+ InitializeCriticalSection(&async_cb_lock);
996
+ async_cb_cond = CreateEvent(NULL, FALSE, FALSE, NULL);
997
+ #endif
998
+ }
999
+