ffi 0.4.0-x86-mswin32

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 (316) hide show
  1. data/LICENSE +51 -0
  2. data/README.rdoc +69 -0
  3. data/Rakefile +142 -0
  4. data/ext/ffi_c/AbstractMemory.c +412 -0
  5. data/ext/ffi_c/AbstractMemory.h +68 -0
  6. data/ext/ffi_c/AutoPointer.c +60 -0
  7. data/ext/ffi_c/AutoPointer.h +18 -0
  8. data/ext/ffi_c/Buffer.c +171 -0
  9. data/ext/ffi_c/Callback.c +374 -0
  10. data/ext/ffi_c/Callback.h +47 -0
  11. data/ext/ffi_c/DynamicLibrary.c +213 -0
  12. data/ext/ffi_c/DynamicLibrary.h +22 -0
  13. data/ext/ffi_c/Invoker.c +962 -0
  14. data/ext/ffi_c/LastError.c +135 -0
  15. data/ext/ffi_c/LastError.h +18 -0
  16. data/ext/ffi_c/MemoryPointer.c +156 -0
  17. data/ext/ffi_c/MemoryPointer.h +20 -0
  18. data/ext/ffi_c/NullPointer.c +143 -0
  19. data/ext/ffi_c/Platform.c +59 -0
  20. data/ext/ffi_c/Platform.h +16 -0
  21. data/ext/ffi_c/Pointer.c +164 -0
  22. data/ext/ffi_c/Pointer.h +25 -0
  23. data/ext/ffi_c/Struct.c +477 -0
  24. data/ext/ffi_c/Struct.h +26 -0
  25. data/ext/ffi_c/Type.c +230 -0
  26. data/ext/ffi_c/Type.h +28 -0
  27. data/ext/ffi_c/Types.c +118 -0
  28. data/ext/ffi_c/Types.h +54 -0
  29. data/ext/ffi_c/compat.h +22 -0
  30. data/ext/ffi_c/endian.h +40 -0
  31. data/ext/ffi_c/extconf.rb +31 -0
  32. data/ext/ffi_c/ffi.c +76 -0
  33. data/ext/ffi_c/libffi.bsd.mk +34 -0
  34. data/ext/ffi_c/libffi.darwin.mk +75 -0
  35. data/ext/ffi_c/libffi.gnu.mk +29 -0
  36. data/ext/ffi_c/libffi.mk +13 -0
  37. data/ext/ffi_c/libffi/ChangeLog +3243 -0
  38. data/ext/ffi_c/libffi/ChangeLog.libffi +347 -0
  39. data/ext/ffi_c/libffi/ChangeLog.libgcj +40 -0
  40. data/ext/ffi_c/libffi/ChangeLog.v1 +764 -0
  41. data/ext/ffi_c/libffi/LICENSE +21 -0
  42. data/ext/ffi_c/libffi/Makefile.am +177 -0
  43. data/ext/ffi_c/libffi/Makefile.in +1640 -0
  44. data/ext/ffi_c/libffi/README +328 -0
  45. data/ext/ffi_c/libffi/TODO +1 -0
  46. data/ext/ffi_c/libffi/acinclude.m4 +92 -0
  47. data/ext/ffi_c/libffi/aclocal.m4 +7516 -0
  48. data/ext/ffi_c/libffi/compile +142 -0
  49. data/ext/ffi_c/libffi/config.guess +1516 -0
  50. data/ext/ffi_c/libffi/config.sub +1626 -0
  51. data/ext/ffi_c/libffi/configure +24414 -0
  52. data/ext/ffi_c/libffi/configure.ac +365 -0
  53. data/ext/ffi_c/libffi/configure.host +11 -0
  54. data/ext/ffi_c/libffi/depcomp +584 -0
  55. data/ext/ffi_c/libffi/doc/libffi.info +533 -0
  56. data/ext/ffi_c/libffi/doc/libffi.texi +541 -0
  57. data/ext/ffi_c/libffi/doc/stamp-vti +4 -0
  58. data/ext/ffi_c/libffi/doc/version.texi +4 -0
  59. data/ext/ffi_c/libffi/fficonfig.h.in +160 -0
  60. data/ext/ffi_c/libffi/include/Makefile.am +9 -0
  61. data/ext/ffi_c/libffi/include/Makefile.in +422 -0
  62. data/ext/ffi_c/libffi/include/ffi.h.in +393 -0
  63. data/ext/ffi_c/libffi/include/ffi_common.h +98 -0
  64. data/ext/ffi_c/libffi/install-sh +323 -0
  65. data/ext/ffi_c/libffi/libffi.pc.in +10 -0
  66. data/ext/ffi_c/libffi/libtool-version +29 -0
  67. data/ext/ffi_c/libffi/ltcf-c.sh +861 -0
  68. data/ext/ffi_c/libffi/ltcf-cxx.sh +1069 -0
  69. data/ext/ffi_c/libffi/ltcf-gcj.sh +700 -0
  70. data/ext/ffi_c/libffi/ltconfig +2862 -0
  71. data/ext/ffi_c/libffi/ltmain.sh +6930 -0
  72. data/ext/ffi_c/libffi/man/Makefile.am +8 -0
  73. data/ext/ffi_c/libffi/man/Makefile.in +395 -0
  74. data/ext/ffi_c/libffi/man/ffi.3 +31 -0
  75. data/ext/ffi_c/libffi/man/ffi_call.3 +103 -0
  76. data/ext/ffi_c/libffi/man/ffi_prep_cif.3 +66 -0
  77. data/ext/ffi_c/libffi/mdate-sh +201 -0
  78. data/ext/ffi_c/libffi/missing +353 -0
  79. data/ext/ffi_c/libffi/mkinstalldirs +158 -0
  80. data/ext/ffi_c/libffi/src/alpha/ffi.c +284 -0
  81. data/ext/ffi_c/libffi/src/alpha/ffitarget.h +48 -0
  82. data/ext/ffi_c/libffi/src/alpha/osf.S +366 -0
  83. data/ext/ffi_c/libffi/src/arm/ffi.c +309 -0
  84. data/ext/ffi_c/libffi/src/arm/ffitarget.h +49 -0
  85. data/ext/ffi_c/libffi/src/arm/sysv.S +299 -0
  86. data/ext/ffi_c/libffi/src/closures.c +596 -0
  87. data/ext/ffi_c/libffi/src/cris/ffi.c +383 -0
  88. data/ext/ffi_c/libffi/src/cris/ffitarget.h +51 -0
  89. data/ext/ffi_c/libffi/src/cris/sysv.S +215 -0
  90. data/ext/ffi_c/libffi/src/debug.c +59 -0
  91. data/ext/ffi_c/libffi/src/dlmalloc.c +5099 -0
  92. data/ext/ffi_c/libffi/src/frv/eabi.S +128 -0
  93. data/ext/ffi_c/libffi/src/frv/ffi.c +292 -0
  94. data/ext/ffi_c/libffi/src/frv/ffitarget.h +61 -0
  95. data/ext/ffi_c/libffi/src/ia64/ffi.c +580 -0
  96. data/ext/ffi_c/libffi/src/ia64/ffitarget.h +50 -0
  97. data/ext/ffi_c/libffi/src/ia64/ia64_flags.h +40 -0
  98. data/ext/ffi_c/libffi/src/ia64/unix.S +560 -0
  99. data/ext/ffi_c/libffi/src/java_raw_api.c +359 -0
  100. data/ext/ffi_c/libffi/src/m32r/ffi.c +232 -0
  101. data/ext/ffi_c/libffi/src/m32r/ffitarget.h +48 -0
  102. data/ext/ffi_c/libffi/src/m32r/sysv.S +121 -0
  103. data/ext/ffi_c/libffi/src/m68k/ffi.c +278 -0
  104. data/ext/ffi_c/libffi/src/m68k/ffitarget.h +49 -0
  105. data/ext/ffi_c/libffi/src/m68k/sysv.S +234 -0
  106. data/ext/ffi_c/libffi/src/mips/ffi.c +926 -0
  107. data/ext/ffi_c/libffi/src/mips/ffitarget.h +202 -0
  108. data/ext/ffi_c/libffi/src/mips/n32.S +534 -0
  109. data/ext/ffi_c/libffi/src/mips/o32.S +381 -0
  110. data/ext/ffi_c/libffi/src/pa/ffi.c +709 -0
  111. data/ext/ffi_c/libffi/src/pa/ffitarget.h +77 -0
  112. data/ext/ffi_c/libffi/src/pa/hpux32.S +368 -0
  113. data/ext/ffi_c/libffi/src/pa/linux.S +357 -0
  114. data/ext/ffi_c/libffi/src/powerpc/aix.S +225 -0
  115. data/ext/ffi_c/libffi/src/powerpc/aix_closure.S +247 -0
  116. data/ext/ffi_c/libffi/src/powerpc/asm.h +125 -0
  117. data/ext/ffi_c/libffi/src/powerpc/darwin.S +245 -0
  118. data/ext/ffi_c/libffi/src/powerpc/darwin_closure.S +317 -0
  119. data/ext/ffi_c/libffi/src/powerpc/ffi.c +1429 -0
  120. data/ext/ffi_c/libffi/src/powerpc/ffi_darwin.c +800 -0
  121. data/ext/ffi_c/libffi/src/powerpc/ffitarget.h +118 -0
  122. data/ext/ffi_c/libffi/src/powerpc/linux64.S +187 -0
  123. data/ext/ffi_c/libffi/src/powerpc/linux64_closure.S +236 -0
  124. data/ext/ffi_c/libffi/src/powerpc/ppc_closure.S +327 -0
  125. data/ext/ffi_c/libffi/src/powerpc/sysv.S +230 -0
  126. data/ext/ffi_c/libffi/src/prep_cif.c +174 -0
  127. data/ext/ffi_c/libffi/src/raw_api.c +254 -0
  128. data/ext/ffi_c/libffi/src/s390/ffi.c +780 -0
  129. data/ext/ffi_c/libffi/src/s390/ffitarget.h +60 -0
  130. data/ext/ffi_c/libffi/src/s390/sysv.S +434 -0
  131. data/ext/ffi_c/libffi/src/sh/ffi.c +716 -0
  132. data/ext/ffi_c/libffi/src/sh/ffitarget.h +49 -0
  133. data/ext/ffi_c/libffi/src/sh/sysv.S +850 -0
  134. data/ext/ffi_c/libffi/src/sh64/ffi.c +453 -0
  135. data/ext/ffi_c/libffi/src/sh64/ffitarget.h +53 -0
  136. data/ext/ffi_c/libffi/src/sh64/sysv.S +530 -0
  137. data/ext/ffi_c/libffi/src/sparc/ffi.c +610 -0
  138. data/ext/ffi_c/libffi/src/sparc/ffitarget.h +66 -0
  139. data/ext/ffi_c/libffi/src/sparc/v8.S +272 -0
  140. data/ext/ffi_c/libffi/src/sparc/v9.S +307 -0
  141. data/ext/ffi_c/libffi/src/types.c +77 -0
  142. data/ext/ffi_c/libffi/src/x86/darwin.S +443 -0
  143. data/ext/ffi_c/libffi/src/x86/darwin64.S +416 -0
  144. data/ext/ffi_c/libffi/src/x86/ffi.c +475 -0
  145. data/ext/ffi_c/libffi/src/x86/ffi64.c +572 -0
  146. data/ext/ffi_c/libffi/src/x86/ffitarget.h +90 -0
  147. data/ext/ffi_c/libffi/src/x86/freebsd.S +458 -0
  148. data/ext/ffi_c/libffi/src/x86/sysv.S +437 -0
  149. data/ext/ffi_c/libffi/src/x86/unix64.S +418 -0
  150. data/ext/ffi_c/libffi/src/x86/win32.S +391 -0
  151. data/ext/ffi_c/libffi/testsuite/Makefile.am +71 -0
  152. data/ext/ffi_c/libffi/testsuite/Makefile.in +447 -0
  153. data/ext/ffi_c/libffi/testsuite/config/default.exp +1 -0
  154. data/ext/ffi_c/libffi/testsuite/lib/libffi-dg.exp +289 -0
  155. data/ext/ffi_c/libffi/testsuite/lib/target-libpath.exp +263 -0
  156. data/ext/ffi_c/libffi/testsuite/lib/wrapper.exp +45 -0
  157. data/ext/ffi_c/libffi/testsuite/libffi.call/call.exp +36 -0
  158. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn0.c +97 -0
  159. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn1.c +89 -0
  160. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn2.c +89 -0
  161. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn3.c +90 -0
  162. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn4.c +97 -0
  163. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn5.c +99 -0
  164. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_fn6.c +98 -0
  165. data/ext/ffi_c/libffi/testsuite/libffi.call/closure_stdcall.c +72 -0
  166. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_12byte.c +102 -0
  167. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_16byte.c +103 -0
  168. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_18byte.c +104 -0
  169. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_19byte.c +110 -0
  170. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_1_1byte.c +97 -0
  171. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_20byte.c +99 -0
  172. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_20byte1.c +101 -0
  173. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_24byte.c +121 -0
  174. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_2byte.c +98 -0
  175. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_3_1byte.c +103 -0
  176. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_3byte1.c +98 -0
  177. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_3byte2.c +98 -0
  178. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_4_1byte.c +106 -0
  179. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_4byte.c +98 -0
  180. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_5_1_byte.c +117 -0
  181. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_5byte.c +106 -0
  182. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_64byte.c +132 -0
  183. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_6_1_byte.c +121 -0
  184. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_6byte.c +107 -0
  185. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_7_1_byte.c +125 -0
  186. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_7byte.c +105 -0
  187. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_8byte.c +96 -0
  188. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_9byte1.c +98 -0
  189. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_9byte2.c +99 -0
  190. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_double.c +101 -0
  191. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_float.c +99 -0
  192. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_longdouble.c +100 -0
  193. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_pointer.c +101 -0
  194. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_sint16.c +99 -0
  195. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_sint32.c +99 -0
  196. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_sint64.c +99 -0
  197. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_uint16.c +99 -0
  198. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_uint32.c +99 -0
  199. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_align_uint64.c +100 -0
  200. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_double.c +51 -0
  201. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_float.c +51 -0
  202. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_schar.c +82 -0
  203. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_sshort.c +82 -0
  204. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_sshortchar.c +94 -0
  205. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_uchar.c +99 -0
  206. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_ushort.c +82 -0
  207. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_multi_ushortchar.c +94 -0
  208. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_schar.c +52 -0
  209. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_sint.c +50 -0
  210. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_sshort.c +50 -0
  211. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_uchar.c +50 -0
  212. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_uint.c +51 -0
  213. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_ulonglong.c +54 -0
  214. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_ushort.c +51 -0
  215. data/ext/ffi_c/libffi/testsuite/libffi.call/ffitest.h +86 -0
  216. data/ext/ffi_c/libffi/testsuite/libffi.call/float.c +59 -0
  217. data/ext/ffi_c/libffi/testsuite/libffi.call/float1.c +58 -0
  218. data/ext/ffi_c/libffi/testsuite/libffi.call/float2.c +57 -0
  219. data/ext/ffi_c/libffi/testsuite/libffi.call/float3.c +72 -0
  220. data/ext/ffi_c/libffi/testsuite/libffi.call/float4.c +62 -0
  221. data/ext/ffi_c/libffi/testsuite/libffi.call/many.c +69 -0
  222. data/ext/ffi_c/libffi/testsuite/libffi.call/many_win32.c +63 -0
  223. data/ext/ffi_c/libffi/testsuite/libffi.call/negint.c +53 -0
  224. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct.c +160 -0
  225. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct1.c +169 -0
  226. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct10.c +141 -0
  227. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct2.c +118 -0
  228. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct3.c +119 -0
  229. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct4.c +119 -0
  230. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct5.c +120 -0
  231. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct6.c +139 -0
  232. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct7.c +119 -0
  233. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct8.c +139 -0
  234. data/ext/ffi_c/libffi/testsuite/libffi.call/nested_struct9.c +139 -0
  235. data/ext/ffi_c/libffi/testsuite/libffi.call/problem1.c +98 -0
  236. data/ext/ffi_c/libffi/testsuite/libffi.call/promotion.c +59 -0
  237. data/ext/ffi_c/libffi/testsuite/libffi.call/pyobjc-tc.c +114 -0
  238. data/ext/ffi_c/libffi/testsuite/libffi.call/return_dbl.c +35 -0
  239. data/ext/ffi_c/libffi/testsuite/libffi.call/return_dbl1.c +43 -0
  240. data/ext/ffi_c/libffi/testsuite/libffi.call/return_dbl2.c +42 -0
  241. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl.c +35 -0
  242. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl1.c +36 -0
  243. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl2.c +49 -0
  244. data/ext/ffi_c/libffi/testsuite/libffi.call/return_fl3.c +42 -0
  245. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ldl.c +34 -0
  246. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ll.c +41 -0
  247. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ll1.c +42 -0
  248. data/ext/ffi_c/libffi/testsuite/libffi.call/return_sc.c +36 -0
  249. data/ext/ffi_c/libffi/testsuite/libffi.call/return_sl.c +38 -0
  250. data/ext/ffi_c/libffi/testsuite/libffi.call/return_uc.c +38 -0
  251. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ul.c +38 -0
  252. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen.c +44 -0
  253. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen_win32.c +44 -0
  254. data/ext/ffi_c/libffi/testsuite/libffi.call/struct1.c +65 -0
  255. data/ext/ffi_c/libffi/testsuite/libffi.call/struct2.c +67 -0
  256. data/ext/ffi_c/libffi/testsuite/libffi.call/struct3.c +59 -0
  257. data/ext/ffi_c/libffi/testsuite/libffi.call/struct4.c +63 -0
  258. data/ext/ffi_c/libffi/testsuite/libffi.call/struct5.c +65 -0
  259. data/ext/ffi_c/libffi/testsuite/libffi.call/struct6.c +64 -0
  260. data/ext/ffi_c/libffi/testsuite/libffi.call/struct7.c +74 -0
  261. data/ext/ffi_c/libffi/testsuite/libffi.call/struct8.c +80 -0
  262. data/ext/ffi_c/libffi/testsuite/libffi.call/struct9.c +67 -0
  263. data/ext/ffi_c/libffi/testsuite/libffi.special/ffitestcxx.h +86 -0
  264. data/ext/ffi_c/libffi/testsuite/libffi.special/special.exp +38 -0
  265. data/ext/ffi_c/libffi/testsuite/libffi.special/unwindtest.cc +123 -0
  266. data/ext/ffi_c/libffi/testsuite/libffi.special/unwindtest_ffi_call.cc +53 -0
  267. data/ext/ffi_c/libffi/texinfo.tex +7482 -0
  268. data/ext/ffi_c/rbffi.h +23 -0
  269. data/gen/Rakefile +12 -0
  270. data/lib/1.8/ffi_c.so +0 -0
  271. data/lib/1.9/ffi_c.so +0 -0
  272. data/lib/ffi.rb +11 -0
  273. data/lib/ffi/autopointer.rb +61 -0
  274. data/lib/ffi/buffer.rb +0 -0
  275. data/lib/ffi/callback.rb +10 -0
  276. data/lib/ffi/enum.rb +78 -0
  277. data/lib/ffi/errno.rb +8 -0
  278. data/lib/ffi/ffi.rb +96 -0
  279. data/lib/ffi/io.rb +21 -0
  280. data/lib/ffi/library.rb +243 -0
  281. data/lib/ffi/managedstruct.rb +55 -0
  282. data/lib/ffi/memorypointer.rb +73 -0
  283. data/lib/ffi/platform.rb +78 -0
  284. data/lib/ffi/pointer.rb +119 -0
  285. data/lib/ffi/struct.rb +354 -0
  286. data/lib/ffi/tools/const_generator.rb +177 -0
  287. data/lib/ffi/tools/generator.rb +58 -0
  288. data/lib/ffi/tools/generator_task.rb +35 -0
  289. data/lib/ffi/tools/struct_generator.rb +194 -0
  290. data/lib/ffi/tools/types_generator.rb +123 -0
  291. data/lib/ffi/types.rb +153 -0
  292. data/lib/ffi/union.rb +17 -0
  293. data/lib/ffi/variadic.rb +30 -0
  294. data/spec/ffi/bool_spec.rb +24 -0
  295. data/spec/ffi/buffer_spec.rb +196 -0
  296. data/spec/ffi/callback_spec.rb +560 -0
  297. data/spec/ffi/enum_spec.rb +164 -0
  298. data/spec/ffi/errno_spec.rb +13 -0
  299. data/spec/ffi/library_spec.rb +144 -0
  300. data/spec/ffi/managed_struct_spec.rb +56 -0
  301. data/spec/ffi/number_spec.rb +231 -0
  302. data/spec/ffi/pointer_spec.rb +195 -0
  303. data/spec/ffi/rbx/attach_function_spec.rb +27 -0
  304. data/spec/ffi/rbx/memory_pointer_spec.rb +102 -0
  305. data/spec/ffi/rbx/spec_helper.rb +1 -0
  306. data/spec/ffi/rbx/struct_spec.rb +13 -0
  307. data/spec/ffi/spec_helper.rb +17 -0
  308. data/spec/ffi/string_spec.rb +103 -0
  309. data/spec/ffi/struct_callback_spec.rb +41 -0
  310. data/spec/ffi/struct_initialize_spec.rb +30 -0
  311. data/spec/ffi/struct_spec.rb +476 -0
  312. data/spec/ffi/typedef_spec.rb +48 -0
  313. data/spec/ffi/union_spec.rb +60 -0
  314. data/spec/ffi/variadic_spec.rb +84 -0
  315. data/spec/spec.opts +4 -0
  316. metadata +375 -0
