foreign.c 38 KB

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