smob.c 13 KB

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