smob.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2006,
  2. * 2009, 2010, 2011, 2012, 2013, 2015 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/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. static SCM scm_smob_trampolines[16];
  95. /* (nargs * nargs) + nopt + rest * (nargs + 1) */
  96. #define SCM_SMOB_TRAMPOLINE(nreq,nopt,rest) \
  97. scm_smob_trampolines[(nreq + nopt + rest) * (nreq + nopt + rest) \
  98. + nopt + rest * (nreq + nopt + rest + 1)]
  99. static SCM
  100. apply_0 (SCM smob)
  101. {
  102. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  103. return subr (smob);
  104. }
  105. static SCM
  106. apply_1 (SCM smob, SCM a)
  107. {
  108. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  109. return subr (smob, a);
  110. }
  111. static SCM
  112. apply_2 (SCM smob, SCM a, SCM b)
  113. {
  114. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  115. return subr (smob, a, b);
  116. }
  117. static SCM
  118. apply_3 (SCM smob, SCM a, SCM b, SCM c)
  119. {
  120. SCM (*subr)() = SCM_SMOB_DESCRIPTOR (smob).apply;
  121. return subr (smob, a, b, c);
  122. }
  123. static SCM
  124. scm_smob_trampoline (unsigned int nreq, unsigned int nopt,
  125. unsigned int rest)
  126. {
  127. SCM trampoline;
  128. if (SCM_UNLIKELY (rest > 1 || nreq + nopt + rest > 3))
  129. scm_out_of_range ("make-smob", scm_from_uint (nreq + nopt + rest));
  130. trampoline = SCM_SMOB_TRAMPOLINE (nreq, nopt, rest);
  131. if (SCM_LIKELY (SCM_UNPACK (trampoline)))
  132. return trampoline;
  133. switch (nreq + nopt + rest)
  134. {
  135. /* The + 1 is for the smob itself. */
  136. case 0:
  137. trampoline = scm_c_make_gsubr ("apply-smob/0", nreq + 1, nopt, rest,
  138. apply_0);
  139. break;
  140. case 1:
  141. trampoline = scm_c_make_gsubr ("apply-smob/1", nreq + 1, nopt, rest,
  142. apply_1);
  143. break;
  144. case 2:
  145. trampoline = scm_c_make_gsubr ("apply-smob/2", nreq + 1, nopt, rest,
  146. apply_2);
  147. break;
  148. case 3:
  149. trampoline = scm_c_make_gsubr ("apply-smob/3", nreq + 1, nopt, rest,
  150. apply_3);
  151. break;
  152. default:
  153. abort ();
  154. }
  155. SCM_SMOB_TRAMPOLINE (nreq, nopt, rest) = trampoline;
  156. return trampoline;
  157. }
  158. scm_t_bits
  159. scm_make_smob_type (char const *name, size_t size)
  160. #define FUNC_NAME "scm_make_smob_type"
  161. {
  162. long new_smob;
  163. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  164. new_smob = scm_numsmob;
  165. if (scm_numsmob != MAX_SMOB_COUNT)
  166. ++scm_numsmob;
  167. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  168. if (new_smob == MAX_SMOB_COUNT)
  169. scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
  170. scm_smobs[new_smob].name = name;
  171. scm_smobs[new_smob].size = size;
  172. /* Make a class object if Goops is present. */
  173. if (SCM_UNPACK (scm_i_smob_class[0]) != 0)
  174. scm_i_smob_class[new_smob] = scm_make_extended_class (name, 0);
  175. return scm_tc7_smob + new_smob * 256;
  176. }
  177. #undef FUNC_NAME
  178. void
  179. scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
  180. {
  181. scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
  182. }
  183. void
  184. scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
  185. {
  186. scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
  187. }
  188. void
  189. scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
  190. {
  191. scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
  192. }
  193. void
  194. scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
  195. {
  196. scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
  197. }
  198. void
  199. scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
  200. unsigned int req, unsigned int opt, unsigned int rst)
  201. {
  202. SCM trampoline = scm_smob_trampoline (req, opt, rst);
  203. scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
  204. scm_smobs[SCM_TC2SMOBNUM (tc)].apply_trampoline = trampoline;
  205. if (SCM_UNPACK (scm_i_smob_class[0]) != 0)
  206. scm_i_inherit_applicable (scm_i_smob_class[SCM_TC2SMOBNUM (tc)]);
  207. }
  208. SCM
  209. scm_make_smob (scm_t_bits tc)
  210. {
  211. scm_t_bits n = SCM_TC2SMOBNUM (tc);
  212. size_t size = scm_smobs[n].size;
  213. scm_t_bits data = (size > 0
  214. ? (scm_t_bits) scm_gc_malloc (size, SCM_SMOBNAME (n))
  215. : 0);
  216. SCM_RETURN_NEWSMOB (tc, data);
  217. }
  218. /* Marking SMOBs using user-supplied mark procedures. */
  219. /* The GC kind used for SMOB types that provide a custom mark procedure. */
  220. static int smob_gc_kind;
  221. /* Mark stack pointer and limit, used by `scm_gc_mark'. */
  222. static scm_i_pthread_key_t current_mark_stack_pointer;
  223. static scm_i_pthread_key_t current_mark_stack_limit;
  224. /* The generic SMOB mark procedure that gets called for SMOBs allocated
  225. with smob_gc_kind. */
  226. static struct GC_ms_entry *
  227. smob_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  228. struct GC_ms_entry *mark_stack_limit, GC_word env)
  229. {
  230. register SCM cell;
  231. register scm_t_bits tc, smobnum;
  232. cell = SCM_PACK_POINTER (addr);
  233. if (SCM_TYP7 (cell) != scm_tc7_smob)
  234. /* It is likely that the GC passed us a pointer to a free-list element
  235. which we must ignore (see warning in `gc/gc_mark.h'). */
  236. return mark_stack_ptr;
  237. tc = SCM_CELL_WORD_0 (cell);
  238. smobnum = SCM_TC2SMOBNUM (tc);
  239. if (smobnum >= scm_numsmob)
  240. /* The first word looks corrupt. */
  241. abort ();
  242. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_1 (cell)),
  243. mark_stack_ptr,
  244. mark_stack_limit, NULL);
  245. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_2 (cell)),
  246. mark_stack_ptr,
  247. mark_stack_limit, NULL);
  248. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (SCM_CELL_OBJECT_3 (cell)),
  249. mark_stack_ptr,
  250. mark_stack_limit, NULL);
  251. if (scm_smobs[smobnum].mark)
  252. {
  253. SCM obj;
  254. scm_i_pthread_setspecific (current_mark_stack_pointer, mark_stack_ptr);
  255. scm_i_pthread_setspecific (current_mark_stack_limit, mark_stack_limit);
  256. /* Invoke the SMOB's mark procedure, which will in turn invoke
  257. `scm_gc_mark', which may modify `current_mark_stack_pointer'. */
  258. obj = scm_smobs[smobnum].mark (cell);
  259. mark_stack_ptr = scm_i_pthread_getspecific (current_mark_stack_pointer);
  260. if (SCM_HEAP_OBJECT_P (obj))
  261. /* Mark the returned object. */
  262. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (obj),
  263. mark_stack_ptr,
  264. mark_stack_limit, NULL);
  265. scm_i_pthread_setspecific (current_mark_stack_pointer, NULL);
  266. scm_i_pthread_setspecific (current_mark_stack_limit, NULL);
  267. }
  268. return mark_stack_ptr;
  269. }
  270. /* Mark object O. We assume that this function is only called during the mark
  271. phase, i.e., from within `smob_mark' or one of its descendants. */
  272. void
  273. scm_gc_mark (SCM o)
  274. {
  275. if (SCM_HEAP_OBJECT_P (o))
  276. {
  277. void *mark_stack_ptr, *mark_stack_limit;
  278. mark_stack_ptr = scm_i_pthread_getspecific (current_mark_stack_pointer);
  279. mark_stack_limit = scm_i_pthread_getspecific (current_mark_stack_limit);
  280. if (mark_stack_ptr == NULL)
  281. /* The function was not called from a mark procedure. */
  282. abort ();
  283. mark_stack_ptr = GC_MARK_AND_PUSH (SCM2PTR (o),
  284. mark_stack_ptr, mark_stack_limit,
  285. NULL);
  286. scm_i_pthread_setspecific (current_mark_stack_pointer, mark_stack_ptr);
  287. }
  288. }
  289. /* Finalize SMOB by calling its SMOB type's free function, if any. */
  290. static void
  291. finalize_smob (void *ptr, void *data)
  292. {
  293. SCM smob;
  294. size_t (* free_smob) (SCM);
  295. smob = SCM_PACK_POINTER (ptr);
  296. #if 0
  297. printf ("finalizing SMOB %p (smobnum: %u)\n",
  298. ptr, SCM_SMOBNUM (smob));
  299. #endif
  300. free_smob = scm_smobs[SCM_SMOBNUM (smob)].free;
  301. if (free_smob)
  302. free_smob (smob);
  303. }
  304. /* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
  305. provide a custom mark procedure and it will be honored. */
  306. SCM
  307. scm_i_new_smob (scm_t_bits tc, scm_t_bits data)
  308. {
  309. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  310. SCM ret;
  311. /* Use the smob_gc_kind if needed to allow the mark procedure to
  312. run. Since the marker only deals with double cells, that case
  313. allocates a double cell. We leave words 2 and 3 to there initial
  314. values, which is 0. */
  315. if (scm_smobs [smobnum].mark)
  316. ret = SCM_PACK_POINTER (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
  317. else
  318. ret = SCM_PACK_POINTER (GC_MALLOC (sizeof (scm_t_cell)));
  319. SCM_SET_CELL_WORD_1 (ret, data);
  320. SCM_SET_CELL_WORD_0 (ret, tc);
  321. if (scm_smobs[smobnum].free)
  322. scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
  323. return ret;
  324. }
  325. /* Return a SMOB with typecode TC. The SMOB type corresponding to TC may
  326. provide a custom mark procedure and it will be honored. */
  327. SCM
  328. scm_i_new_double_smob (scm_t_bits tc, scm_t_bits data1,
  329. scm_t_bits data2, scm_t_bits data3)
  330. {
  331. scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
  332. SCM ret;
  333. /* Use the smob_gc_kind if needed to allow the mark procedure to
  334. run. */
  335. if (scm_smobs [smobnum].mark)
  336. ret = SCM_PACK_POINTER (GC_generic_malloc (2 * sizeof (scm_t_cell), smob_gc_kind));
  337. else
  338. ret = SCM_PACK_POINTER (GC_MALLOC (2 * sizeof (scm_t_cell)));
  339. SCM_SET_CELL_WORD_3 (ret, data3);
  340. SCM_SET_CELL_WORD_2 (ret, data2);
  341. SCM_SET_CELL_WORD_1 (ret, data1);
  342. SCM_SET_CELL_WORD_0 (ret, tc);
  343. if (scm_smobs[smobnum].free)
  344. scm_i_set_finalizer (SCM2PTR (ret), finalize_smob, NULL);
  345. return ret;
  346. }
  347. SCM
  348. scm_smob_type_class (scm_t_bits tc)
  349. {
  350. scm_load_goops ();
  351. return scm_i_smob_class[SCM_TC2SMOBNUM (tc)];
  352. }
  353. void
  354. scm_smob_prehistory ()
  355. {
  356. long i;
  357. scm_i_pthread_key_create (&current_mark_stack_pointer, NULL);
  358. scm_i_pthread_key_create (&current_mark_stack_limit, NULL);
  359. smob_gc_kind = GC_new_kind (GC_new_free_list (),
  360. GC_MAKE_PROC (GC_new_proc (smob_mark), 0),
  361. 0,
  362. /* Clear new objects. As of version 7.1, libgc
  363. doesn't seem to support passing 0 here. */
  364. 1);
  365. scm_numsmob = 0;
  366. for (i = 0; i < MAX_SMOB_COUNT; ++i)
  367. {
  368. scm_smobs[i].name = 0;
  369. scm_smobs[i].size = 0;
  370. scm_smobs[i].mark = 0;
  371. scm_smobs[i].free = 0;
  372. scm_smobs[i].print = scm_smob_print;
  373. scm_smobs[i].equalp = 0;
  374. scm_smobs[i].apply = 0;
  375. scm_smobs[i].apply_trampoline = SCM_BOOL_F;
  376. }
  377. }
  378. /*
  379. Local Variables:
  380. c-file-style: "gnu"
  381. End:
  382. */