smob.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2006,
  2. * 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include "libguile/_scm.h"
  26. #include "libguile/async.h"
  27. #include "libguile/goops.h"
  28. #include "libguile/instructions.h"
  29. #include "libguile/objcodes.h"
  30. #include "libguile/programs.h"
  31. #include "libguile/smob.h"
  32. #include "libguile/bdw-gc.h"
  33. #include <gc/gc_mark.h>
  34. /* scm_smobs scm_numsmob
  35. * implement a fixed sized array of smob records.
  36. * Indexes into this table are used when generating type
  37. * tags for smobjects (if you know a tag you can get an index and conversely).
  38. */
  39. #define MAX_SMOB_COUNT SCM_I_MAX_SMOB_TYPE_COUNT
  40. long scm_numsmob;
  41. scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
  42. void
  43. scm_assert_smob_type (scm_t_bits tag, SCM val)
  44. {
  45. if (!SCM_SMOB_PREDICATE (tag, val))
  46. scm_wrong_type_arg_msg (NULL, 0, val, scm_smobs[SCM_TC2SMOBNUM(tag)].name);
  47. }
  48. /* {Mark}
  49. */
  50. /* This function is vestigial. It used to be the mark function's
  51. responsibility to set the mark bit on the smob or port, but now the
  52. generic marking routine in gc.c takes care of that, and a zero
  53. pointer for a mark function means "don't bother". So you never
  54. need scm_mark0.
  55. However, we leave it here because it's harmless to call it, and
  56. people out there have smob code that uses it, and there's no reason
  57. to make their links fail. */
  58. SCM
  59. scm_mark0 (SCM ptr SCM_UNUSED)
  60. {
  61. return SCM_BOOL_F;
  62. }
  63. SCM
  64. /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
  65. be used for real pairs. */
  66. scm_markcdr (SCM ptr)
  67. {
  68. return SCM_CELL_OBJECT_1 (ptr);
  69. }
  70. /* {Free}
  71. */
  72. size_t
  73. scm_free0 (SCM ptr SCM_UNUSED)
  74. {
  75. return 0;
  76. }
  77. /* {Print}
  78. */
  79. int
  80. scm_smob_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  81. {
  82. long n = SCM_SMOBNUM (exp);
  83. scm_puts_unlocked ("#<", port);
  84. scm_puts_unlocked (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
  85. scm_putc_unlocked (' ', port);
  86. if (scm_smobs[n].size)
  87. scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
  88. else
  89. scm_uintprint (SCM_UNPACK (exp), 16, port);
  90. scm_putc_unlocked ('>', port);
  91. return 1;
  92. }
  93. /* {Apply}
  94. */
  95. static SCM scm_smob_trampolines[16];
  96. /* (nargs * nargs) + nopt + rest * (nargs + 1) */
  97. #define SCM_SMOB_TRAMPOLINE(nreq,nopt,rest) \
  98. scm_smob_trampolines[(nreq + nopt + rest) * (nreq + nopt + rest) \
  99. + nopt + rest * (nreq + nopt + rest + 1)]
  100. static SCM
  101. apply_0 (SCM smob)
  102. {
  103. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  104. return subr (smob);
  105. }
  106. static SCM
  107. apply_1 (SCM smob, SCM a)
  108. {
  109. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  110. return subr (smob, a);
  111. }
  112. static SCM
  113. apply_2 (SCM smob, SCM a, SCM b)
  114. {
  115. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  116. return subr (smob, a, b);
  117. }
  118. static SCM
  119. apply_3 (SCM smob, SCM a, SCM b, SCM c)
  120. {
  121. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  122. return subr (smob, a, b, c);
  123. }
  124. static SCM
  125. scm_smob_trampoline (unsigned int nreq, unsigned int nopt,
  126. unsigned int rest)
  127. {
  128. SCM trampoline;
  129. if (SCM_UNLIKELY (rest > 1 || nreq + nopt + rest > 3))
  130. scm_out_of_range ("make-smob", scm_from_uint (nreq + nopt + rest));
  131. trampoline = SCM_SMOB_TRAMPOLINE (nreq, nopt, rest);
  132. if (SCM_LIKELY (SCM_UNPACK (trampoline)))
  133. return trampoline;
  134. switch (nreq + nopt + rest)
  135. {
  136. /* The + 1 is for the smob itself. */
  137. case 0:
  138. trampoline = scm_c_make_gsubr ("apply-smob/0", nreq + 1, nopt, rest,
  139. apply_0);
  140. break;
  141. case 1:
  142. trampoline = scm_c_make_gsubr ("apply-smob/1", nreq + 1, nopt, rest,
  143. apply_1);
  144. break;
  145. case 2:
  146. trampoline = scm_c_make_gsubr ("apply-smob/2", nreq + 1, nopt, rest,
  147. apply_2);
  148. break;
  149. case 3:
  150. trampoline = scm_c_make_gsubr ("apply-smob/3", nreq + 1, nopt, rest,
  151. apply_3);
  152. break;
  153. default:
  154. abort ();
  155. }
  156. SCM_SMOB_TRAMPOLINE (nreq, nopt, rest) = trampoline;
  157. return trampoline;
  158. }
  159. scm_t_bits
  160. scm_make_smob_type (char const *name, size_t size)
  161. #define FUNC_NAME "scm_make_smob_type"
  162. {
  163. long new_smob;
  164. SCM_CRITICAL_SECTION_START;
  165. new_smob = scm_numsmob;
  166. if (scm_numsmob != MAX_SMOB_COUNT)
  167. ++scm_numsmob;
  168. SCM_CRITICAL_SECTION_END;
  169. if (new_smob == MAX_SMOB_COUNT)
  170. scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
  171. scm_smobs[new_smob].name = name;
  172. scm_smobs[new_smob].size = size;
  173. /* Make a class object if Goops is present. */
  174. if (SCM_UNPACK (scm_smob_class[0]) != 0)
  175. scm_smob_class[new_smob] = scm_make_extended_class (name, 0);
  176. return scm_tc7_smob + new_smob * 256;
  177. }
  178. #undef FUNC_NAME
  179. void
  180. scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
  181. {
  182. scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
  183. }
  184. void
  185. scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
  186. {
  187. scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
  188. }
  189. void
  190. scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
  191. {
  192. scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
  193. }
  194. void
  195. scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
  196. {
  197. scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
  198. }
  199. void
  200. scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
  201. unsigned int req, unsigned int opt, unsigned int rst)
  202. {
  203. SCM trampoline = scm_smob_trampoline (req, opt, rst);
  204. scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
  205. scm_smobs[SCM_TC2SMOBNUM (tc)].apply_trampoline = trampoline;
  206. if (SCM_UNPACK (scm_smob_class[0]) != 0)
  207. scm_i_inherit_applicable (scm_smob_class[SCM_TC2SMOBNUM (tc)]);
  208. }
  209. SCM
  210. scm_make_smob (scm_t_bits tc)
  211. {
  212. scm_t_bits n = SCM_TC2SMOBNUM (tc);
  213. size_t size = scm_smobs[n].size;
  214. scm_t_bits data = (size > 0
  215. ? (scm_t_bits) scm_gc_malloc (size, SCM_SMOBNAME (n))
  216. : 0);
  217. SCM_RETURN_NEWSMOB (tc, data);
  218. }
  219. /* Marking SMOBs using user-supplied mark procedures. */
  220. /* The GC kind used for SMOB types that provide a custom mark procedure. */
  221. static int smob_gc_kind;
  222. /* Mark stack pointer and limit, used by `scm_gc_mark'. */
  223. static scm_i_pthread_key_t current_mark_stack_pointer;
  224. static scm_i_pthread_key_t current_mark_stack_limit;
  225. /* The generic SMOB mark procedure that gets called for SMOBs allocated
  226. with smob_gc_kind. */
  227. static struct GC_ms_entry *
  228. smob_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  229. struct GC_ms_entry *mark_stack_limit, GC_word env)
  230. {
  231. register SCM cell;
  232. register scm_t_bits tc, smobnum;
  233. cell = SCM_PACK_POINTER (addr);
  234. if (SCM_TYP7 (cell) != scm_tc7_smob)
  235. /* It is likely that the GC passed us a pointer to a free-list element
  236. which we must ignore (see warning in `gc/gc_mark.h'). */
  237. return mark_stack_ptr;
  238. tc = SCM_CELL_WORD_0 (cell);
  239. smobnum = SCM_TC2SMOBNUM (tc);
  240. if (smobnum >= scm_numsmob)
  241. /* The first word looks corrupt. */
  242. abort ();
  243. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_1 (cell)),
  244. mark_stack_ptr,
  245. mark_stack_limit, NULL);
  246. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_2 (cell)),
  247. mark_stack_ptr,
  248. mark_stack_limit, NULL);
  249. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_3 (cell)),
  250. mark_stack_ptr,
  251. mark_stack_limit, NULL);
  252. if (scm_smobs[smobnum].mark)
  253. {
  254. SCM obj;
  255. scm_i_pthread_setspecific (current_mark_stack_pointer, mark_stack_ptr);
  256. scm_i_pthread_setspecific (current_mark_stack_limit, mark_stack_limit);
  257. /* Invoke the SMOB's mark procedure, which will in turn invoke
  258. `scm_gc_mark', which may modify `current_mark_stack_pointer'. */
  259. obj = scm_smobs[smobnum].mark (cell);
  260. mark_stack_ptr = scm_i_pthread_getspecific (current_mark_stack_pointer);
  261. if (SCM_HEAP_OBJECT_P (obj))
  262. /* Mark the returned object. */
  263. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (obj),
  264. mark_stack_ptr,
  265. mark_stack_limit, NULL);
  266. scm_i_pthread_setspecific (current_mark_stack_pointer, NULL);
  267. scm_i_pthread_setspecific (current_mark_stack_limit, NULL);
  268. }
  269. return mark_stack_ptr;
  270. }
  271. /* Mark object O. We assume that this function is only called during the mark
  272. phase, i.e., from within `smob_mark' or one of its descendants. */
  273. void
  274. scm_gc_mark (SCM o)
  275. {
  276. if (SCM_HEAP_OBJECT_P (o))
  277. {
  278. void *mark_stack_ptr, *mark_stack_limit;
  279. mark_stack_ptr = scm_i_pthread_getspecific (current_mark_stack_pointer);
  280. mark_stack_limit = scm_i_pthread_getspecific (current_mark_stack_limit);
  281. if (mark_stack_ptr == NULL)
  282. /* The function was not called from a mark procedure. */
  283. abort ();
  284. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (o),
  285. mark_stack_ptr, mark_stack_limit,
  286. NULL);
  287. scm_i_pthread_setspecific (current_mark_stack_pointer, mark_stack_ptr);
  288. }
  289. }
  290. /* Finalize SMOB by calling its SMOB type's free function, if any. */
  291. static void
  292. finalize_smob (void *ptr, void *data)
  293. {
  294. SCM smob;
  295. size_t (* free_smob) (SCM);
  296. smob = SCM_PACK_POINTER (ptr);
  297. #if 0
  298. printf ("finalizing SMOB %p (smobnum: %u)\n",
  299. ptr, SCM_SMOBNUM (smob));
  300. #endif
  301. free_smob = scm_smobs[SCM_SMOBNUM (smob)].free;
  302. if (free_smob)
  303. free_smob (smob);
  304. }
  305. /* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
  306. provide a custom mark procedure and it will be honored. */
  307. SCM
  308. scm_i_new_smob (scm_t_bits tc, scm_t_bits data)
  309. {
  310. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  311. SCM ret;
  312. /* Use the smob_gc_kind if needed to allow the mark procedure to
  313. run. Since the marker only deals with double cells, that case
  314. allocates a double cell. We leave words 2 and 3 to there initial
  315. values, which is 0. */
  316. if (scm_smobs [smobnum].mark)
  317. ret = SCM_PACK_POINTER (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
  318. else
  319. ret = SCM_PACK_POINTER (GC_MALLOC (sizeof (scm_t_cell)));
  320. SCM_SET_CELL_WORD_1 (ret, data);
  321. SCM_SET_CELL_WORD_0 (ret, tc);
  322. if (scm_smobs[smobnum].free)
  323. scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
  324. return ret;
  325. }
  326. /* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
  327. provide a custom mark procedure and it will be honored. */
  328. SCM
  329. scm_i_new_double_smob (scm_t_bits tc, scm_t_bits data1,
  330. scm_t_bits data2, scm_t_bits data3)
  331. {
  332. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  333. SCM ret;
  334. /* Use the smob_gc_kind if needed to allow the mark procedure to
  335. run. */
  336. if (scm_smobs [smobnum].mark)
  337. ret = SCM_PACK_POINTER (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
  338. else
  339. ret = SCM_PACK_POINTER (GC_MALLOC (2 * sizeof (scm_t_cell)));
  340. SCM_SET_CELL_WORD_3 (ret, data3);
  341. SCM_SET_CELL_WORD_2 (ret, data2);
  342. SCM_SET_CELL_WORD_1 (ret, data1);
  343. SCM_SET_CELL_WORD_0 (ret, tc);
  344. if (scm_smobs[smobnum].free)
  345. scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
  346. return ret;
  347. }
  348. void
  349. scm_smob_prehistory ()
  350. {
  351. long i;
  352. scm_i_pthread_key_create (&current_mark_stack_pointer, NULL);
  353. scm_i_pthread_key_create (&current_mark_stack_limit, NULL);
  354. smob_gc_kind = GC_new_kind (GC_new_free_list (),
  355. GC_MAKE_PROC (GC_new_proc (smob_mark), 0),
  356. 0,
  357. /* Clear new objects. As of version 7.1, libgc
  358. doesn't seem to support passing 0 here. */
  359. 1);
  360. scm_numsmob = 0;
  361. for (i = 0; i < MAX_SMOB_COUNT; ++i)
  362. {
  363. scm_smobs[i].name = 0;
  364. scm_smobs[i].size = 0;
  365. scm_smobs[i].mark = 0;
  366. scm_smobs[i].free = 0;
  367. scm_smobs[i].print = scm_smob_print;
  368. scm_smobs[i].equalp = 0;
  369. scm_smobs[i].apply = 0;
  370. scm_smobs[i].apply_trampoline = SCM_BOOL_F;
  371. }
  372. }
  373. /*
  374. Local Variables:
  375. c-file-style: "gnu"
  376. End:
  377. */