foreign.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /* Copyright (C) 2010-2016 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <ffi.h>
  22. #include <alloca.h>
  23. #include <alignof.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include "libguile/_scm.h"
  28. #include "libguile/bytevectors.h"
  29. #include "libguile/instructions.h"
  30. #include "libguile/threads.h"
  31. #include "libguile/foreign.h"
  32. SCM_SYMBOL (sym_void, "void");
  33. SCM_SYMBOL (sym_float, "float");
  34. SCM_SYMBOL (sym_double, "double");
  35. SCM_SYMBOL (sym_uint8, "uint8");
  36. SCM_SYMBOL (sym_int8, "int8");
  37. SCM_SYMBOL (sym_uint16, "uint16");
  38. SCM_SYMBOL (sym_int16, "int16");
  39. SCM_SYMBOL (sym_uint32, "uint32");
  40. SCM_SYMBOL (sym_int32, "int32");
  41. SCM_SYMBOL (sym_uint64, "uint64");
  42. SCM_SYMBOL (sym_int64, "int64");
  43. SCM_SYMBOL (sym_short, "short");
  44. SCM_SYMBOL (sym_int, "int");
  45. SCM_SYMBOL (sym_long, "long");
  46. SCM_SYMBOL (sym_unsigned_short, "unsigned-short");
  47. SCM_SYMBOL (sym_unsigned_int, "unsigned-int");
  48. SCM_SYMBOL (sym_unsigned_long, "unsigned-long");
  49. SCM_SYMBOL (sym_size_t, "size_t");
  50. SCM_SYMBOL (sym_ssize_t, "ssize_t");
  51. SCM_SYMBOL (sym_ptrdiff_t, "ptrdiff_t");
  52. SCM_SYMBOL (sym_intptr_t, "intptr_t");
  53. SCM_SYMBOL (sym_uintptr_t, "uintptr_t");
  54. /* that's for pointers, you know. */
  55. SCM_SYMBOL (sym_asterisk, "*");
  56. SCM_SYMBOL (sym_null, "%null-pointer");
  57. SCM_SYMBOL (sym_null_pointer_error, "null-pointer-error");
  58. /* The cell representing the null pointer. */
  59. static SCM null_pointer;
  60. /* Raise a null pointer dereference error. */
  61. static void
  62. null_pointer_error (const char *func_name)
  63. {
  64. scm_error (sym_null_pointer_error, func_name,
  65. "null pointer dereference", SCM_EOL, SCM_EOL);
  66. }
  67. static SCM cif_to_procedure (SCM cif, SCM func_ptr, int with_errno);
  68. static SCM pointer_weak_refs = SCM_BOOL_F;
  69. static void
  70. register_weak_reference (SCM from, SCM to)
  71. {
  72. scm_weak_table_putq_x (pointer_weak_refs, from, to);
  73. }
  74. static void
  75. pointer_finalizer_trampoline (void *ptr, void *data)
  76. {
  77. scm_t_pointer_finalizer finalizer = data;
  78. finalizer (SCM_POINTER_VALUE (SCM_PACK_POINTER (ptr)));
  79. }
  80. SCM_DEFINE (scm_pointer_p, "pointer?", 1, 0, 0,
  81. (SCM obj),
  82. "Return @code{#t} if @var{obj} is a pointer object, "
  83. "@code{#f} otherwise.\n")
  84. #define FUNC_NAME s_scm_pointer_p
  85. {
  86. return scm_from_bool (SCM_POINTER_P (obj));
  87. }
  88. #undef FUNC_NAME
  89. SCM_DEFINE (scm_make_pointer, "make-pointer", 1, 1, 0,
  90. (SCM address, SCM finalizer),
  91. "Return a foreign pointer object pointing to @var{address}. "
  92. "If @var{finalizer} is passed, it should be a pointer to a "
  93. "one-argument C function that will be called when the pointer "
  94. "object becomes unreachable.")
  95. #define FUNC_NAME s_scm_make_pointer
  96. {
  97. void *c_finalizer;
  98. scm_t_uintptr c_address;
  99. c_address = scm_to_uintptr_t (address);
  100. if (SCM_UNBNDP (finalizer))
  101. c_finalizer = NULL;
  102. else
  103. {
  104. SCM_VALIDATE_POINTER (2, finalizer);
  105. c_finalizer = SCM_POINTER_VALUE (finalizer);
  106. }
  107. return scm_from_pointer ((void *) c_address, c_finalizer);
  108. }
  109. #undef FUNC_NAME
  110. void *
  111. scm_to_pointer (SCM pointer)
  112. #define FUNC_NAME "scm_to_pointer"
  113. {
  114. SCM_VALIDATE_POINTER (1, pointer);
  115. return SCM_POINTER_VALUE (pointer);
  116. }
  117. #undef FUNC_NAME
  118. SCM
  119. scm_from_pointer (void *ptr, scm_t_pointer_finalizer finalizer)
  120. {
  121. SCM ret;
  122. if (ptr == NULL && finalizer == NULL)
  123. ret = null_pointer;
  124. else
  125. {
  126. ret = scm_cell (scm_tc7_pointer, (scm_t_bits) ptr);
  127. if (finalizer)
  128. scm_i_set_finalizer (SCM2PTR (ret), pointer_finalizer_trampoline,
  129. finalizer);
  130. }
  131. return ret;
  132. }
  133. SCM_DEFINE (scm_pointer_address, "pointer-address", 1, 0, 0,
  134. (SCM pointer),
  135. "Return the numerical value of @var{pointer}.")
  136. #define FUNC_NAME s_scm_pointer_address
  137. {
  138. SCM_VALIDATE_POINTER (1, pointer);
  139. return scm_from_uintptr_t ((scm_t_uintptr) SCM_POINTER_VALUE (pointer));
  140. }
  141. #undef FUNC_NAME
  142. SCM_DEFINE (scm_pointer_to_scm, "pointer->scm", 1, 0, 0,
  143. (SCM pointer),
  144. "Unsafely cast @var{pointer} to a Scheme object.\n"
  145. "Cross your fingers!")
  146. #define FUNC_NAME s_scm_pointer_to_scm
  147. {
  148. SCM_VALIDATE_POINTER (1, pointer);
  149. return SCM_PACK ((scm_t_bits) SCM_POINTER_VALUE (pointer));
  150. }
  151. #undef FUNC_NAME
  152. SCM_DEFINE (scm_scm_to_pointer, "scm->pointer", 1, 0, 0,
  153. (SCM scm),
  154. "Return a foreign pointer object with the @code{object-address}\n"
  155. "of @var{scm}.")
  156. #define FUNC_NAME s_scm_scm_to_pointer
  157. {
  158. SCM ret;
  159. ret = scm_from_pointer ((void*) SCM_UNPACK (scm), NULL);
  160. if (SCM_HEAP_OBJECT_P (ret))
  161. register_weak_reference (ret, scm);
  162. return ret;
  163. }
  164. #undef FUNC_NAME
  165. SCM_DEFINE (scm_pointer_to_bytevector, "pointer->bytevector", 2, 2, 0,
  166. (SCM pointer, SCM len, SCM offset, SCM uvec_type),
  167. "Return a bytevector aliasing the @var{len} bytes pointed\n"
  168. "to by @var{pointer}.\n\n"
  169. "The user may specify an alternate default interpretation for\n"
  170. "the memory by passing the @var{uvec_type} argument, to indicate\n"
  171. "that the memory is an array of elements of that type.\n"
  172. "@var{uvec_type} should be something that\n"
  173. "@code{uniform-vector-element-type} would return, like @code{f32}\n"
  174. "or @code{s16}.\n\n"
  175. "When @var{offset} is passed, it specifies the offset in bytes\n"
  176. "relative to @var{pointer} of the memory region aliased by the\n"
  177. "returned bytevector.")
  178. #define FUNC_NAME s_scm_pointer_to_bytevector
  179. {
  180. SCM ret;
  181. scm_t_int8 *ptr;
  182. size_t boffset, blen;
  183. scm_t_array_element_type btype;
  184. SCM_VALIDATE_POINTER (1, pointer);
  185. ptr = SCM_POINTER_VALUE (pointer);
  186. if (SCM_UNLIKELY (ptr == NULL))
  187. null_pointer_error (FUNC_NAME);
  188. if (SCM_UNBNDP (uvec_type))
  189. btype = SCM_ARRAY_ELEMENT_TYPE_VU8;
  190. else
  191. {
  192. int i;
  193. for (i = 0; i <= SCM_ARRAY_ELEMENT_TYPE_LAST; i++)
  194. if (scm_is_eq (uvec_type, scm_i_array_element_types[i]))
  195. break;
  196. switch (i)
  197. {
  198. case SCM_ARRAY_ELEMENT_TYPE_VU8:
  199. case SCM_ARRAY_ELEMENT_TYPE_U8:
  200. case SCM_ARRAY_ELEMENT_TYPE_S8:
  201. case SCM_ARRAY_ELEMENT_TYPE_U16:
  202. case SCM_ARRAY_ELEMENT_TYPE_S16:
  203. case SCM_ARRAY_ELEMENT_TYPE_U32:
  204. case SCM_ARRAY_ELEMENT_TYPE_S32:
  205. case SCM_ARRAY_ELEMENT_TYPE_U64:
  206. case SCM_ARRAY_ELEMENT_TYPE_S64:
  207. case SCM_ARRAY_ELEMENT_TYPE_F32:
  208. case SCM_ARRAY_ELEMENT_TYPE_F64:
  209. case SCM_ARRAY_ELEMENT_TYPE_C32:
  210. case SCM_ARRAY_ELEMENT_TYPE_C64:
  211. btype = i;
  212. break;
  213. default:
  214. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec_type,
  215. "uniform vector type");
  216. }
  217. }
  218. if (SCM_UNBNDP (offset))
  219. boffset = 0;
  220. else
  221. boffset = scm_to_size_t (offset);
  222. blen = scm_to_size_t (len);
  223. ret = scm_c_take_typed_bytevector ((signed char *) ptr + boffset,
  224. blen, btype, pointer);
  225. return ret;
  226. }
  227. #undef FUNC_NAME
  228. SCM_DEFINE (scm_bytevector_to_pointer, "bytevector->pointer", 1, 1, 0,
  229. (SCM bv, SCM offset),
  230. "Return a pointer pointer aliasing the memory pointed to by\n"
  231. "@var{bv} or @var{offset} bytes after @var{bv} when @var{offset}\n"
  232. "is passed.")
  233. #define FUNC_NAME s_scm_bytevector_to_pointer
  234. {
  235. SCM ret;
  236. signed char *ptr;
  237. size_t boffset;
  238. SCM_VALIDATE_BYTEVECTOR (1, bv);
  239. ptr = SCM_BYTEVECTOR_CONTENTS (bv);
  240. if (SCM_UNBNDP (offset))
  241. boffset = 0;
  242. else
  243. boffset = scm_to_unsigned_integer (offset, 0,
  244. SCM_BYTEVECTOR_LENGTH (bv) - 1);
  245. ret = scm_from_pointer (ptr + boffset, NULL);
  246. register_weak_reference (ret, bv);
  247. return ret;
  248. }
  249. #undef FUNC_NAME
  250. SCM_DEFINE (scm_set_pointer_finalizer_x, "set-pointer-finalizer!", 2, 0, 0,
  251. (SCM pointer, SCM finalizer),
  252. "Arrange for the C procedure wrapped by @var{finalizer} to be\n"
  253. "called on the pointer wrapped by @var{pointer} when @var{pointer}\n"
  254. "becomes unreachable. Note: the C procedure should not call into\n"
  255. "Scheme. If you need a Scheme finalizer, use guardians.")
  256. #define FUNC_NAME s_scm_set_pointer_finalizer_x
  257. {
  258. SCM_VALIDATE_POINTER (1, pointer);
  259. SCM_VALIDATE_POINTER (2, finalizer);
  260. scm_i_add_finalizer (SCM2PTR (pointer), pointer_finalizer_trampoline,
  261. SCM_POINTER_VALUE (finalizer));
  262. return SCM_UNSPECIFIED;
  263. }
  264. #undef FUNC_NAME
  265. void
  266. scm_i_pointer_print (SCM pointer, SCM port, scm_print_state *pstate)
  267. {
  268. scm_puts ("#<pointer 0x", port);
  269. scm_uintprint (scm_to_uintptr_t (scm_pointer_address (pointer)), 16, port);
  270. scm_putc ('>', port);
  271. }
  272. /* Non-primitive helpers functions. These procedures could be
  273. implemented in terms of the primitives above but would be inefficient
  274. (heap allocation overhead, Scheme/C round trips, etc.) */
  275. SCM_DEFINE (scm_dereference_pointer, "dereference-pointer", 1, 0, 0,
  276. (SCM pointer),
  277. "Assuming @var{pointer} points to a memory region that\n"
  278. "holds a pointer, return this pointer.")
  279. #define FUNC_NAME s_scm_dereference_pointer
  280. {
  281. void **ptr;
  282. SCM_VALIDATE_POINTER (1, pointer);
  283. ptr = SCM_POINTER_VALUE (pointer);
  284. if (SCM_UNLIKELY (ptr == NULL))
  285. null_pointer_error (FUNC_NAME);
  286. return scm_from_pointer (*ptr, NULL);
  287. }
  288. #undef FUNC_NAME
  289. SCM_DEFINE (scm_string_to_pointer, "string->pointer", 1, 1, 0,
  290. (SCM string, SCM encoding),
  291. "Return a foreign pointer to a nul-terminated copy of\n"
  292. "@var{string} in the given @var{encoding}, defaulting to\n"
  293. "the current locale encoding. The C string is freed when\n"
  294. "the returned foreign pointer becomes unreachable.\n\n"
  295. "This is the Scheme equivalent of @code{scm_to_stringn}.")
  296. #define FUNC_NAME s_scm_string_to_pointer
  297. {
  298. SCM_VALIDATE_STRING (1, string);
  299. /* XXX: Finalizers slow down libgc; they could be avoided if
  300. `scm_to_string' & co. were able to use libgc-allocated memory. */
  301. if (SCM_UNBNDP (encoding))
  302. return scm_from_pointer (scm_to_locale_string (string), free);
  303. else
  304. {
  305. char *enc;
  306. SCM ret;
  307. SCM_VALIDATE_STRING (2, encoding);
  308. enc = scm_to_locale_string (encoding);
  309. scm_dynwind_begin (0);
  310. scm_dynwind_free (enc);
  311. ret = scm_from_pointer
  312. (scm_to_stringn (string, NULL, enc,
  313. scm_i_default_string_failed_conversion_handler ()),
  314. free);
  315. scm_dynwind_end ();
  316. return ret;
  317. }
  318. }
  319. #undef FUNC_NAME
  320. SCM_DEFINE (scm_pointer_to_string, "pointer->string", 1, 2, 0,
  321. (SCM pointer, SCM length, SCM encoding),
  322. "Return the string representing the C string pointed to by\n"
  323. "@var{pointer}. If @var{length} is omitted or @code{-1}, the\n"
  324. "string is assumed to be nul-terminated. Otherwise\n"
  325. "@var{length} is the number of bytes in memory pointed to by\n"
  326. "@var{pointer}. The C string is assumed to be in the given\n"
  327. "@var{encoding}, defaulting to the current locale encoding.\n\n"
  328. "This is the Scheme equivalent of @code{scm_from_stringn}.")
  329. #define FUNC_NAME s_scm_pointer_to_string
  330. {
  331. size_t len;
  332. SCM_VALIDATE_POINTER (1, pointer);
  333. if (SCM_UNBNDP (length)
  334. || scm_is_true (scm_eqv_p (length, scm_from_int (-1))))
  335. len = (size_t)-1;
  336. else
  337. len = scm_to_size_t (length);
  338. if (SCM_UNBNDP (encoding))
  339. return scm_from_locale_stringn (SCM_POINTER_VALUE (pointer), len);
  340. else
  341. {
  342. char *enc;
  343. SCM ret;
  344. SCM_VALIDATE_STRING (3, encoding);
  345. enc = scm_to_locale_string (encoding);
  346. scm_dynwind_begin (0);
  347. scm_dynwind_free (enc);
  348. ret = scm_from_stringn (SCM_POINTER_VALUE (pointer), len, enc,
  349. scm_i_default_string_failed_conversion_handler ());
  350. scm_dynwind_end ();
  351. return ret;
  352. }
  353. }
  354. #undef FUNC_NAME
  355. SCM_DEFINE (scm_alignof, "alignof", 1, 0, 0, (SCM type),
  356. "Return the alignment of @var{type}, in bytes.\n\n"
  357. "@var{type} should be a valid C type, like @code{int}.\n"
  358. "Alternately @var{type} may be the symbol @code{*}, in which\n"
  359. "case the alignment of a pointer is returned. @var{type} may\n"
  360. "also be a list of types, in which case the alignment of a\n"
  361. "@code{struct} with ABI-conventional packing is returned.")
  362. #define FUNC_NAME s_scm_alignof
  363. {
  364. if (SCM_I_INUMP (type))
  365. {
  366. switch (SCM_I_INUM (type))
  367. {
  368. case SCM_FOREIGN_TYPE_FLOAT:
  369. return scm_from_size_t (alignof_type (float));
  370. case SCM_FOREIGN_TYPE_DOUBLE:
  371. return scm_from_size_t (alignof_type (double));
  372. case SCM_FOREIGN_TYPE_UINT8:
  373. return scm_from_size_t (alignof_type (scm_t_uint8));
  374. case SCM_FOREIGN_TYPE_INT8:
  375. return scm_from_size_t (alignof_type (scm_t_int8));
  376. case SCM_FOREIGN_TYPE_UINT16:
  377. return scm_from_size_t (alignof_type (scm_t_uint16));
  378. case SCM_FOREIGN_TYPE_INT16:
  379. return scm_from_size_t (alignof_type (scm_t_int16));
  380. case SCM_FOREIGN_TYPE_UINT32:
  381. return scm_from_size_t (alignof_type (scm_t_uint32));
  382. case SCM_FOREIGN_TYPE_INT32:
  383. return scm_from_size_t (alignof_type (scm_t_int32));
  384. case SCM_FOREIGN_TYPE_UINT64:
  385. return scm_from_size_t (alignof_type (scm_t_uint64));
  386. case SCM_FOREIGN_TYPE_INT64:
  387. return scm_from_size_t (alignof_type (scm_t_int64));
  388. default:
  389. scm_wrong_type_arg (FUNC_NAME, 1, type);
  390. }
  391. }
  392. else if (scm_is_eq (type, sym_asterisk))
  393. /* a pointer */
  394. return scm_from_size_t (alignof_type (void*));
  395. else if (scm_is_pair (type))
  396. {
  397. /* TYPE is a structure. Section 3-3 of the i386, x86_64, PowerPC,
  398. and SPARC P.S. of the System V ABI all say: "Aggregates
  399. (structures and arrays) and unions assume the alignment of
  400. their most strictly aligned component." */
  401. size_t max;
  402. for (max = 0; scm_is_pair (type); type = SCM_CDR (type))
  403. {
  404. size_t align;
  405. align = scm_to_size_t (scm_alignof (SCM_CAR (type)));
  406. if (align > max)
  407. max = align;
  408. }
  409. return scm_from_size_t (max);
  410. }
  411. else
  412. scm_wrong_type_arg (FUNC_NAME, 1, type);
  413. }
  414. #undef FUNC_NAME
  415. SCM_DEFINE (scm_sizeof, "sizeof", 1, 0, 0, (SCM type),
  416. "Return the size of @var{type}, in bytes.\n\n"
  417. "@var{type} should be a valid C type, like @code{int}.\n"
  418. "Alternately @var{type} may be the symbol @code{*}, in which\n"
  419. "case the size of a pointer is returned. @var{type} may also\n"
  420. "be a list of types, in which case the size of a @code{struct}\n"
  421. "with ABI-conventional packing is returned.")
  422. #define FUNC_NAME s_scm_sizeof
  423. {
  424. if (SCM_I_INUMP (type))
  425. {
  426. switch (SCM_I_INUM (type))
  427. {
  428. case SCM_FOREIGN_TYPE_FLOAT:
  429. return scm_from_size_t (sizeof (float));
  430. case SCM_FOREIGN_TYPE_DOUBLE:
  431. return scm_from_size_t (sizeof (double));
  432. case SCM_FOREIGN_TYPE_UINT8:
  433. return scm_from_size_t (sizeof (scm_t_uint8));
  434. case SCM_FOREIGN_TYPE_INT8:
  435. return scm_from_size_t (sizeof (scm_t_int8));
  436. case SCM_FOREIGN_TYPE_UINT16:
  437. return scm_from_size_t (sizeof (scm_t_uint16));
  438. case SCM_FOREIGN_TYPE_INT16:
  439. return scm_from_size_t (sizeof (scm_t_int16));
  440. case SCM_FOREIGN_TYPE_UINT32:
  441. return scm_from_size_t (sizeof (scm_t_uint32));
  442. case SCM_FOREIGN_TYPE_INT32:
  443. return scm_from_size_t (sizeof (scm_t_int32));
  444. case SCM_FOREIGN_TYPE_UINT64:
  445. return scm_from_size_t (sizeof (scm_t_uint64));
  446. case SCM_FOREIGN_TYPE_INT64:
  447. return scm_from_size_t (sizeof (scm_t_int64));
  448. default:
  449. scm_wrong_type_arg (FUNC_NAME, 1, type);
  450. }
  451. }
  452. else if (scm_is_eq (type, sym_asterisk))
  453. /* a pointer */
  454. return scm_from_size_t (sizeof (void*));
  455. else if (scm_is_pair (type))
  456. {
  457. /* a struct */
  458. size_t off = 0;
  459. size_t align = scm_to_size_t (scm_alignof(type));
  460. while (scm_is_pair (type))
  461. {
  462. off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type))));
  463. off += scm_to_size_t (scm_sizeof (scm_car (type)));
  464. type = scm_cdr (type);
  465. }
  466. return scm_from_size_t (ROUND_UP(off, align));
  467. }
  468. else
  469. scm_wrong_type_arg (FUNC_NAME, 1, type);
  470. }
  471. #undef FUNC_NAME
  472. /* return 1 on success, 0 on failure */
  473. static int
  474. parse_ffi_type (SCM type, int return_p, long *n_structs, long *n_struct_elts)
  475. {
  476. if (SCM_I_INUMP (type))
  477. {
  478. if ((SCM_I_INUM (type) < 0 )
  479. || (SCM_I_INUM (type) > SCM_FOREIGN_TYPE_LAST))
  480. return 0;
  481. else if (SCM_I_INUM (type) == SCM_FOREIGN_TYPE_VOID && !return_p)
  482. return 0;
  483. else
  484. return 1;
  485. }
  486. else if (scm_is_eq (type, sym_asterisk))
  487. /* a pointer */
  488. return 1;
  489. else
  490. {
  491. long len;
  492. len = scm_ilength (type);
  493. if (len < 1)
  494. return 0;
  495. while (len--)
  496. {
  497. if (!parse_ffi_type (scm_car (type), 0, n_structs, n_struct_elts))
  498. return 0;
  499. (*n_struct_elts)++;
  500. type = scm_cdr (type);
  501. }
  502. (*n_structs)++;
  503. return 1;
  504. }
  505. }
  506. static void
  507. fill_ffi_type (SCM type, ffi_type *ftype, ffi_type ***type_ptrs,
  508. ffi_type **types)
  509. {
  510. if (SCM_I_INUMP (type))
  511. {
  512. switch (SCM_I_INUM (type))
  513. {
  514. case SCM_FOREIGN_TYPE_FLOAT:
  515. *ftype = ffi_type_float;
  516. return;
  517. case SCM_FOREIGN_TYPE_DOUBLE:
  518. *ftype = ffi_type_double;
  519. return;
  520. case SCM_FOREIGN_TYPE_UINT8:
  521. *ftype = ffi_type_uint8;
  522. return;
  523. case SCM_FOREIGN_TYPE_INT8:
  524. *ftype = ffi_type_sint8;
  525. return;
  526. case SCM_FOREIGN_TYPE_UINT16:
  527. *ftype = ffi_type_uint16;
  528. return;
  529. case SCM_FOREIGN_TYPE_INT16:
  530. *ftype = ffi_type_sint16;
  531. return;
  532. case SCM_FOREIGN_TYPE_UINT32:
  533. *ftype = ffi_type_uint32;
  534. return;
  535. case SCM_FOREIGN_TYPE_INT32:
  536. *ftype = ffi_type_sint32;
  537. return;
  538. case SCM_FOREIGN_TYPE_UINT64:
  539. *ftype = ffi_type_uint64;
  540. return;
  541. case SCM_FOREIGN_TYPE_INT64:
  542. *ftype = ffi_type_sint64;
  543. return;
  544. case SCM_FOREIGN_TYPE_VOID:
  545. *ftype = ffi_type_void;
  546. return;
  547. default:
  548. scm_wrong_type_arg_msg ("pointer->procedure", 0, type,
  549. "foreign type");
  550. }
  551. }
  552. else if (scm_is_eq (type, sym_asterisk))
  553. /* a pointer */
  554. {
  555. *ftype = ffi_type_pointer;
  556. return;
  557. }
  558. else
  559. {
  560. long i, len;
  561. len = scm_ilength (type);
  562. ftype->size = 0;
  563. ftype->alignment = 0;
  564. ftype->type = FFI_TYPE_STRUCT;
  565. ftype->elements = *type_ptrs;
  566. *type_ptrs += len + 1;
  567. for (i = 0; i < len; i++)
  568. {
  569. ftype->elements[i] = *types;
  570. *types += 1;
  571. fill_ffi_type (scm_car (type), ftype->elements[i],
  572. type_ptrs, types);
  573. type = scm_cdr (type);
  574. }
  575. ftype->elements[i] = NULL;
  576. }
  577. }
  578. /* Return a "cif" (call interface) for the given RETURN_TYPE and
  579. ARG_TYPES. */
  580. static ffi_cif *
  581. make_cif (SCM return_type, SCM arg_types, const char *caller)
  582. #define FUNC_NAME caller
  583. {
  584. SCM walk;
  585. long i, nargs, n_structs, n_struct_elts;
  586. size_t cif_len;
  587. char *mem;
  588. ffi_cif *cif;
  589. ffi_type **type_ptrs;
  590. ffi_type *types;
  591. nargs = scm_ilength (arg_types);
  592. SCM_ASSERT (nargs >= 0, arg_types, 3, FUNC_NAME);
  593. /* fixme: assert nargs < 1<<32 */
  594. n_structs = n_struct_elts = 0;
  595. /* For want of talloc, we're going to have to do this in two passes: first we
  596. figure out how much memory is needed for all types, then we allocate the
  597. cif and the types all in one block. */
  598. if (!parse_ffi_type (return_type, 1, &n_structs, &n_struct_elts))
  599. scm_wrong_type_arg (FUNC_NAME, 1, return_type);
  600. for (walk = arg_types; scm_is_pair (walk); walk = scm_cdr (walk))
  601. if (!parse_ffi_type (scm_car (walk), 0, &n_structs, &n_struct_elts))
  602. scm_wrong_type_arg (FUNC_NAME, 3, scm_car (walk));
  603. /* the memory: with space for the cif itself */
  604. cif_len = sizeof (ffi_cif);
  605. /* then ffi_type pointers: one for each arg, one for each struct
  606. element, and one for each struct (for null-termination) */
  607. cif_len = (ROUND_UP (cif_len, alignof_type (void *))
  608. + (nargs + n_structs + n_struct_elts)*sizeof(void*));
  609. /* then the ffi_type structs themselves, one per arg and struct element, and
  610. one for the return val */
  611. cif_len = (ROUND_UP (cif_len, alignof_type (ffi_type))
  612. + (nargs + n_struct_elts + 1)*sizeof(ffi_type));
  613. mem = scm_gc_malloc_pointerless (cif_len, "foreign");
  614. /* ensure all the memory is initialized, even the holes */
  615. memset (mem, 0, cif_len);
  616. cif = (ffi_cif *) mem;
  617. /* reuse cif_len to walk through the mem */
  618. cif_len = ROUND_UP (sizeof (ffi_cif), alignof_type (void *));
  619. type_ptrs = (ffi_type**)(mem + cif_len);
  620. cif_len = ROUND_UP (cif_len
  621. + (nargs + n_structs + n_struct_elts)*sizeof(void*),
  622. alignof_type (ffi_type));
  623. types = (ffi_type*)(mem + cif_len);
  624. /* whew. now knit the pointers together. */
  625. cif->rtype = types++;
  626. fill_ffi_type (return_type, cif->rtype, &type_ptrs, &types);
  627. cif->arg_types = type_ptrs;
  628. type_ptrs += nargs;
  629. for (walk = arg_types, i = 0; scm_is_pair (walk); walk = scm_cdr (walk), i++)
  630. {
  631. cif->arg_types[i] = types++;
  632. fill_ffi_type (scm_car (walk), cif->arg_types[i], &type_ptrs, &types);
  633. }
  634. /* round out the cif, and we're done. */
  635. cif->abi = FFI_DEFAULT_ABI;
  636. cif->nargs = nargs;
  637. cif->bytes = 0;
  638. cif->flags = 0;
  639. if (FFI_OK != ffi_prep_cif (cif, FFI_DEFAULT_ABI, cif->nargs, cif->rtype,
  640. cif->arg_types))
  641. SCM_MISC_ERROR ("ffi_prep_cif failed", SCM_EOL);
  642. return cif;
  643. }
  644. #undef FUNC_NAME
  645. static SCM
  646. pointer_to_procedure (SCM return_type, SCM func_ptr, SCM arg_types,
  647. int with_errno)
  648. #define FUNC_NAME "pointer->procedure"
  649. {
  650. ffi_cif *cif;
  651. SCM_VALIDATE_POINTER (2, func_ptr);
  652. cif = make_cif (return_type, arg_types, FUNC_NAME);
  653. return cif_to_procedure (scm_from_pointer (cif, NULL), func_ptr,
  654. with_errno);
  655. }
  656. #undef FUNC_NAME
  657. SCM
  658. scm_pointer_to_procedure (SCM return_type, SCM func_ptr, SCM arg_types)
  659. {
  660. return pointer_to_procedure (return_type, func_ptr, arg_types, 0);
  661. }
  662. SCM
  663. scm_pointer_to_procedure_with_errno (SCM return_type, SCM func_ptr,
  664. SCM arg_types)
  665. {
  666. return pointer_to_procedure (return_type, func_ptr, arg_types, 1);
  667. }
  668. SCM_KEYWORD (k_return_errno, "return-errno?");
  669. SCM_INTERNAL SCM scm_i_pointer_to_procedure (SCM, SCM, SCM, SCM);
  670. SCM_DEFINE (scm_i_pointer_to_procedure, "pointer->procedure", 3, 0, 1,
  671. (SCM return_type, SCM func_ptr, SCM arg_types, SCM keyword_args),
  672. "Make a foreign function.\n\n"
  673. "Given the foreign void pointer @var{func_ptr}, its argument and\n"
  674. "return types @var{arg_types} and @var{return_type}, return a\n"
  675. "procedure that will pass arguments to the foreign function\n"
  676. "and return appropriate values.\n\n"
  677. "@var{arg_types} should be a list of foreign types.\n"
  678. "@code{return_type} should be a foreign type.\n"
  679. "If the @code{#:return-errno?} keyword argument is provided and\n"
  680. "its value is true, then the returned procedure will return two\n"
  681. "values, with @code{errno} as the second value.")
  682. #define FUNC_NAME s_scm_i_pointer_to_procedure
  683. {
  684. SCM return_errno = SCM_BOOL_F;
  685. scm_c_bind_keyword_arguments (FUNC_NAME, keyword_args, 0,
  686. k_return_errno, &return_errno,
  687. SCM_UNDEFINED);
  688. return pointer_to_procedure (return_type, func_ptr, arg_types,
  689. scm_to_bool (return_errno));
  690. }
  691. #undef FUNC_NAME
  692. static const scm_t_uint32 *
  693. get_foreign_stub_code (unsigned int nargs, int with_errno)
  694. {
  695. size_t i;
  696. size_t code_len = with_errno ? 4 : 5;
  697. scm_t_uint32 *code;
  698. code = scm_gc_malloc_pointerless (code_len * sizeof (scm_t_uint32),
  699. "foreign code");
  700. if (nargs >= (1 << 24) + 1)
  701. scm_misc_error ("make-foreign-function", "too many arguments: ~a",
  702. scm_list_1 (scm_from_uint (nargs)));
  703. i = 0;
  704. code[i++] = SCM_PACK_OP_24 (assert_nargs_ee, nargs + 1);
  705. code[i++] = SCM_PACK_OP_12_12 (foreign_call, 0, 1);
  706. code[i++] = SCM_PACK_OP_24 (handle_interrupts, 0);
  707. if (!with_errno)
  708. code[i++] = SCM_PACK_OP_24 (reset_frame, 2);
  709. code[i++] = SCM_PACK_OP_24 (return_values, 0);
  710. return code;
  711. }
  712. static SCM
  713. cif_to_procedure (SCM cif, SCM func_ptr, int with_errno)
  714. {
  715. ffi_cif *c_cif;
  716. SCM ret;
  717. scm_t_bits nfree = 2;
  718. scm_t_bits flags = SCM_F_PROGRAM_IS_FOREIGN;
  719. c_cif = (ffi_cif *) SCM_POINTER_VALUE (cif);
  720. ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
  721. SCM_SET_CELL_WORD_1 (ret, get_foreign_stub_code (c_cif->nargs, with_errno));
  722. SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, cif);
  723. SCM_PROGRAM_FREE_VARIABLE_SET (ret, 1, func_ptr);
  724. return ret;
  725. }
  726. /* Set *LOC to the foreign representation of X with TYPE. */
  727. static void
  728. unpack (const ffi_type *type, void *loc, SCM x, int return_value_p)
  729. #define FUNC_NAME "scm_i_foreign_call"
  730. {
  731. switch (type->type)
  732. {
  733. case FFI_TYPE_FLOAT:
  734. *(float *) loc = scm_to_double (x);
  735. break;
  736. case FFI_TYPE_DOUBLE:
  737. *(double *) loc = scm_to_double (x);
  738. break;
  739. /* For integer return values smaller than `int', libffi expects the
  740. result in an `ffi_arg'-long buffer. */
  741. case FFI_TYPE_UINT8:
  742. if (return_value_p)
  743. *(ffi_arg *) loc = scm_to_uint8 (x);
  744. else
  745. *(scm_t_uint8 *) loc = scm_to_uint8 (x);
  746. break;
  747. case FFI_TYPE_SINT8:
  748. if (return_value_p)
  749. *(ffi_arg *) loc = scm_to_int8 (x);
  750. else
  751. *(scm_t_int8 *) loc = scm_to_int8 (x);
  752. break;
  753. case FFI_TYPE_UINT16:
  754. if (return_value_p)
  755. *(ffi_arg *) loc = scm_to_uint16 (x);
  756. else
  757. *(scm_t_uint16 *) loc = scm_to_uint16 (x);
  758. break;
  759. case FFI_TYPE_SINT16:
  760. if (return_value_p)
  761. *(ffi_arg *) loc = scm_to_int16 (x);
  762. else
  763. *(scm_t_int16 *) loc = scm_to_int16 (x);
  764. break;
  765. case FFI_TYPE_UINT32:
  766. if (return_value_p)
  767. *(ffi_arg *) loc = scm_to_uint32 (x);
  768. else
  769. *(scm_t_uint32 *) loc = scm_to_uint32 (x);
  770. break;
  771. case FFI_TYPE_SINT32:
  772. if (return_value_p)
  773. *(ffi_arg *) loc = scm_to_int32 (x);
  774. else
  775. *(scm_t_int32 *) loc = scm_to_int32 (x);
  776. break;
  777. case FFI_TYPE_UINT64:
  778. *(scm_t_uint64 *) loc = scm_to_uint64 (x);
  779. break;
  780. case FFI_TYPE_SINT64:
  781. *(scm_t_int64 *) loc = scm_to_int64 (x);
  782. break;
  783. case FFI_TYPE_STRUCT:
  784. SCM_VALIDATE_POINTER (1, x);
  785. memcpy (loc, SCM_POINTER_VALUE (x), type->size);
  786. break;
  787. case FFI_TYPE_POINTER:
  788. SCM_VALIDATE_POINTER (1, x);
  789. *(void **) loc = SCM_POINTER_VALUE (x);
  790. break;
  791. case FFI_TYPE_VOID:
  792. /* Do nothing. */
  793. break;
  794. default:
  795. abort ();
  796. }
  797. }
  798. #undef FUNC_NAME
  799. /* Return a Scheme representation of the foreign value at LOC of type
  800. TYPE. When RETURN_VALUE_P is true, LOC is assumed to point to a
  801. return value buffer; otherwise LOC is assumed to point to an
  802. argument buffer. */
  803. static SCM
  804. pack (const ffi_type * type, const void *loc, int return_value_p)
  805. {
  806. switch (type->type)
  807. {
  808. case FFI_TYPE_VOID:
  809. return SCM_UNSPECIFIED;
  810. case FFI_TYPE_FLOAT:
  811. return scm_from_double (*(float *) loc);
  812. case FFI_TYPE_DOUBLE:
  813. return scm_from_double (*(double *) loc);
  814. /* For integer return values smaller than `int', libffi stores the
  815. result in an `ffi_arg'-long buffer, of which only the
  816. significant bits must be kept---hence the pair of casts below.
  817. See <http://thread.gmane.org/gmane.comp.lib.ffi.general/406>
  818. for details. */
  819. case FFI_TYPE_UINT8:
  820. if (return_value_p)
  821. return scm_from_uint8 ((scm_t_uint8) *(ffi_arg *) loc);
  822. else
  823. return scm_from_uint8 (* (scm_t_uint8 *) loc);
  824. case FFI_TYPE_SINT8:
  825. if (return_value_p)
  826. return scm_from_int8 ((scm_t_int8) *(ffi_arg *) loc);
  827. else
  828. return scm_from_int8 (* (scm_t_int8 *) loc);
  829. case FFI_TYPE_UINT16:
  830. if (return_value_p)
  831. return scm_from_uint16 ((scm_t_uint16) *(ffi_arg *) loc);
  832. else
  833. return scm_from_uint16 (* (scm_t_uint16 *) loc);
  834. case FFI_TYPE_SINT16:
  835. if (return_value_p)
  836. return scm_from_int16 ((scm_t_int16) *(ffi_arg *) loc);
  837. else
  838. return scm_from_int16 (* (scm_t_int16 *) loc);
  839. case FFI_TYPE_UINT32:
  840. if (return_value_p)
  841. return scm_from_uint32 ((scm_t_uint32) *(ffi_arg *) loc);
  842. else
  843. return scm_from_uint32 (* (scm_t_uint32 *) loc);
  844. case FFI_TYPE_SINT32:
  845. if (return_value_p)
  846. return scm_from_int32 ((scm_t_int32) *(ffi_arg *) loc);
  847. else
  848. return scm_from_int32 (* (scm_t_int32 *) loc);
  849. case FFI_TYPE_UINT64:
  850. return scm_from_uint64 (*(scm_t_uint64 *) loc);
  851. case FFI_TYPE_SINT64:
  852. return scm_from_int64 (*(scm_t_int64 *) loc);
  853. case FFI_TYPE_STRUCT:
  854. {
  855. void *mem = scm_gc_malloc_pointerless (type->size, "foreign");
  856. memcpy (mem, loc, type->size);
  857. return scm_from_pointer (mem, NULL);
  858. }
  859. case FFI_TYPE_POINTER:
  860. return scm_from_pointer (*(void **) loc, NULL);
  861. default:
  862. abort ();
  863. }
  864. }
  865. SCM
  866. scm_i_foreign_call (SCM cif_scm, SCM pointer_scm, int *errno_ret,
  867. const union scm_vm_stack_element *argv)
  868. {
  869. /* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
  870. objtable. */
  871. ffi_cif *cif;
  872. void (*func) (void);
  873. scm_t_uint8 *data;
  874. void *rvalue;
  875. void **args;
  876. unsigned i;
  877. size_t arg_size;
  878. scm_t_ptrdiff off;
  879. cif = SCM_POINTER_VALUE (cif_scm);
  880. func = SCM_POINTER_VALUE (pointer_scm);
  881. /* Argument pointers. */
  882. args = alloca (sizeof (void *) * cif->nargs);
  883. /* Compute the worst-case amount of memory needed to store all the argument
  884. values. Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero,
  885. so it can't be used for that purpose. */
  886. for (i = 0, arg_size = 0; i < cif->nargs; i++)
  887. arg_size += cif->arg_types[i]->size + cif->arg_types[i]->alignment - 1;
  888. /* Space for argument values, followed by return value. */
  889. data = alloca (arg_size + cif->rtype->size
  890. + max (sizeof (void *), cif->rtype->alignment));
  891. /* Unpack ARGV to native values, setting ARGV pointers. */
  892. for (i = 0, off = 0;
  893. i < cif->nargs;
  894. off = (scm_t_uint8 *) args[i] - data + cif->arg_types[i]->size,
  895. i++)
  896. {
  897. /* Suitably align the storage area for argument I. */
  898. args[i] = (void *) ROUND_UP ((scm_t_uintptr) data + off,
  899. cif->arg_types[i]->alignment);
  900. assert ((scm_t_uintptr) args[i] % cif->arg_types[i]->alignment == 0);
  901. unpack (cif->arg_types[i], args[i], argv[cif->nargs - i - 1].as_scm, 0);
  902. }
  903. /* Prepare space for the return value. On some platforms, such as
  904. `armv5tel-*-linux-gnueabi', the return value has to be at least
  905. word-aligned, even if its type doesn't have any alignment requirement as is
  906. the case with `char'. */
  907. rvalue = (void *) ROUND_UP ((scm_t_uintptr) data + off,
  908. max (sizeof (void *), cif->rtype->alignment));
  909. /* off we go! */
  910. errno = 0;
  911. ffi_call (cif, func, rvalue, args);
  912. *errno_ret = errno;
  913. return pack (cif->rtype, rvalue, 1);
  914. }
  915. /* Function pointers aka. "callbacks" or "closures". */
  916. #ifdef FFI_CLOSURES
  917. /* Trampoline to invoke a libffi closure that wraps a Scheme
  918. procedure. */
  919. static void
  920. invoke_closure (ffi_cif *cif, void *ret, void **args, void *data)
  921. {
  922. size_t i;
  923. SCM proc, *argv, result;
  924. proc = SCM_PACK_POINTER (data);
  925. argv = alloca (cif->nargs * sizeof (*argv));
  926. /* Pack ARGS to SCM values, setting ARGV pointers. */
  927. for (i = 0; i < cif->nargs; i++)
  928. argv[i] = pack (cif->arg_types[i], args[i], 0);
  929. result = scm_call_n (proc, argv, cif->nargs);
  930. unpack (cif->rtype, ret, result, 1);
  931. }
  932. SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0,
  933. (SCM return_type, SCM proc, SCM arg_types),
  934. "Return a pointer to a C function of type @var{return_type}\n"
  935. "taking arguments of types @var{arg_types} (a list) and\n"
  936. "behaving as a proxy to procedure @var{proc}. Thus\n"
  937. "@var{proc}'s arity, supported argument types, and return\n"
  938. "type should match @var{return_type} and @var{arg_types}.\n")
  939. #define FUNC_NAME s_scm_procedure_to_pointer
  940. {
  941. SCM cif_pointer, pointer;
  942. ffi_cif *cif;
  943. ffi_status err;
  944. void *closure, *executable;
  945. cif = make_cif (return_type, arg_types, FUNC_NAME);
  946. closure = ffi_closure_alloc (sizeof (ffi_closure), &executable);
  947. err = ffi_prep_closure_loc ((ffi_closure *) closure, cif,
  948. invoke_closure, SCM_UNPACK_POINTER (proc),
  949. executable);
  950. if (err != FFI_OK)
  951. {
  952. ffi_closure_free (closure);
  953. SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL);
  954. }
  955. /* CIF points to GC-managed memory and it should remain as long as
  956. POINTER (see below) is live. Wrap it in a Scheme pointer to then
  957. hold a weak reference on it. */
  958. cif_pointer = scm_from_pointer (cif, NULL);
  959. if (closure == executable)
  960. {
  961. pointer = scm_from_pointer (executable, ffi_closure_free);
  962. register_weak_reference (pointer,
  963. scm_list_2 (proc, cif_pointer));
  964. }
  965. else
  966. {
  967. /* CLOSURE needs to be freed eventually. However, since
  968. `GC_all_interior_pointers' is disabled, we can't just register
  969. a finalizer for CLOSURE. Instead, we create a pointer object
  970. for CLOSURE, with a finalizer, and register it as a weak
  971. reference of POINTER. */
  972. SCM friend;
  973. pointer = scm_from_pointer (executable, NULL);
  974. friend = scm_from_pointer (closure, ffi_closure_free);
  975. register_weak_reference (pointer,
  976. scm_list_3 (proc, cif_pointer, friend));
  977. }
  978. return pointer;
  979. }
  980. #undef FUNC_NAME
  981. #endif /* FFI_CLOSURES */
  982. static void
  983. scm_init_foreign (void)
  984. {
  985. #ifndef SCM_MAGIC_SNARFER
  986. #include "libguile/foreign.x"
  987. #endif
  988. scm_define (sym_void, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID));
  989. scm_define (sym_float, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT));
  990. scm_define (sym_double, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE));
  991. scm_define (sym_uint8, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8));
  992. scm_define (sym_int8, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8));
  993. scm_define (sym_uint16, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16));
  994. scm_define (sym_int16, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16));
  995. scm_define (sym_uint32, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32));
  996. scm_define (sym_int32, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32));
  997. scm_define (sym_uint64, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64));
  998. scm_define (sym_int64, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64));
  999. scm_define (sym_short,
  1000. #if SIZEOF_SHORT == 8
  1001. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1002. #elif SIZEOF_SHORT == 4
  1003. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1004. #elif SIZEOF_SHORT == 2
  1005. scm_from_uint8 (SCM_FOREIGN_TYPE_INT16)
  1006. #else
  1007. # error unsupported sizeof (short)
  1008. #endif
  1009. );
  1010. scm_define (sym_unsigned_short,
  1011. #if SIZEOF_SHORT == 8
  1012. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1013. #elif SIZEOF_SHORT == 4
  1014. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1015. #elif SIZEOF_SHORT == 2
  1016. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16)
  1017. #else
  1018. # error unsupported sizeof (short)
  1019. #endif
  1020. );
  1021. scm_define (sym_int,
  1022. #if SIZEOF_INT == 8
  1023. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1024. #elif SIZEOF_INT == 4
  1025. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1026. #else
  1027. # error unsupported sizeof (int)
  1028. #endif
  1029. );
  1030. scm_define (sym_unsigned_int,
  1031. #if SIZEOF_UNSIGNED_INT == 8
  1032. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1033. #elif SIZEOF_UNSIGNED_INT == 4
  1034. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1035. #else
  1036. # error unsupported sizeof (unsigned int)
  1037. #endif
  1038. );
  1039. scm_define (sym_long,
  1040. #if SIZEOF_LONG == 8
  1041. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1042. #elif SIZEOF_LONG == 4
  1043. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1044. #else
  1045. # error unsupported sizeof (long)
  1046. #endif
  1047. );
  1048. scm_define (sym_unsigned_long,
  1049. #if SIZEOF_UNSIGNED_LONG == 8
  1050. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1051. #elif SIZEOF_UNSIGNED_LONG == 4
  1052. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1053. #else
  1054. # error unsupported sizeof (unsigned long)
  1055. #endif
  1056. );
  1057. scm_define (sym_size_t,
  1058. #if SIZEOF_SIZE_T == 8
  1059. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1060. #elif SIZEOF_SIZE_T == 4
  1061. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1062. #else
  1063. # error unsupported sizeof (size_t)
  1064. #endif
  1065. );
  1066. scm_define (sym_ssize_t,
  1067. #if SIZEOF_SIZE_T == 8
  1068. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1069. #elif SIZEOF_SIZE_T == 4
  1070. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1071. #else
  1072. # error unsupported sizeof (ssize_t)
  1073. #endif
  1074. );
  1075. scm_define (sym_ptrdiff_t,
  1076. #if SCM_SIZEOF_SCM_T_PTRDIFF == 8
  1077. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1078. #elif SCM_SIZEOF_SCM_T_PTRDIFF == 4
  1079. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1080. #else
  1081. # error unsupported sizeof (scm_t_ptrdiff)
  1082. #endif
  1083. );
  1084. scm_define (sym_intptr_t,
  1085. #if SCM_SIZEOF_INTPTR_T == 8
  1086. scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
  1087. #elif SCM_SIZEOF_INTPTR_T == 4
  1088. scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
  1089. #else
  1090. # error unsupported sizeof (scm_t_intptr)
  1091. #endif
  1092. );
  1093. scm_define (sym_uintptr_t,
  1094. #if SCM_SIZEOF_UINTPTR_T == 8
  1095. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
  1096. #elif SCM_SIZEOF_UINTPTR_T == 4
  1097. scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
  1098. #else
  1099. # error unsupported sizeof (scm_t_uintptr)
  1100. #endif
  1101. );
  1102. null_pointer = scm_cell (scm_tc7_pointer, 0);
  1103. scm_define (sym_null, null_pointer);
  1104. }
  1105. void
  1106. scm_register_foreign (void)
  1107. {
  1108. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1109. "scm_init_foreign",
  1110. (scm_t_extension_init_func)scm_init_foreign,
  1111. NULL);
  1112. pointer_weak_refs = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  1113. }
  1114. /*
  1115. Local Variables:
  1116. c-file-style: "gnu"
  1117. End:
  1118. */