smob.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2009, 2010, 2011, 2012 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. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/async.h"
  26. #include "libguile/goops.h"
  27. #include "libguile/instructions.h"
  28. #include "libguile/objcodes.h"
  29. #include "libguile/programs.h"
  30. #include "libguile/smob.h"
  31. #include "libguile/bdw-gc.h"
  32. #include <gc/gc_mark.h>
  33. /* scm_smobs scm_numsmob
  34. * implement a fixed sized array of smob records.
  35. * Indexes into this table are used when generating type
  36. * tags for smobjects (if you know a tag you can get an index and conversely).
  37. */
  38. #define MAX_SMOB_COUNT SCM_I_MAX_SMOB_TYPE_COUNT
  39. long scm_numsmob;
  40. scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
  41. void
  42. scm_assert_smob_type (scm_t_bits tag, SCM val)
  43. {
  44. if (!SCM_SMOB_PREDICATE (tag, val))
  45. scm_wrong_type_arg_msg (NULL, 0, val, scm_smobs[SCM_TC2SMOBNUM(tag)].name);
  46. }
  47. /* {Mark}
  48. */
  49. /* This function is vestigial. It used to be the mark function's
  50. responsibility to set the mark bit on the smob or port, but now the
  51. generic marking routine in gc.c takes care of that, and a zero
  52. pointer for a mark function means "don't bother". So you never
  53. need scm_mark0.
  54. However, we leave it here because it's harmless to call it, and
  55. people out there have smob code that uses it, and there's no reason
  56. to make their links fail. */
  57. SCM
  58. scm_mark0 (SCM ptr SCM_UNUSED)
  59. {
  60. return SCM_BOOL_F;
  61. }
  62. SCM
  63. /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
  64. be used for real pairs. */
  65. scm_markcdr (SCM ptr)
  66. {
  67. return SCM_CELL_OBJECT_1 (ptr);
  68. }
  69. /* {Free}
  70. */
  71. size_t
  72. scm_free0 (SCM ptr SCM_UNUSED)
  73. {
  74. return 0;
  75. }
  76. /* {Print}
  77. */
  78. int
  79. scm_smob_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  80. {
  81. long n = SCM_SMOBNUM (exp);
  82. scm_puts_unlocked ("#<", port);
  83. scm_puts_unlocked (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
  84. scm_putc_unlocked (' ', port);
  85. if (scm_smobs[n].size)
  86. scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
  87. else
  88. scm_uintprint (SCM_UNPACK (exp), 16, port);
  89. scm_putc_unlocked ('>', port);
  90. return 1;
  91. }
  92. /* {Apply}
  93. */
  94. #ifdef WORDS_BIGENDIAN
  95. #define OBJCODE_HEADER 0, 0, 0, 16, 0, 0, 0, 40
  96. #define META_HEADER 0, 0, 0, 32, 0, 0, 0, 0
  97. #else
  98. #define OBJCODE_HEADER 16, 0, 0, 0, 40, 0, 0, 0
  99. #define META_HEADER 32, 0, 0, 0, 0, 0, 0, 0
  100. #endif
  101. /* This code is the same as in gsubr.c, except we use smob_call instead of
  102. struct_call. */
  103. /* A: req; B: opt; C: rest */
  104. #define A(nreq) \
  105. OBJCODE_HEADER, \
  106. /* 0 */ scm_op_assert_nargs_ee, 0, nreq, /* assert number of args */ \
  107. /* 3 */ scm_op_object_ref, 0, /* push the foreign object wrapping the subr pointer */ \
  108. /* 5 */ scm_op_smob_call, nreq, /* and call (will return value as well) */ \
  109. /* 7 */ scm_op_nop, \
  110. /* 8 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, \
  111. /* 12 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, \
  112. /* 16 */ META (3, 7, nreq, 0, 0)
  113. #define B(nopt) \
  114. OBJCODE_HEADER, \
  115. /* 0 */ scm_op_bind_optionals, 0, nopt, /* bind optionals */ \
  116. /* 3 */ scm_op_assert_nargs_ee, 0, nopt, /* assert number of args */ \
  117. /* 6 */ scm_op_object_ref, 0, /* push the foreign object wrapping the smob pointer */ \
  118. /* 8 */ scm_op_smob_call, nopt, /* and call (will return value as well) */ \
  119. /* 10 */ scm_op_nop, scm_op_nop, \
  120. /* 12 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, \
  121. /* 16 */ META (6, 10, 0, nopt, 0)
  122. #define C() \
  123. OBJCODE_HEADER, \
  124. /* 0 */ scm_op_push_rest, 0, 0, /* cons all args into a list */ \
  125. /* 3 */ scm_op_object_ref, 0, /* push the foreign object wrapping the smob pointer */ \
  126. /* 5 */ scm_op_smob_call, 1, /* and call (will return value as well) */ \
  127. /* 7 */ scm_op_nop, \
  128. /* 8 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, \
  129. /* 12 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, \
  130. /* 16 */ META (3, 7, 0, 0, 1)
  131. #define AB(nreq, nopt) \
  132. OBJCODE_HEADER, \
  133. /* 0 */ scm_op_assert_nargs_ge, 0, nreq, /* assert number of args */ \
  134. /* 3 */ scm_op_bind_optionals, 0, nreq+nopt, /* bind optionals */ \
  135. /* 6 */ scm_op_assert_nargs_ee, 0, nreq+nopt, /* assert number of args */ \
  136. /* 9 */ scm_op_object_ref, 0, /* push the foreign object wrapping the smob pointer */ \
  137. /* 11 */ scm_op_smob_call, nreq+nopt, /* and call (will return value as well) */ \
  138. /* 13 */ scm_op_nop, scm_op_nop, scm_op_nop, \
  139. /* 16 */ META (9, 13, nreq, nopt, 0)
  140. #define AC(nreq) \
  141. OBJCODE_HEADER, \
  142. /* 0 */ scm_op_assert_nargs_ge, 0, nreq, /* assert number of args */ \
  143. /* 3 */ scm_op_push_rest, 0, nreq, /* cons rest list */ \
  144. /* 6 */ scm_op_object_ref, 0, /* push the foreign object wrapping the smob pointer */ \
  145. /* 8 */ scm_op_smob_call, nreq+1, /* and call (will return value as well) */ \
  146. /* 10 */ scm_op_nop, scm_op_nop, \
  147. /* 12 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, \
  148. /* 16 */ META (6, 10, nreq, 0, 1)
  149. #define BC(nopt) \
  150. OBJCODE_HEADER, \
  151. /* 0 */ scm_op_bind_optionals, 0, nopt, /* bind optionals */ \
  152. /* 3 */ scm_op_push_rest, 0, nopt, /* cons rest list */ \
  153. /* 6 */ scm_op_object_ref, 0, /* push the foreign object wrapping the smob pointer */ \
  154. /* 8 */ scm_op_smob_call, nopt+1, /* and call (will return value as well) */ \
  155. /* 10 */ scm_op_nop, scm_op_nop, \
  156. /* 12 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, \
  157. /* 16 */ META (6, 10, 0, nopt, 1)
  158. #define ABC(nreq, nopt) \
  159. OBJCODE_HEADER, \
  160. /* 0 */ scm_op_assert_nargs_ge, 0, nreq, /* assert number of args */ \
  161. /* 3 */ scm_op_bind_optionals, 0, nreq+nopt, /* bind optionals */ \
  162. /* 6 */ scm_op_push_rest, 0, nreq+nopt, /* cons rest list */ \
  163. /* 9 */ scm_op_object_ref, 0, /* push the foreign object wrapping the smob pointer */ \
  164. /* 11 */ scm_op_smob_call, nreq+nopt+1, /* and call (will return value as well) */ \
  165. /* 13 */ scm_op_nop, scm_op_nop, scm_op_nop, \
  166. /* 16 */ META (9, 13, nreq, nopt, 1)
  167. #define META(start, end, nreq, nopt, rest) \
  168. META_HEADER, \
  169. /* 0 */ scm_op_make_eol, /* bindings */ \
  170. /* 1 */ scm_op_make_eol, /* sources */ \
  171. /* 2 */ scm_op_make_int8, start, scm_op_make_int8, end, /* arity: from ip N to ip N */ \
  172. /* 6 */ scm_op_make_int8, nreq, /* the arity is N required args */ \
  173. /* 8 */ scm_op_make_int8, nopt, /* N optionals */ \
  174. /* 10 */ rest ? scm_op_make_true : scm_op_make_false, /* maybe a rest arg */ \
  175. /* 11 */ scm_op_list, 0, 5, /* make a list of those 5 vals */ \
  176. /* 14 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */ \
  177. /* 17 */ scm_op_load_symbol, 0, 0, 4, 'n', 'a', 'm', 'e', /* `name' */ \
  178. /* 25 */ scm_op_object_ref, 1, /* the name from the object table */ \
  179. /* 27 */ scm_op_cons, /* make a pair for the properties */ \
  180. /* 28 */ scm_op_list, 0, 4, /* pack bindings, sources, and arities into list */ \
  181. /* 31 */ scm_op_return /* and return */ \
  182. /* 32 */
  183. static const struct
  184. {
  185. scm_t_uint64 dummy; /* ensure 8-byte alignment; perhaps there's a better way */
  186. const scm_t_uint8 bytes[16 * (sizeof (struct scm_objcode) + 16
  187. + sizeof (struct scm_objcode) + 32)];
  188. } raw_bytecode = {
  189. 0,
  190. {
  191. /* Use the elisp macros from gsubr.c */
  192. /* C-u 3 M-x generate-bytecodes RET */
  193. /* 0 arguments */
  194. A(0),
  195. /* 1 arguments */
  196. A(1), B(1), C(),
  197. /* 2 arguments */
  198. A(2), AB(1,1), B(2), AC(1), BC(1),
  199. /* 3 arguments */
  200. A(3), AB(2,1), AB(1,2), B(3), AC(2), ABC(1,1), BC(2)
  201. }
  202. };
  203. #undef A
  204. #undef B
  205. #undef C
  206. #undef AB
  207. #undef AC
  208. #undef BC
  209. #undef ABC
  210. #undef OBJCODE_HEADER
  211. #undef META_HEADER
  212. #undef META
  213. #define STATIC_OBJCODE_TAG \
  214. SCM_PACK (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0))
  215. static const struct
  216. {
  217. scm_t_uint64 dummy; /* alignment */
  218. scm_t_cell cells[16 * 2]; /* 4*4 double cells */
  219. } objcode_cells = {
  220. 0,
  221. /* C-u 3 M-x generate-objcode-cells RET */
  222. {
  223. /* 0 arguments */
  224. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 0) },
  225. { SCM_BOOL_F, SCM_PACK (0) },
  226. /* 1 arguments */
  227. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 64) },
  228. { SCM_BOOL_F, SCM_PACK (0) },
  229. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 128) },
  230. { SCM_BOOL_F, SCM_PACK (0) },
  231. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 192) },
  232. { SCM_BOOL_F, SCM_PACK (0) },
  233. /* 2 arguments */
  234. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 256) },
  235. { SCM_BOOL_F, SCM_PACK (0) },
  236. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 320) },
  237. { SCM_BOOL_F, SCM_PACK (0) },
  238. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 384) },
  239. { SCM_BOOL_F, SCM_PACK (0) },
  240. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 448) },
  241. { SCM_BOOL_F, SCM_PACK (0) },
  242. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 512) },
  243. { SCM_BOOL_F, SCM_PACK (0) },
  244. /* 3 arguments */
  245. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 576) },
  246. { SCM_BOOL_F, SCM_PACK (0) },
  247. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 640) },
  248. { SCM_BOOL_F, SCM_PACK (0) },
  249. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 704) },
  250. { SCM_BOOL_F, SCM_PACK (0) },
  251. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 768) },
  252. { SCM_BOOL_F, SCM_PACK (0) },
  253. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 832) },
  254. { SCM_BOOL_F, SCM_PACK (0) },
  255. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 896) },
  256. { SCM_BOOL_F, SCM_PACK (0) },
  257. { STATIC_OBJCODE_TAG, SCM_PACK (raw_bytecode.bytes + 960) },
  258. { SCM_BOOL_F, SCM_PACK (0) }
  259. }
  260. };
  261. static const SCM scm_smob_objcode_trampolines[16] = {
  262. /* C-u 3 M-x generate-objcodes RET */
  263. /* 0 arguments */
  264. SCM_PACK (objcode_cells.cells+0),
  265. /* 1 arguments */
  266. SCM_PACK (objcode_cells.cells+2),
  267. SCM_PACK (objcode_cells.cells+4),
  268. SCM_PACK (objcode_cells.cells+6),
  269. /* 2 arguments */
  270. SCM_PACK (objcode_cells.cells+8),
  271. SCM_PACK (objcode_cells.cells+10),
  272. SCM_PACK (objcode_cells.cells+12),
  273. SCM_PACK (objcode_cells.cells+14),
  274. SCM_PACK (objcode_cells.cells+16),
  275. /* 3 arguments */
  276. SCM_PACK (objcode_cells.cells+18),
  277. SCM_PACK (objcode_cells.cells+20),
  278. SCM_PACK (objcode_cells.cells+22),
  279. SCM_PACK (objcode_cells.cells+24),
  280. SCM_PACK (objcode_cells.cells+26),
  281. SCM_PACK (objcode_cells.cells+28),
  282. SCM_PACK (objcode_cells.cells+30)
  283. };
  284. /* (nargs * nargs) + nopt + rest * (nargs + 1) */
  285. #define SCM_SMOB_OBJCODE_TRAMPOLINE(nreq,nopt,rest) \
  286. scm_smob_objcode_trampolines[(nreq + nopt + rest) * (nreq + nopt + rest) \
  287. + nopt + rest * (nreq + nopt + rest + 1)]
  288. static SCM
  289. scm_smob_objcode_trampoline (unsigned int nreq, unsigned int nopt,
  290. unsigned int rest)
  291. {
  292. if (SCM_UNLIKELY (rest > 1 || nreq + nopt + rest > 3))
  293. scm_out_of_range ("make-smob", scm_from_uint (nreq + nopt + rest));
  294. return SCM_SMOB_OBJCODE_TRAMPOLINE (nreq, nopt, rest);
  295. }
  296. scm_t_bits
  297. scm_make_smob_type (char const *name, size_t size)
  298. #define FUNC_NAME "scm_make_smob_type"
  299. {
  300. long new_smob;
  301. SCM_CRITICAL_SECTION_START;
  302. new_smob = scm_numsmob;
  303. if (scm_numsmob != MAX_SMOB_COUNT)
  304. ++scm_numsmob;
  305. SCM_CRITICAL_SECTION_END;
  306. if (new_smob == MAX_SMOB_COUNT)
  307. scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
  308. scm_smobs[new_smob].name = name;
  309. scm_smobs[new_smob].size = size;
  310. /* Make a class object if Goops is present. */
  311. if (SCM_UNPACK (scm_smob_class[0]) != 0)
  312. scm_smob_class[new_smob] = scm_make_extended_class (name, 0);
  313. return scm_tc7_smob + new_smob * 256;
  314. }
  315. #undef FUNC_NAME
  316. void
  317. scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
  318. {
  319. scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
  320. }
  321. void
  322. scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
  323. {
  324. scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
  325. }
  326. void
  327. scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
  328. {
  329. scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
  330. }
  331. void
  332. scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
  333. {
  334. scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
  335. }
  336. void
  337. scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
  338. unsigned int req, unsigned int opt, unsigned int rst)
  339. {
  340. scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
  341. scm_smobs[SCM_TC2SMOBNUM (tc)].apply_trampoline_objcode
  342. = scm_smob_objcode_trampoline (req, opt, rst);
  343. if (SCM_UNPACK (scm_smob_class[0]) != 0)
  344. scm_i_inherit_applicable (scm_smob_class[SCM_TC2SMOBNUM (tc)]);
  345. }
  346. static SCM tramp_weak_map = SCM_BOOL_F;
  347. SCM
  348. scm_i_smob_apply_trampoline (SCM smob)
  349. {
  350. SCM tramp;
  351. tramp = scm_weak_table_refq (tramp_weak_map, smob, SCM_BOOL_F);
  352. if (scm_is_true (tramp))
  353. return tramp;
  354. else
  355. {
  356. const char *name;
  357. SCM objtable;
  358. name = SCM_SMOBNAME (SCM_SMOBNUM (smob));
  359. if (!name)
  360. name = "smob-apply";
  361. objtable = scm_c_make_vector (2, SCM_UNDEFINED);
  362. SCM_SIMPLE_VECTOR_SET (objtable, 0, smob);
  363. SCM_SIMPLE_VECTOR_SET (objtable, 1, scm_from_utf8_symbol (name));
  364. tramp = scm_make_program (SCM_SMOB_DESCRIPTOR (smob).apply_trampoline_objcode,
  365. objtable, SCM_BOOL_F);
  366. /* Race conditions (between the ref and this set!) cannot cause
  367. any harm here. */
  368. scm_weak_table_putq_x (tramp_weak_map, smob, tramp);
  369. return tramp;
  370. }
  371. }
  372. SCM
  373. scm_make_smob (scm_t_bits tc)
  374. {
  375. scm_t_bits n = SCM_TC2SMOBNUM (tc);
  376. size_t size = scm_smobs[n].size;
  377. scm_t_bits data = (size > 0
  378. ? (scm_t_bits) scm_gc_malloc (size, SCM_SMOBNAME (n))
  379. : 0);
  380. SCM_RETURN_NEWSMOB (tc, data);
  381. }
  382. /* Marking SMOBs using user-supplied mark procedures. */
  383. /* The GC kind used for SMOB types that provide a custom mark procedure. */
  384. static int smob_gc_kind;
  385. /* The generic SMOB mark procedure that gets called for SMOBs allocated
  386. with smob_gc_kind. */
  387. static struct GC_ms_entry *
  388. smob_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  389. struct GC_ms_entry *mark_stack_limit, GC_word env)
  390. {
  391. register SCM cell;
  392. register scm_t_bits tc, smobnum;
  393. cell = SCM_PACK_POINTER (addr);
  394. if (SCM_TYP7 (cell) != scm_tc7_smob)
  395. /* It is likely that the GC passed us a pointer to a free-list element
  396. which we must ignore (see warning in `gc/gc_mark.h'). */
  397. return mark_stack_ptr;
  398. tc = SCM_CELL_WORD_0 (cell);
  399. smobnum = SCM_TC2SMOBNUM (tc);
  400. if (smobnum >= scm_numsmob)
  401. /* The first word looks corrupt. */
  402. abort ();
  403. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_1 (cell)),
  404. mark_stack_ptr,
  405. mark_stack_limit, NULL);
  406. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_2 (cell)),
  407. mark_stack_ptr,
  408. mark_stack_limit, NULL);
  409. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_3 (cell)),
  410. mark_stack_ptr,
  411. mark_stack_limit, NULL);
  412. if (scm_smobs[smobnum].mark)
  413. {
  414. SCM obj;
  415. SCM_I_CURRENT_THREAD->current_mark_stack_ptr = mark_stack_ptr;
  416. SCM_I_CURRENT_THREAD->current_mark_stack_limit = mark_stack_limit;
  417. /* Invoke the SMOB's mark procedure, which will in turn invoke
  418. `scm_gc_mark ()', which may modify `current_mark_stack_ptr'. */
  419. obj = scm_smobs[smobnum].mark (cell);
  420. mark_stack_ptr = SCM_I_CURRENT_THREAD->current_mark_stack_ptr;
  421. if (SCM_HEAP_OBJECT_P (obj))
  422. /* Mark the returned object. */
  423. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (obj),
  424. mark_stack_ptr,
  425. mark_stack_limit, NULL);
  426. SCM_I_CURRENT_THREAD->current_mark_stack_limit = NULL;
  427. SCM_I_CURRENT_THREAD->current_mark_stack_ptr = NULL;
  428. }
  429. return mark_stack_ptr;
  430. }
  431. /* Mark object O. We assume that this function is only called during the
  432. mark phase, i.e., from within `smob_mark ()' or one of its
  433. descendents. */
  434. void
  435. scm_gc_mark (SCM o)
  436. {
  437. #define CURRENT_MARK_PTR \
  438. ((struct GC_ms_entry *)(SCM_I_CURRENT_THREAD->current_mark_stack_ptr))
  439. #define CURRENT_MARK_LIMIT \
  440. ((struct GC_ms_entry *)(SCM_I_CURRENT_THREAD->current_mark_stack_limit))
  441. if (SCM_HEAP_OBJECT_P (o))
  442. {
  443. /* At this point, the `current_mark_*' fields of the current thread
  444. must be defined (they are set in `smob_mark ()'). */
  445. register struct GC_ms_entry *mark_stack_ptr;
  446. if (!CURRENT_MARK_PTR)
  447. /* The function was not called from a mark procedure. */
  448. abort ();
  449. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (o),
  450. CURRENT_MARK_PTR, CURRENT_MARK_LIMIT,
  451. NULL);
  452. SCM_I_CURRENT_THREAD->current_mark_stack_ptr = mark_stack_ptr;
  453. }
  454. #undef CURRENT_MARK_PTR
  455. #undef CURRENT_MARK_LIMIT
  456. }
  457. /* Finalize SMOB by calling its SMOB type's free function, if any. */
  458. static void
  459. finalize_smob (GC_PTR ptr, GC_PTR data)
  460. {
  461. SCM smob;
  462. size_t (* free_smob) (SCM);
  463. smob = SCM_PACK_POINTER (ptr);
  464. #if 0
  465. printf ("finalizing SMOB %p (smobnum: %u)\n",
  466. ptr, SCM_SMOBNUM (smob));
  467. #endif
  468. free_smob = scm_smobs[SCM_SMOBNUM (smob)].free;
  469. if (free_smob)
  470. free_smob (smob);
  471. }
  472. /* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
  473. provide a custom mark procedure and it will be honored. */
  474. SCM
  475. scm_i_new_smob (scm_t_bits tc, scm_t_bits data)
  476. {
  477. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  478. SCM ret;
  479. /* Use the smob_gc_kind if needed to allow the mark procedure to
  480. run. Since the marker only deals with double cells, that case
  481. allocates a double cell. We leave words 2 and 3 to there initial
  482. values, which is 0. */
  483. if (scm_smobs [smobnum].mark)
  484. ret = SCM_PACK_POINTER (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
  485. else
  486. ret = SCM_PACK_POINTER (GC_MALLOC (sizeof (scm_t_cell)));
  487. SCM_SET_CELL_WORD_1 (ret, data);
  488. SCM_SET_CELL_WORD_0 (ret, tc);
  489. if (scm_smobs[smobnum].free)
  490. scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
  491. return ret;
  492. }
  493. /* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
  494. provide a custom mark procedure and it will be honored. */
  495. SCM
  496. scm_i_new_double_smob (scm_t_bits tc, scm_t_bits data1,
  497. scm_t_bits data2, scm_t_bits data3)
  498. {
  499. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  500. SCM ret;
  501. /* Use the smob_gc_kind if needed to allow the mark procedure to
  502. run. */
  503. if (scm_smobs [smobnum].mark)
  504. ret = SCM_PACK_POINTER (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
  505. else
  506. ret = SCM_PACK_POINTER (GC_MALLOC (2 * sizeof (scm_t_cell)));
  507. SCM_SET_CELL_WORD_3 (ret, data3);
  508. SCM_SET_CELL_WORD_2 (ret, data2);
  509. SCM_SET_CELL_WORD_1 (ret, data1);
  510. SCM_SET_CELL_WORD_0 (ret, tc);
  511. if (scm_smobs[smobnum].free)
  512. scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
  513. return ret;
  514. }
  515. void
  516. scm_smob_prehistory ()
  517. {
  518. long i;
  519. smob_gc_kind = GC_new_kind (GC_new_free_list (),
  520. GC_MAKE_PROC (GC_new_proc (smob_mark), 0),
  521. 0,
  522. /* Clear new objects. As of version 7.1, libgc
  523. doesn't seem to support passing 0 here. */
  524. 1);
  525. scm_numsmob = 0;
  526. for (i = 0; i < MAX_SMOB_COUNT; ++i)
  527. {
  528. scm_smobs[i].name = 0;
  529. scm_smobs[i].size = 0;
  530. scm_smobs[i].mark = 0;
  531. scm_smobs[i].free = 0;
  532. scm_smobs[i].print = scm_smob_print;
  533. scm_smobs[i].equalp = 0;
  534. scm_smobs[i].apply = 0;
  535. scm_smobs[i].apply_trampoline_objcode = SCM_BOOL_F;
  536. }
  537. tramp_weak_map = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  538. }
  539. /*
  540. Local Variables:
  541. c-file-style: "gnu"
  542. End:
  543. */