@@ -0,0 +1,47 @@
1
+ #ifndef _CALLBACK_H
2
+ #define _CALLBACK_H
3
+
4
+ #include "Types.h"
5
+ #include "Type.h"
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+ #include <ffi.h>
11
+
12
+ typedef struct {
13
+ Type type; // The native type of a CallbackInfo object
14
+ VALUE rbReturnType;
15
+ VALUE rbParameterTypes;
16
+
17
+ Type* returnType;
18
+ Type** parameterTypes;
19
+ ffi_type* ffiReturnType;
20
+ ffi_type** ffiParameterTypes;
21
+ ffi_cif ffi_cif;
22
+ int parameterCount;
23
+ int flags;
24
+ ffi_abi abi;
25
+ } CallbackInfo;
26
+
27
+ typedef struct {
28
+ void* code;
29
+ ffi_closure* ffi_closure;
30
+ ffi_cif ffi_cif;
31
+ int flags;
32
+ CallbackInfo* cbInfo;
33
+ VALUE rbCallbackInfo;
34
+ VALUE rbProc;
35
+ } NativeCallback;
36
+
37
+ extern VALUE rbffi_CallbackInfoClass;
38
+ extern void rbffi_Callback_Init(VALUE ffiModule);
39
+ extern VALUE rbffi_NativeCallback_NewInstance(VALUE, VALUE);
40
+ extern VALUE rbffi_NativeCallback_ForProc(VALUE proc, VALUE cbInfo);
41
+
42
+ #ifdef __cplusplus
43
+ }
44
+ #endif
45
+
46
+ #endif /* _CALLBACK_H */
47
+
@@ -0,0 +1,213 @@
1
+ #include <sys/types.h>
2
+ #include <stdio.h>
3
+ #include <stdint.h>
4
+ #if defined(_WIN32) || defined(__WIN32__)
5
+ # include <windows.h>
6
+ #else
7
+ # include <dlfcn.h>
8
+ #endif
9
+ #include <ruby.h>
10
+
11
+ #include <ffi.h>
12
+
13
+ #include "rbffi.h"
14
+ #include "compat.h"
15
+ #include "AbstractMemory.h"
16
+ #include "Pointer.h"
17
+ #include "DynamicLibrary.h"
18
+
19
+ typedef struct LibrarySymbol_ {
20
+ AbstractMemory memory;
21
+ VALUE library;
22
+ VALUE name;
23
+ } LibrarySymbol;
24
+
25
+ static VALUE library_initialize(VALUE self, VALUE libname, VALUE libflags);
26
+ static void library_free(Library* lib);
27
+
28
+
29
+ static VALUE symbol_allocate(VALUE klass);
30
+ static VALUE symbol_new(VALUE library, void* address, VALUE name);
31
+ static void symbol_mark(LibrarySymbol* sym);
32
+
33
+ static VALUE LibraryClass = Qnil, SymbolClass = Qnil;
34
+
35
+ #if defined(_WIN32) || defined(__WIN32__)
36
+ static void* dl_open(const char* name, int flags);
37
+ static void dl_error(char* buf, int size);
38
+ #define dl_sym(handle, name) GetProcAddress(handle, name)
39
+ #define dl_close(handle) FreeLibrary(handle)
40
+ enum { RTLD_LAZY=1, RTLD_NOW, RTLD_GLOBAL, RTLD_LOCAL };
41
+ #else
42
+ # define dl_open(name, flags) dlopen(name, flags != 0 ? flags : RTLD_LAZY)
43
+ # define dl_error(buf, size) do { snprintf(buf, size, "%s", dlerror()); } while(0)
44
+ # define dl_sym(handle, name) dlsym(handle, name)
45
+ # define dl_close(handle) dlclose(handle)
46
+ #ifndef RTLD_LOCAL
47
+ # define RTLD_LOCAL 8
48
+ #endif
49
+ #endif
50
+
51
+ static VALUE
52
+ library_allocate(VALUE klass)
53
+ {
54
+ Library* library;
55
+ return Data_Make_Struct(klass, Library, NULL, library_free, library);
56
+ }
57
+
58
+ static VALUE
59
+ library_open(VALUE klass, VALUE libname, VALUE libflags)
60
+ {
61
+ return library_initialize(library_allocate(klass), libname, libflags);
62
+ }
63
+
64
+ static VALUE
65
+ library_initialize(VALUE self, VALUE libname, VALUE libflags)
66
+ {
67
+ Library* library;
68
+ int flags;
69
+
70
+ Check_Type(libflags, T_FIXNUM);
71
+
72
+ Data_Get_Struct(self, Library, library);
73
+ flags = libflags != Qnil ? NUM2UINT(libflags) : 0;
74
+
75
+ library->handle = dl_open(libname != Qnil ? StringValueCStr(libname) : NULL, flags);
76
+ if (library->handle == NULL) {
77
+ char errmsg[1024];
78
+ dl_error(errmsg, sizeof(errmsg));
79
+ rb_raise(rb_eLoadError, "Could not open library '%s': %s",
80
+ libname != Qnil ? StringValueCStr(libname) : "[current process]",
81
+ errmsg);
82
+ }
83
+ rb_iv_set(self, "@name", libname != Qnil ? libname : rb_str_new2("[current process]"));
84
+ return self;
85
+ }
86
+
87
+ static VALUE
88
+ library_dlsym(VALUE self, VALUE name)
89
+ {
90
+ Library* library;
91
+ void* address = NULL;
92
+ Check_Type(name, T_STRING);
93
+
94
+ Data_Get_Struct(self, Library, library);
95
+ address = dl_sym(library->handle, StringValueCStr(name));
96
+
97
+ return address != NULL ? symbol_new(self, address, name) : Qnil;
98
+ }
99
+
100
+ static VALUE
101
+ library_dlerror(VALUE self)
102
+ {
103
+ char errmsg[1024];
104
+ dl_error(errmsg, sizeof(errmsg));
105
+ return rb_tainted_str_new2(errmsg);
106
+ }
107
+
108
+ static void
109
+ library_free(Library* library)
110
+ {
111
+ // dlclose() on MacOS tends to segfault - avoid it
112
+ #ifndef __APPLE__
113
+ if (library->handle != NULL) {
114
+ dl_close(library->handle);
115
+ }
116
+ #endif
117
+ xfree(library);
118
+ }
119
+
120
+ #if defined(_WIN32) || defined(__WIN32__)
121
+ static void*
122
+ dl_open(const char* name, int flags)
123
+ {
124
+ if (name == NULL) {
125
+ return GetModuleHandle(NULL);
126
+ } else {
127
+ return LoadLibraryEx(name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
128
+ }
129
+ }
130
+
131
+ static void
132
+ dl_error(char* buf, int size)
133
+ {
134
+ FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
135
+ 0, buf, size, NULL);
136
+ }
137
+ #endif
138
+
139
+ static VALUE
140
+ symbol_allocate(VALUE klass)
141
+ {
142
+ LibrarySymbol* sym;
143
+ VALUE obj = Data_Make_Struct(klass, LibrarySymbol, NULL, -1, sym);
144
+ sym->name = Qnil;
145
+ sym->library = Qnil;
146
+
147
+ return obj;
148
+ }
149
+
150
+ static VALUE
151
+ symbol_new(VALUE library, void* address, VALUE name)
152
+ {
153
+ LibrarySymbol* sym;
154
+ VALUE obj = Data_Make_Struct(SymbolClass, LibrarySymbol, symbol_mark, -1, sym);
155
+
156
+ sym->memory.address = address;
157
+ sym->memory.size = LONG_MAX;
158
+ sym->library = library;
159
+ sym->name = name;
160
+
161
+ return obj;
162
+ }
163
+
164
+ static void
165
+ symbol_mark(LibrarySymbol* sym)
166
+ {
167
+ rb_gc_mark(sym->library);
168
+ rb_gc_mark(sym->name);
169
+ }
170
+
171
+ static VALUE
172
+ symbol_inspect(VALUE self)
173
+ {
174
+ LibrarySymbol* sym;
175
+ char buf[256];
176
+
177
+ Data_Get_Struct(self, LibrarySymbol, sym);
178
+ snprintf(buf, sizeof(buf), "#<FFI::Library::Symbol name=%s address=%p>",
179
+ StringValueCStr(sym->name), sym->memory.address);
180
+ return rb_str_new2(buf);
181
+ }
182
+
183
+ void
184
+ rbffi_DynamicLibrary_Init(VALUE moduleFFI)
185
+ {
186
+ LibraryClass = rb_define_class_under(moduleFFI, "DynamicLibrary", rb_cObject);
187
+ rb_global_variable(&LibraryClass);
188
+ SymbolClass = rb_define_class_under(LibraryClass, "Symbol", rbffi_PointerClass);
189
+ rb_global_variable(&SymbolClass);
190
+
191
+ rb_define_const(moduleFFI, "NativeLibrary", LibraryClass); // backwards compat library
192
+ rb_define_alloc_func(LibraryClass, library_allocate);
193
+ rb_define_singleton_method(LibraryClass, "open", library_open, 2);
194
+ rb_define_singleton_method(LibraryClass, "last_error", library_dlerror, 0);
195
+ rb_define_method(LibraryClass, "initialize", library_initialize, 2);
196
+ rb_define_method(LibraryClass, "find_symbol", library_dlsym, 1);
197
+ rb_define_method(LibraryClass, "find_function", library_dlsym, 1);
198
+ rb_define_method(LibraryClass, "find_variable", library_dlsym, 1);
199
+ rb_define_method(LibraryClass, "last_error", library_dlerror, 0);
200
+ rb_define_attr(LibraryClass, "name", 1, 0);
201
+
202
+ rb_define_alloc_func(SymbolClass, symbol_allocate);
203
+ rb_undef_method(SymbolClass, "new");
204
+ rb_define_method(SymbolClass, "inspect", symbol_inspect, 0);
205
+
206
+ #define DEF(x) rb_define_const(LibraryClass, "RTLD_" #x, UINT2NUM(RTLD_##x))
207
+ DEF(LAZY);
208
+ DEF(NOW);
209
+ DEF(GLOBAL);
210
+ DEF(LOCAL);
211
+
212
+ }
213
+
@@ -0,0 +1,22 @@
1
+
2
+ #ifndef _LIBRARY_H
3
+ #define _LIBRARY_H
4
+
5
+ #ifdef __cplusplus
6
+ extern "C" {
7
+ #endif
8
+
9
+
10
+
11
+ typedef struct Library {
12
+ void* handle;
13
+ } Library;
14
+
15
+ extern void rbffi_DynamicLibrary_Init(VALUE ffiModule);
16
+
17
+ #ifdef __cplusplus
18
+ }
19
+ #endif
20
+
21
+ #endif /* _LIBRARY_H */
22
+
@@ -0,0 +1,962 @@
1
+ #include <sys/param.h>
2
+ #include <sys/types.h>
3
+ #ifndef _WIN32
4
+ # include <sys/mman.h>
5
+ #endif
6
+ #include <stdio.h>
7
+ #include <stdint.h>
8
+ #include <stdbool.h>
9
+ #ifndef _WIN32
10
+ # include <unistd.h>
11
+ # include <pthread.h>
12
+ #endif
13
+ #include <errno.h>
14
+ #include <ruby.h>
15
+
16
+ #include <ffi.h>
17
+ #include "rbffi.h"
18
+ #include "compat.h"
19
+
20
+ #include "AbstractMemory.h"
21
+ #include "Pointer.h"
22
+ #include "Struct.h"
23
+ #include "Platform.h"
24
+ #include "Callback.h"
25
+ #include "Types.h"
26
+ #include "Type.h"
27
+ #include "LastError.h"
28
+
29
+ #if defined(__i386__) && !defined(_WIN32) && !defined(__WIN32__)
30
+ # define USE_RAW
31
+ #endif
32
+ #if defined(HAVE_NATIVETHREAD) && !defined(_WIN32) && !defined(__WIN32__)
33
+ # define USE_PTHREAD_LOCAL
34
+ #endif
35
+ #define MAX_FIXED_ARITY (3)
36
+
37
+ #ifndef roundup
38
+ # define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
39
+ #endif
40
+
41
+ typedef struct MethodHandle MethodHandle;
42
+ typedef struct Invoker Invoker;
43
+
44
+ struct Invoker {
45
+ VALUE address;
46
+ VALUE enums;
47
+ void* function;
48
+ ffi_cif cif;
49
+ int paramCount;
50
+ ffi_type** ffiParamTypes;
51
+ NativeType* paramTypes;
52
+ Type* returnType;
53
+ VALUE rbReturnType;
54
+ VALUE callbackArray;
55
+ int callbackCount;
56
+ VALUE* callbackParameters;
57
+ ffi_abi abi;
58
+ MethodHandle* methodHandle;
59
+ };
60
+ #ifdef USE_RAW
61
+ # define METHOD_CLOSURE ffi_raw_closure
62
+ #else
63
+ # define METHOD_CLOSURE ffi_closure
64
+ #endif
65
+ typedef struct MethodHandlePool MethodHandlePool;
66
+ struct MethodHandle {
67
+ Invoker* invoker;
68
+ int arity;
69
+ METHOD_CLOSURE* closure;
70
+ void* code;
71
+ ffi_cif cif;
72
+ struct MethodHandlePool* pool;
73
+ MethodHandle* next;
74
+ };
75
+ #if !defined(_WIN32) && !defined(__WIN32__)
76
+ struct MethodHandlePool {
77
+ #ifdef HAVE_NATIVETHREAD
78
+ pthread_mutex_t mutex;
79
+ #endif
80
+ MethodHandle* list;
81
+ };
82
+ #endif /* _WIN32 */
83
+
84
+ static VALUE invoker_allocate(VALUE klass);
85
+ static VALUE invoker_initialize(VALUE self, VALUE function, VALUE parameterTypes,
86
+ VALUE rbReturnType, VALUE returnType, VALUE convention, VALUE enums);
87
+ static void invoker_mark(Invoker *);
88
+ static void invoker_free(Invoker *);
89
+ static VALUE invoker_call(int argc, VALUE* argv, VALUE self);
90
+ static VALUE invoker_arity(VALUE self);
91
+ static void* callback_param(VALUE proc, VALUE cbinfo);
92
+ #ifdef USE_RAW
93
+ static void attached_method_invoke(ffi_cif* cif, void* retval, ffi_raw* parameters, void* user_data);
94
+ static void attached_method_vinvoke(ffi_cif* cif, void* retval, ffi_raw* parameters, void* user_data);
95
+ #else
96
+ #ifndef _WIN32
97
+ static void attached_method_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data);
98
+ #endif /* _WIN32 */
99
+ static void attached_method_vinvoke(ffi_cif* cif, void* retval, void** parameters, void* user_data);
100
+ #endif
101
+ static MethodHandle* method_handle_alloc(int arity);
102
+ static void method_handle_free(MethodHandle *);
103
+
104
+
105
+ #ifndef _WIN32
106
+ static int PageSize;
107
+ #endif
108
+
109
+ VALUE rbffi_InvokerClass = Qnil;
110
+ static VALUE classInvoker = Qnil, classVariadicInvoker = Qnil;
111
+ static ID to_ptr = 0;
112
+ static ID map_symbol_id = 0;
113
+
114
+ #ifdef USE_RAW
115
+ # ifndef __i386__
116
+ # error "RAW argument packing only supported on i386"
117
+ # endif
118
+
119
+ #define INT8_SIZE (sizeof(char))
120
+ #define INT16_SIZE (sizeof(short))
121
+ #define INT32_SIZE (sizeof(int))
122
+ #define INT64_SIZE (sizeof(long long))
123
+ #define FLOAT32_SIZE (sizeof(float))
124
+ #define FLOAT64_SIZE (sizeof(double))
125
+ #define ADDRESS_SIZE (sizeof(void *))
126
+ #define INT8_ADJ (4)
127
+ #define INT16_ADJ (4)
128
+ #define INT32_ADJ (4)
129
+ #define INT64_ADJ (8)
130
+ #define FLOAT32_ADJ (4)
131
+ #define FLOAT64_ADJ (8)
132
+ #define ADDRESS_ADJ (sizeof(void *))
133
+
134
+ #endif /* USE_RAW */
135
+
136
+ #ifdef USE_RAW
137
+ # define ADJ(p, a) ((p) = (FFIStorage*) (((char *) p) + a##_ADJ))
138
+ #else
139
+ # define ADJ(p, a) (++(p))
140
+ #endif
141
+
142
+ static VALUE
143
+ invoker_allocate(VALUE klass)
144
+ {
145
+ Invoker *invoker;
146
+ VALUE obj = Data_Make_Struct(klass, Invoker, invoker_mark, invoker_free, invoker);
147
+
148
+ invoker->rbReturnType = Qnil;
149
+ invoker->callbackArray = Qnil;
150
+ invoker->address = Qnil;
151
+ invoker->enums = Qnil;
152
+
153
+ return obj;
154
+ }
155
+
156
+ static VALUE
157
+ invoker_initialize(VALUE self, VALUE function, VALUE parameterTypes,
158
+ VALUE rbReturnTypeSymbol, VALUE returnType, VALUE convention, VALUE enums)
159
+ {
160
+ Invoker* invoker = NULL;
161
+ ffi_type* ffiReturnType;
162
+ ffi_abi abi;
163
+ ffi_status ffiStatus;
164
+ int i;
165
+
166
+ Check_Type(parameterTypes, T_ARRAY);
167
+ Check_Type(rbReturnTypeSymbol, T_SYMBOL);
168
+ Check_Type(convention, T_STRING);
169
+ Check_Type(function, T_DATA);
170
+
171
+ Data_Get_Struct(self, Invoker, invoker);
172
+ invoker->enums = enums;
173
+ invoker->address = function;
174
+ invoker->function = rbffi_AbstractMemory_Cast(function, rbffi_PointerClass)->address;
175
+ invoker->paramCount = RARRAY_LEN(parameterTypes);
176
+ invoker->paramTypes = ALLOC_N(NativeType, invoker->paramCount);
177
+ invoker->ffiParamTypes = ALLOC_N(ffi_type *, invoker->paramCount);
178
+
179
+ for (i = 0; i < invoker->paramCount; ++i) {
180
+ VALUE entry = rb_ary_entry(parameterTypes, i);
181
+ if (rb_obj_is_kind_of(entry, rbffi_CallbackInfoClass)) {
182
+ invoker->callbackParameters = REALLOC_N(invoker->callbackParameters, VALUE,
183
+ invoker->callbackCount + 1);
184
+ invoker->callbackParameters[invoker->callbackCount++] = entry;
185
+ invoker->paramTypes[i] = NATIVE_CALLBACK;
186
+ invoker->ffiParamTypes[i] = &ffi_type_pointer;
187
+ } else if (rb_obj_is_kind_of(entry, rbffi_TypeClass)){
188
+ int paramType = rbffi_Type_GetIntValue(entry);
189
+ invoker->paramTypes[i] = paramType;
190
+ invoker->ffiParamTypes[i] = rbffi_NativeType_ToFFI(paramType);
191
+ }
192
+ if (invoker->ffiParamTypes[i] == NULL) {
193
+ rb_raise(rb_eTypeError, "Invalid parameter type");
194
+ }
195
+ }
196
+ if (!rb_obj_is_kind_of(returnType, rbffi_TypeClass)) {
197
+ rb_raise(rb_eTypeError, "Invalid return type");
198
+ }
199
+ Data_Get_Struct(returnType, Type, invoker->returnType);
200
+ invoker->rbReturnType = rb_obj_is_kind_of(returnType, rbffi_CallbackInfoClass)
201
+ ? returnType : rbReturnTypeSymbol;
202
+
203
+ ffiReturnType = invoker->returnType->ffiType;
204
+ if (ffiReturnType == NULL) {
205
+ rb_raise(rb_eTypeError, "Invalid return type");
206
+ }
207
+ #if defined(_WIN32) || defined(__WIN32__)
208
+ abi = strcmp(StringValueCStr(convention), "stdcall") == 0 ? FFI_STDCALL : FFI_DEFAULT_ABI;
209
+ #else
210
+ abi = FFI_DEFAULT_ABI;
211
+ #endif
212
+ ffiStatus = ffi_prep_cif(&invoker->cif, abi, invoker->paramCount,
213
+ ffiReturnType, invoker->ffiParamTypes);
214
+ switch (ffiStatus) {
215
+ case FFI_BAD_ABI:
216
+ rb_raise(rb_eArgError, "Invalid ABI specified");
217
+ case FFI_BAD_TYPEDEF:
218
+ rb_raise(rb_eArgError, "Invalid argument type specified");
219
+ case FFI_OK:
220
+ break;
221
+ default:
222
+ rb_raise(rb_eArgError, "Unknown FFI error");
223
+ }
224
+ invoker->methodHandle = method_handle_alloc(invoker->callbackCount < 1 ? invoker->paramCount : -1);
225
+ invoker->methodHandle->invoker = invoker;
226
+ return self;
227
+ }
228
+
229
+ static VALUE
230
+ variadic_invoker_new(VALUE klass, VALUE function, VALUE rbReturnTypeSymbol, VALUE returnType, VALUE convention, VALUE enums)
231
+ {
232
+ Invoker* invoker = NULL;
233
+ VALUE retval = Qnil;
234
+
235
+ Check_Type(rbReturnTypeSymbol, T_SYMBOL);
236
+ Check_Type(convention, T_STRING);
237
+ Check_Type(function, T_DATA);
238
+
239
+ retval = Data_Make_Struct(klass, Invoker, invoker_mark, invoker_free, invoker);
240
+ invoker->enums = enums;
241
+ invoker->address = function;
242
+ invoker->function = rbffi_AbstractMemory_Cast(function, rbffi_PointerClass)->address;
243
+ #if defined(_WIN32) || defined(__WIN32__)
244
+ invoker->abi = strcmp(StringValueCStr(convention), "stdcall") == 0 ? FFI_STDCALL : FFI_DEFAULT_ABI;
245
+ #else
246
+ invoker->abi = FFI_DEFAULT_ABI;
247
+ #endif
248
+
249
+ invoker->rbReturnType = rb_obj_is_kind_of(returnType, rbffi_CallbackInfoClass)
250
+ ? returnType : rbReturnTypeSymbol;
251
+ if (!rb_obj_is_kind_of(returnType, rbffi_TypeClass)) {
252
+ rb_raise(rb_eTypeError, "Invalid return type");
253
+ }
254
+ Data_Get_Struct(returnType, Type, invoker->returnType);
255
+ invoker->paramCount = -1;
256
+ return retval;
257
+ }
258
+
259
+ static ffi_type* methodHandleParamTypes[MAX_FIXED_ARITY + 2];
260
+ static ffi_type* methodHandleVarargParamTypes[]= {
261
+ &ffi_type_sint,
262
+ &ffi_type_pointer,
263
+ NULL,
264
+ };
265
+
266
+ #if defined(_WIN32) || defined(__WIN32__)
267
+ static MethodHandle*
268
+ method_handle_alloc(int arity)
269
+ {
270
+ char errmsg[1024];
271
+ ffi_type* ffiReturnType = (sizeof (VALUE) == sizeof (long))
272
+ ? &ffi_type_ulong : &ffi_type_uint64;
273
+ int ffiParamCount, ffiStatus;
274
+ MethodHandle* method = NULL;
275
+ ffi_type** ffiParamTypes;
276
+ void (*fn)(ffi_cif* cif, void* retval, void** parameters, void* user_data);
277
+
278
+ ffiParamCount = 3;
279
+ ffiParamTypes = methodHandleVarargParamTypes;
280
+ fn = attached_method_vinvoke;
281
+ method = ALLOC_N(MethodHandle, 1);
282
+ memset(method, 0, sizeof(*method));
283
+ method->arity = -1;
284
+ ffiStatus = ffi_prep_cif(&method->cif, FFI_DEFAULT_ABI, ffiParamCount,
285
+ ffiReturnType, ffiParamTypes);
286
+ if (ffiStatus != FFI_OK) {
287
+ snprintf(errmsg, sizeof (errmsg), "ffi_prep_cif failed. status=%#x", ffiStatus);
288
+ goto error;
289
+ }
290
+ method->closure = ffi_closure_alloc(sizeof(*method->closure), &method->code);
291
+ if (method->closure == NULL) {
292
+ snprintf(errmsg, sizeof(errmsg), "ffi_closure_alloc failed");
293
+ goto error;
294
+ }
295
+ ffiStatus = ffi_prep_closure_loc(method->closure, &method->cif, fn, method, method->code);
296
+ if (ffiStatus != FFI_OK) {
297
+ snprintf(errmsg, sizeof (errmsg), "ffi_prep_closure failed. status=%#x", ffiStatus);
298
+ goto error;
299
+ }
300
+ return method;
301
+ error:
302
+ if (method != NULL) {
303
+ if (method->closure != NULL) {
304
+ ffi_closure_free(method->closure);
305
+ }
306
+ xfree(method);
307
+ }
308
+ rb_raise(rb_eRuntimeError, "%s", errmsg);
309
+ }
310
+ static void
311
+ method_handle_free(MethodHandle* method)
312
+ {
313
+ if (method->closure != NULL) {
314
+ ffi_closure_free(method->closure);
315
+ }
316
+ xfree(method);
317
+ }
318
+ #else
319
+ static MethodHandlePool methodHandlePool[MAX_FIXED_ARITY + 1], defaultMethodHandlePool;
320
+
321
+ #if defined(HAVE_NATIVETHREAD) && !defined(_WIN32)
322
+ # define pool_lock(p) pthread_mutex_lock(&(p)->mutex)
323
+ # define pool_unlock(p) pthread_mutex_unlock(&(p)->mutex)
324
+ #else
325
+ # define pool_lock(p)
326
+ # define pool_unlock(p)
327
+ #endif
328
+ static MethodHandle*
329
+ method_handle_alloc(int arity)
330
+ {
331
+ MethodHandle* method, *list = NULL;
332
+ MethodHandlePool* pool;
333
+ ffi_type** ffiParamTypes;
334
+ ffi_type* ffiReturnType;
335
+ caddr_t page;
336
+ int ffiParamCount, ffiStatus;
337
+ int nclosures, closureSize, methodArity;
338
+ int i;
339
+ #ifdef USE_RAW
340
+ void (*fn)(ffi_cif* cif, void* retval, ffi_raw* parameters, void* user_data);
341
+ #else
342
+ void (*fn)(ffi_cif* cif, void* retval, void** parameters, void* user_data);
343
+ #endif
344
+
345
+
346
+ pool = (arity >= 0 && arity <= MAX_FIXED_ARITY) ? &methodHandlePool[arity] : &defaultMethodHandlePool;
347
+ pool_lock(pool);
348
+ if (pool->list != NULL) {
349
+ method = pool->list;
350
+ pool->list = pool->list->next;
351
+ pool_unlock(pool);
352
+ return method;
353
+ }
354
+ ffiReturnType = (sizeof (VALUE) == sizeof (long))
355
+ ? &ffi_type_ulong : &ffi_type_uint64;
356
+ closureSize = roundup(sizeof(METHOD_CLOSURE), 8);
357
+ nclosures = PageSize / closureSize;
358
+ page = mmap(NULL, PageSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
359
+ if (page == (caddr_t) -1) {
360
+ pool_unlock(pool);
361
+ rb_raise(rb_eRuntimeError, "mmap failed. errno=%d (%s)", errno, strerror(errno));
362
+ }
363
+
364
+ /* figure out whichh function to bounce the execution through */
365
+ if (arity >= 0 && arity <= MAX_FIXED_ARITY) {
366
+ ffiParamCount = arity + 1;
367
+ ffiParamTypes = methodHandleParamTypes;
368
+ fn = attached_method_invoke;
369
+ methodArity = arity;
370
+ } else {
371
+ ffiParamCount = 3;
372
+ ffiParamTypes = methodHandleVarargParamTypes;
373
+ fn = attached_method_vinvoke;
374
+ methodArity = -1;
375
+ }
376
+
377
+ for (i = 0; i < nclosures; ++i) {
378
+ char errmsg[256];
379
+ method = calloc(1, sizeof(MethodHandle));
380
+ if (method == NULL) {
381
+ snprintf(errmsg, sizeof(errmsg), "malloc failed: %s", strerror(errno));
382
+ goto error;
383
+ }
384
+ method->next = list;
385
+ list = method;
386
+ method->pool = pool;
387
+ method->code = method->closure = (METHOD_CLOSURE *) (page + (i * closureSize));
388
+
389
+ ffiStatus = ffi_prep_cif(&method->cif, FFI_DEFAULT_ABI, ffiParamCount,
390
+ ffiReturnType, ffiParamTypes);
391
+ if (ffiStatus != FFI_OK) {
392
+ snprintf(errmsg, sizeof(errmsg), "ffi_prep_cif failed. status=%#x", ffiStatus);
393
+ goto error;
394
+ }
395
+
396
+ #ifdef USE_RAW
397
+ ffiStatus = ffi_prep_raw_closure(method->closure, &method->cif, fn, method);
398
+ #else
399
+ ffiStatus = ffi_prep_closure(method->closure, &method->cif, fn, method);
400
+ #endif
401
+ if (ffiStatus != FFI_OK) {
402
+ snprintf(errmsg, sizeof(errmsg), "ffi_prep_closure failed. status=%#x", ffiStatus);
403
+ goto error;
404
+ }
405
+ method->arity = methodArity;
406
+ continue;
407
+ error:
408
+ while (list != NULL) {
409
+ method = list;
410
+ list = list->next;
411
+ free(method);
412
+ }
413
+ munmap(page, PageSize);
414
+ pool_unlock(pool);
415
+ rb_raise(rb_eRuntimeError, "%s", errmsg);
416
+ }
417
+ mprotect(page, PageSize, PROT_READ | PROT_EXEC);
418
+
419
+ /* take the first member of the list for this handle */
420
+ method = list;
421
+ list = list->next;
422
+
423
+ /* now add the new block of MethodHandles to the pool */
424
+ if (list != NULL) {
425
+ list->next = pool->list;
426
+ pool->list = list;
427
+ }
428
+ pool_unlock(pool);
429
+ return method;
430
+ }
431
+ static void
432
+ method_handle_free(MethodHandle* method)
433
+ {
434
+ MethodHandlePool* pool = method->pool;
435
+ pool_lock(pool);
436
+ method->next = pool->list;
437
+ pool->list = method;
438
+ pool_unlock(pool);
439
+ }
440
+ #endif /* _WIN32 */
441
+
442
+ typedef union {
443
+ #ifdef USE_RAW
444
+ signed int s8, s16, s32;
445
+ unsigned int u8, u16, u32;
446
+ #else
447
+ signed char s8;
448
+ unsigned char u8;
449
+ signed short s16;
450
+ unsigned short u16;
451
+ signed int s32;
452
+ unsigned int u32;
453
+ #endif
454
+ signed long long i64;
455
+ unsigned long long u64;
456
+ void* ptr;
457
+ float f32;
458
+ double f64;
459
+ } FFIStorage;
460
+
461
+ static inline int
462
+ getSignedInt(VALUE value, int type, int minValue, int maxValue, const char* typeName, VALUE enums)
463
+ {
464
+ int i;
465
+ if (type == T_SYMBOL && enums != Qnil) {
466
+ value = rb_funcall(enums, map_symbol_id, 1, value);
467
+ if (value == Qnil) {
468
+ rb_raise(rb_eTypeError, "Expected a valid enum constant");
469
+ }
470
+ }
471
+ else if (type != T_FIXNUM && type != T_BIGNUM) {
472
+ rb_raise(rb_eTypeError, "Expected an Integer parameter");
473
+ }
474
+ i = NUM2INT(value);
475
+ if (i < minValue || i > maxValue) {
476
+ rb_raise(rb_eRangeError, "Value %d outside %s range", i, typeName);
477
+ }
478
+ return i;
479
+ }
480
+
481
+ static inline int
482
+ getUnsignedInt(VALUE value, int type, int maxValue, const char* typeName)
483
+ {
484
+ int i;
485
+ if (type != T_FIXNUM && type != T_BIGNUM) {
486
+ rb_raise(rb_eTypeError, "Expected an Integer parameter");
487
+ }
488
+ i = NUM2INT(value);
489
+ if (i < 0 || i > maxValue) {
490
+ rb_raise(rb_eRangeError, "Value %d outside %s range", i, typeName);
491
+ }
492
+ return i;
493
+ }
494
+
495
+ /* Special handling/checking for unsigned 32 bit integers */
496
+ static inline unsigned int
497
+ getUnsignedInt32(VALUE value, int type)
498
+ {
499
+ long long i;
500
+ if (type != T_FIXNUM && type != T_BIGNUM) {
501
+ rb_raise(rb_eTypeError, "Expected an Integer parameter");
502
+ }
503
+ i = NUM2LL(value);
504
+ if (i < 0L || i > 0xffffffffL) {
505
+ rb_raise(rb_eRangeError, "Value %lld outside unsigned int range", i);
506
+ }
507
+ return (unsigned int) i;
508
+ }
509
+
510
+ static void
511
+ ffi_arg_setup(const Invoker* invoker, int argc, VALUE* argv, NativeType* paramTypes,
512
+ FFIStorage* paramStorage, void** ffiValues)
513
+ {
514
+ VALUE callbackProc = Qnil;
515
+ FFIStorage* param = &paramStorage[0];
516
+ int i, argidx, cbidx, argCount;
517
+
518
+ if (invoker->paramCount != -1 && invoker->paramCount != argc) {
519
+ if (argc == (invoker->paramCount - 1) && invoker->callbackCount == 1 && rb_block_given_p()) {
520
+ callbackProc = rb_block_proc();
521
+ } else {
522
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, invoker->paramCount);
523
+ }
524
+ }
525
+ argCount = invoker->paramCount != -1 ? invoker->paramCount : argc;
526
+ for (i = 0, argidx = 0, cbidx = 0; i < argCount; ++i) {
527
+ int type = argidx < argc ? TYPE(argv[argidx]) : T_NONE;
528
+ ffiValues[i] = param;
529
+ switch (paramTypes[i]) {
530
+ case NATIVE_INT8:
531
+ param->s8 = getSignedInt(argv[argidx++], type, -128, 127, "char", Qnil);
532
+ ADJ(param, INT8);
533
+ break;
534
+ case NATIVE_INT16:
535
+ param->s16 = getSignedInt(argv[argidx++], type, -0x8000, 0x7fff, "short", Qnil);
536
+ ADJ(param, INT16);
537
+ break;
538
+ case NATIVE_INT32:
539
+ case NATIVE_ENUM:
540
+ param->s32 = getSignedInt(argv[argidx++], type, -0x80000000, 0x7fffffff, "int", invoker->enums);
541
+ ADJ(param, INT32);
542
+ break;
543
+ case NATIVE_BOOL:
544
+ if (type != T_TRUE && type != T_FALSE) {
545
+ rb_raise(rb_eTypeError, "Expected a Boolean parameter");
546
+ }
547
+ param->s32 = argv[argidx++] == Qtrue;
548
+ ADJ(param, INT32);
549
+ break;
550
+ case NATIVE_UINT8:
551
+ param->u8 = getUnsignedInt(argv[argidx++], type, 0xff, "unsigned char");
552
+ ADJ(param, INT8);
553
+ break;
554
+ case NATIVE_UINT16:
555
+ param->u16 = getUnsignedInt(argv[argidx++], type, 0xffff, "unsigned short");
556
+ ADJ(param, INT16);
557
+ break;
558
+ case NATIVE_UINT32:
559
+ /* Special handling/checking for unsigned 32 bit integers */
560
+ param->u32 = getUnsignedInt32(argv[argidx++], type);
561
+ ADJ(param, INT32);
562
+ break;
563
+ case NATIVE_INT64:
564
+ if (type != T_FIXNUM && type != T_BIGNUM) {
565
+ rb_raise(rb_eTypeError, "Expected an Integer parameter");
566
+ }
567
+ param->i64 = NUM2LL(argv[argidx]);
568
+ ADJ(param, INT64);
569
+ ++argidx;
570
+ break;
571
+ case NATIVE_UINT64:
572
+ if (type != T_FIXNUM && type != T_BIGNUM) {
573
+ rb_raise(rb_eTypeError, "Expected an Integer parameter");
574
+ }
575
+ param->u64 = NUM2ULL(argv[argidx]);
576
+ ADJ(param, INT64);
577
+ ++argidx;
578
+ break;
579
+ case NATIVE_FLOAT32:
580
+ if (type != T_FLOAT && type != T_FIXNUM) {
581
+ rb_raise(rb_eTypeError, "Expected a Float parameter");
582
+ }
583
+ param->f32 = (float) NUM2DBL(argv[argidx]);
584
+ ADJ(param, FLOAT32);
585
+ ++argidx;
586
+ break;
587
+ case NATIVE_FLOAT64:
588
+ if (type != T_FLOAT && type != T_FIXNUM) {
589
+ rb_raise(rb_eTypeError, "Expected a Float parameter");
590
+ }
591
+ param->f64 = NUM2DBL(argv[argidx]);
592
+ ADJ(param, FLOAT64);
593
+ ++argidx;
594
+ break;
595
+ case NATIVE_STRING:
596
+ if (type == T_STRING) {
597
+ if (rb_safe_level() >= 1 && OBJ_TAINTED(argv[argidx])) {
598
+ rb_raise(rb_eSecurityError, "Unsafe string parameter");
599
+ }
600
+ param->ptr = StringValueCStr(argv[argidx]);
601
+ } else if (type == T_NIL) {
602
+ param->ptr = NULL;
603
+ } else {
604
+ rb_raise(rb_eArgError, "Invalid String value");
605
+ }
606
+ ADJ(param, ADDRESS);
607
+ ++argidx;
608
+ break;
609
+ case NATIVE_POINTER:
610
+ case NATIVE_BUFFER_IN:
611
+ case NATIVE_BUFFER_OUT:
612
+ case NATIVE_BUFFER_INOUT:
613
+ if (type == T_DATA && rb_obj_is_kind_of(argv[argidx], rbffi_AbstractMemoryClass)) {
614
+ param->ptr = ((AbstractMemory *) DATA_PTR(argv[argidx]))->address;
615
+ } else if (type == T_DATA && rb_obj_is_kind_of(argv[argidx], rbffi_StructClass)) {
616
+ AbstractMemory* memory = ((Struct *) DATA_PTR(argv[argidx]))->pointer;
617
+ param->ptr = memory != NULL ? memory->address : NULL;
618
+ } else if (type == T_STRING) {
619
+ if (rb_safe_level() >= 1 && OBJ_TAINTED(argv[argidx])) {
620
+ rb_raise(rb_eSecurityError, "Unsafe string parameter");
621
+ }
622
+ param->ptr = StringValuePtr(argv[argidx]);
623
+ } else if (type == T_NIL) {
624
+ param->ptr = NULL;
625
+ } else if (rb_respond_to(argv[argidx], to_ptr)) {
626
+ VALUE ptr = rb_funcall2(argv[argidx], to_ptr, 0, NULL);
627
+ if (rb_obj_is_kind_of(ptr, rbffi_AbstractMemoryClass) && TYPE(ptr) == T_DATA) {
628
+ param->ptr = ((AbstractMemory *) DATA_PTR(ptr))->address;
629
+ } else {
630
+ rb_raise(rb_eArgError, "to_ptr returned an invalid pointer");
631
+ }
632
+
633
+ } else {
634
+ rb_raise(rb_eArgError, ":pointer argument is not a valid pointer");
635
+ }
636
+ ADJ(param, ADDRESS);
637
+ ++argidx;
638
+ break;
639
+ case NATIVE_CALLBACK:
640
+ if (callbackProc != Qnil) {
641
+ param->ptr = callback_param(callbackProc, invoker->callbackParameters[cbidx++]);
642
+ } else {
643
+ param->ptr = callback_param(argv[argidx], invoker->callbackParameters[cbidx++]);
644
+ ++argidx;
645
+ }
646
+ ADJ(param, ADDRESS);
647
+ break;
648
+ default:
649
+ rb_raise(rb_eArgError, "Invalid parameter type: %d", paramTypes[i]);
650
+ }
651
+ }
652
+ }
653
+ static inline VALUE
654
+ ffi_invoke(ffi_cif* cif, Invoker* invoker, void** ffiValues)
655
+ {
656
+ FFIStorage retval;
657
+
658
+ #ifdef USE_RAW
659
+ ffi_raw_call(cif, FFI_FN(invoker->function), &retval, (ffi_raw *) ffiValues[0]);
660
+ #else
661
+ ffi_call(cif, FFI_FN(invoker->function), &retval, ffiValues);
662
+ #endif
663
+ rbffi_save_errno();
664
+
665
+ return rbffi_NativeValue_ToRuby(invoker->returnType, invoker->rbReturnType, &retval,
666
+ invoker->enums);
667
+ }
668
+
669
+ static VALUE
670
+ invoker_call(int argc, VALUE* argv, VALUE self)
671
+ {
672
+ Invoker* invoker;
673
+ int argCount;
674
+ FFIStorage params_[MAX_FIXED_ARITY], *params = &params_[0];
675
+ void* ffiValues_[MAX_FIXED_ARITY], **ffiValues = &ffiValues_[0];
676
+
677
+ Data_Get_Struct(self, Invoker, invoker);
678
+
679
+ argCount = invoker->paramCount != -1 ? invoker->paramCount : argc;
680
+
681
+ if (argCount > MAX_FIXED_ARITY) {
682
+ params = ALLOCA_N(FFIStorage, argCount);
683
+ ffiValues = ALLOCA_N(void *, argCount);
684
+ }
685
+
686
+ ffi_arg_setup(invoker, argc, argv, invoker->paramTypes, params, ffiValues);
687
+
688
+ return ffi_invoke(&invoker->cif, invoker, ffiValues);
689
+ }
690
+
691
+ #ifdef USE_RAW
692
+
693
+ /*
694
+ * attached_method_invoke is used as the <= 3 argument fixed-arity fast path
695
+ */
696
+ static void
697
+ attached_method_invoke(ffi_cif* cif, void* retval, ffi_raw* parameters, void* user_data)
698
+ {
699
+ MethodHandle* handle = (MethodHandle *) user_data;
700
+ Invoker* invoker = handle->invoker;
701
+ void* ffiValues[MAX_FIXED_ARITY];
702
+ FFIStorage params[MAX_FIXED_ARITY];
703
+
704
+ if (invoker->paramCount > 0) {
705
+ ffi_arg_setup(invoker, invoker->paramCount, (VALUE *)&parameters[1],
706
+ invoker->paramTypes, params, ffiValues);
707
+ }
708
+ *((VALUE *) retval) = ffi_invoke(&invoker->cif, invoker, ffiValues);
709
+ }
710
+
711
+ /*
712
+ * attached_method_vinvoke is used functions with more than 3 parameters
713
+ */
714
+ static void
715
+ attached_method_vinvoke(ffi_cif* cif, void* retval, ffi_raw* parameters, void* user_data)
716
+ {
717
+ MethodHandle* handle = (MethodHandle *) user_data;
718
+ Invoker* invoker = handle->invoker;
719
+ void** ffiValues = ALLOCA_N(void *, invoker->paramCount);
720
+ FFIStorage* params = ALLOCA_N(FFIStorage, invoker->paramCount);
721
+ int argc = parameters[0].sint;
722
+ VALUE* argv = *(VALUE **) &parameters[1];
723
+
724
+ ffi_arg_setup(invoker, argc, argv, invoker->paramTypes, params, ffiValues);
725
+ *((VALUE *) retval) = ffi_invoke(&invoker->cif, invoker, ffiValues);
726
+ }
727
+
728
+ #else
729
+ #ifndef _WIN32
730
+ static void
731
+ attached_method_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
732
+ {
733
+ MethodHandle* handle = (MethodHandle *) user_data;
734
+ Invoker* invoker = handle->invoker;
735
+ void* ffiValues[MAX_FIXED_ARITY];
736
+ FFIStorage params[MAX_FIXED_ARITY];
737
+ VALUE argv[MAX_FIXED_ARITY];
738
+ int i;
739
+ //printf("Attached method invoke nargs=%d paramCount=%d\n", cif->nargs, invoker->paramCount); fflush(stdout);
740
+ for (i = 0; i < invoker->paramCount; ++i) {
741
+ memcpy(&argv[i], parameters[i + 1], sizeof(argv[i]));
742
+ }
743
+ if (invoker->paramCount > 0) {
744
+ ffi_arg_setup(invoker, invoker->paramCount, argv, invoker->paramTypes, params, ffiValues);
745
+ }
746
+ *((VALUE *) retval) = ffi_invoke(&invoker->cif, invoker, ffiValues);
747
+ }
748
+ #endif /* _WIN32 */
749
+ static void
750
+ attached_method_vinvoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
751
+ {
752
+ MethodHandle* handle = (MethodHandle *) user_data;
753
+ Invoker* invoker = handle->invoker;
754
+ void** ffiValues = ALLOCA_N(void *, invoker->paramCount);
755
+ FFIStorage* params = ALLOCA_N(FFIStorage, invoker->paramCount);
756
+ int argc = *(int *) parameters[0];
757
+ VALUE* argv = *(VALUE **) parameters[1];
758
+
759
+ ffi_arg_setup(invoker, argc, argv, invoker->paramTypes, params, ffiValues);
760
+ *((VALUE *) retval) = ffi_invoke(&invoker->cif, invoker, ffiValues);
761
+ }
762
+ #endif
763
+
764
+ static VALUE
765
+ invoker_attach(VALUE self, VALUE module, VALUE name)
766
+ {
767
+ Invoker* invoker;
768
+ MethodHandle* handle;
769
+ char var[1024];
770
+
771
+ Data_Get_Struct(self, Invoker, invoker);
772
+ if (invoker->paramCount == -1) {
773
+ rb_raise(rb_eRuntimeError, "Cannot attach variadic invokers");
774
+ }
775
+
776
+ handle = invoker->methodHandle;
777
+ rb_define_module_function(module, StringValueCStr(name),
778
+ handle->code, handle->arity);
779
+ snprintf(var, sizeof(var), "@@%s", StringValueCStr(name));
780
+ rb_cv_set(module, var, self);
781
+ rb_define_method(module, StringValueCStr(name),
782
+ handle->code, handle->arity);
783
+ return self;
784
+ }
785
+ static VALUE
786
+ invoker_arity(VALUE self)
787
+ {
788
+ Invoker* invoker;
789
+ Data_Get_Struct(self, Invoker, invoker);
790
+ return INT2FIX(invoker->paramCount);
791
+ }
792
+
793
+ static void
794
+ invoker_mark(Invoker *invoker)
795
+ {
796
+ if (invoker->callbackCount > 0) {
797
+ rb_gc_mark_locations(&invoker->callbackParameters[0], &invoker->callbackParameters[invoker->callbackCount]);
798
+ }
799
+ if (invoker->address != Qnil) {
800
+ rb_gc_mark(invoker->address);
801
+ }
802
+ if (invoker->enums != Qnil) {
803
+ rb_gc_mark(invoker->enums);
804
+ }
805
+ if (invoker->rbReturnType != Qnil) {
806
+ rb_gc_mark(invoker->rbReturnType);
807
+ }
808
+ }
809
+
810
+ static void
811
+ invoker_free(Invoker *invoker)
812
+ {
813
+ if (invoker != NULL) {
814
+ if (invoker->paramTypes != NULL) {
815
+ xfree(invoker->paramTypes);
816
+ invoker->paramTypes = NULL;
817
+ }
818
+ if (invoker->ffiParamTypes != NULL) {
819
+ xfree(invoker->ffiParamTypes);
820
+ invoker->ffiParamTypes = NULL;
821
+ }
822
+ if (invoker->callbackParameters != NULL) {
823
+ xfree(invoker->callbackParameters);
824
+ invoker->callbackParameters = NULL;
825
+ }
826
+ if (invoker->methodHandle != NULL) {
827
+ method_handle_free(invoker->methodHandle);
828
+ }
829
+ xfree(invoker);
830
+ }
831
+ }
832
+
833
+ static VALUE
834
+ variadic_invoker_call(VALUE self, VALUE parameterTypes, VALUE parameterValues)
835
+ {
836
+ Invoker* invoker;
837
+ FFIStorage* params;
838
+ ffi_cif cif;
839
+ void** ffiValues;
840
+ ffi_type** ffiParamTypes;
841
+ ffi_type* ffiReturnType;
842
+ NativeType* paramTypes;
843
+ VALUE* argv;
844
+ int paramCount = 0, i;
845
+ ffi_status ffiStatus;
846
+
847
+ Check_Type(parameterTypes, T_ARRAY);
848
+ Check_Type(parameterValues, T_ARRAY);
849
+
850
+ Data_Get_Struct(self, Invoker, invoker);
851
+ paramCount = RARRAY_LEN(parameterTypes);
852
+ paramTypes = ALLOCA_N(NativeType, paramCount);
853
+ ffiParamTypes = ALLOCA_N(ffi_type *, paramCount);
854
+ params = ALLOCA_N(FFIStorage, paramCount);
855
+ ffiValues = ALLOCA_N(void*, paramCount);
856
+ argv = ALLOCA_N(VALUE, paramCount);
857
+
858
+ for (i = 0; i < paramCount; ++i) {
859
+ VALUE entry = rb_ary_entry(parameterTypes, i);
860
+ int paramType = rbffi_Type_GetIntValue(entry);
861
+ switch (paramType) {
862
+ case NATIVE_INT8:
863
+ case NATIVE_INT16:
864
+ case NATIVE_INT32:
865
+ case NATIVE_ENUM:
866
+ paramType = NATIVE_INT32;
867
+ break;
868
+ case NATIVE_UINT8:
869
+ case NATIVE_UINT16:
870
+ case NATIVE_UINT32:
871
+ paramType = NATIVE_UINT32;
872
+ break;
873
+ case NATIVE_FLOAT32:
874
+ paramType = NATIVE_FLOAT64;
875
+ break;
876
+ }
877
+ paramTypes[i] = paramType;
878
+ ffiParamTypes[i] = rbffi_NativeType_ToFFI(paramType);
879
+ if (ffiParamTypes[i] == NULL) {
880
+ rb_raise(rb_eArgError, "Invalid parameter type #%x", paramType);
881
+ }
882
+ argv[i] = rb_ary_entry(parameterValues, i);
883
+ }
884
+
885
+ ffiReturnType = invoker->returnType->ffiType;
886
+ if (ffiReturnType == NULL) {
887
+ rb_raise(rb_eArgError, "Invalid return type");
888
+ }
889
+ ffiStatus = ffi_prep_cif(&cif, invoker->abi, paramCount, ffiReturnType, ffiParamTypes);
890
+ switch (ffiStatus) {
891
+ case FFI_BAD_ABI:
892
+ rb_raise(rb_eArgError, "Invalid ABI specified");
893
+ case FFI_BAD_TYPEDEF:
894
+ rb_raise(rb_eArgError, "Invalid argument type specified");
895
+ case FFI_OK:
896
+ break;
897
+ default:
898
+ rb_raise(rb_eArgError, "Unknown FFI error");
899
+ }
900
+ ffi_arg_setup(invoker, paramCount, argv, paramTypes, params, ffiValues);
901
+ return ffi_invoke(&cif, invoker, ffiValues);
902
+ }
903
+
904
+ static void*
905
+ callback_param(VALUE proc, VALUE cbInfo)
906
+ {
907
+ VALUE callback ;
908
+ if (proc == Qnil) {
909
+ return NULL ;
910
+ }
911
+ callback = rbffi_NativeCallback_ForProc(proc, cbInfo);
912
+ return ((NativeCallback *) DATA_PTR(callback))->code;
913
+ }
914
+
915
+ void
916
+ rbffi_Invoker_Init(VALUE moduleFFI)
917
+ {
918
+ ffi_type* ffiValueType;
919
+ int i;
920
+
921
+ rbffi_InvokerClass = classInvoker = rb_define_class_under(moduleFFI, "Invoker", rb_cObject);
922
+ rb_global_variable(&rbffi_InvokerClass);
923
+ rb_global_variable(&classInvoker);
924
+
925
+ rb_define_alloc_func(classInvoker, invoker_allocate);
926
+ rb_define_method(classInvoker, "initialize", invoker_initialize, 6);
927
+ rb_define_method(classInvoker, "call", invoker_call, -1);
928
+ rb_define_alias(classInvoker, "call0", "call");
929
+ rb_define_alias(classInvoker, "call1", "call");
930
+ rb_define_alias(classInvoker, "call2", "call");
931
+ rb_define_alias(classInvoker, "call3", "call");
932
+ rb_define_method(classInvoker, "arity", invoker_arity, 0);
933
+ rb_define_method(classInvoker, "attach", invoker_attach, 2);
934
+ classVariadicInvoker = rb_define_class_under(moduleFFI, "VariadicInvoker", rb_cObject);
935
+ rb_global_variable(&classVariadicInvoker);
936
+
937
+ rb_define_singleton_method(classVariadicInvoker, "__new", variadic_invoker_new, 5);
938
+ rb_define_method(classVariadicInvoker, "invoke", variadic_invoker_call, 2);
939
+ rb_define_alias(classVariadicInvoker, "call", "invoke");
940
+
941
+ to_ptr = rb_intern("to_ptr");
942
+ map_symbol_id = rb_intern("__map_symbol");
943
+
944
+ ffiValueType = (sizeof (VALUE) == sizeof (long))
945
+ ? &ffi_type_ulong : &ffi_type_uint64;
946
+ for (i = 0; i <= MAX_FIXED_ARITY + 1; ++i) {
947
+ methodHandleParamTypes[i] = ffiValueType;
948
+ }
949
+ methodHandleVarargParamTypes[2] = ffiValueType;
950
+
951
+ #ifndef _WIN32
952
+ PageSize = sysconf(_SC_PAGESIZE);
953
+ #endif /* _WIN32 */
954
+
955
+ #if defined(USE_PTHREAD_LOCAL)
956
+ for (i = 0; i < 4; ++i) {
957
+ pthread_mutex_init(&methodHandlePool[i].mutex, NULL);
958
+ }
959
+ pthread_mutex_init(&defaultMethodHandlePool.mutex, NULL);
960
+ #endif /* USE_PTHREAD_LOCAL */
961
+
962
+ }