smob.c 20 KB

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