guardians.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* Copyright (C) 1998,1999,2000,2001, 2006, 2008 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. /* This is an implementation of guardians as described in
  19. * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
  20. * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
  21. * Programming Language Design and Implementation, June 1993
  22. * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
  23. *
  24. * Original design: Mikael Djurfeldt
  25. * Original implementation: Michael Livshin
  26. * Hacked on since by: everybody
  27. *
  28. * By this point, the semantics are actually quite different from
  29. * those described in the abovementioned paper. The semantic changes
  30. * are there to improve safety and intuitiveness. The interface is
  31. * still (mostly) the one described by the paper, however.
  32. *
  33. * Boiled down again: Marius Vollmer
  34. *
  35. * Now they should again behave like those described in the paper.
  36. * Scheme guardians should be simple and friendly, not like the greedy
  37. * monsters we had...
  38. */
  39. #ifdef HAVE_CONFIG_H
  40. # include <config.h>
  41. #endif
  42. #include "libguile/_scm.h"
  43. #include "libguile/async.h"
  44. #include "libguile/ports.h"
  45. #include "libguile/print.h"
  46. #include "libguile/smob.h"
  47. #include "libguile/validate.h"
  48. #include "libguile/root.h"
  49. #include "libguile/hashtab.h"
  50. #include "libguile/weaks.h"
  51. #include "libguile/deprecation.h"
  52. #include "libguile/eval.h"
  53. #include "libguile/guardians.h"
  54. /* The live and zombies FIFOs are implemented as tconcs as described
  55. in Dybvig's paper. This decouples addition and removal of elements
  56. so that no synchronization between these needs to take place.
  57. */
  58. typedef struct t_tconc
  59. {
  60. SCM head;
  61. SCM tail;
  62. } t_tconc;
  63. #define TCONC_EMPTYP(tc) (scm_is_eq ((tc).head, (tc).tail))
  64. #define TCONC_IN(tc, obj, pair) \
  65. do { \
  66. SCM_SETCAR ((tc).tail, obj); \
  67. SCM_SET_CELL_OBJECT_1 (pair, SCM_EOL); \
  68. SCM_SET_CELL_OBJECT_0 (pair, SCM_BOOL_F); \
  69. SCM_SETCDR ((tc).tail, pair); \
  70. (tc).tail = pair; \
  71. } while (0)
  72. #define TCONC_OUT(tc, res) \
  73. do { \
  74. (res) = SCM_CAR ((tc).head); \
  75. (tc).head = SCM_CDR ((tc).head); \
  76. } while (0)
  77. static scm_t_bits tc16_guardian;
  78. typedef struct t_guardian
  79. {
  80. t_tconc live;
  81. t_tconc zombies;
  82. struct t_guardian *next;
  83. } t_guardian;
  84. #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
  85. #define GUARDIAN_DATA(x) ((t_guardian *) SCM_CELL_WORD_1 (x))
  86. static t_guardian *guardians;
  87. void
  88. scm_i_init_guardians_for_gc ()
  89. {
  90. guardians = NULL;
  91. }
  92. /* mark a guardian by adding it to the live guardian list. */
  93. static SCM
  94. guardian_mark (SCM ptr)
  95. {
  96. t_guardian *g = GUARDIAN_DATA (ptr);
  97. g->next = guardians;
  98. guardians = g;
  99. return SCM_BOOL_F;
  100. }
  101. /* Identify inaccessible objects and move them from the live list to
  102. the zombie list. An object is inaccessible when it is unmarked at
  103. this point. Therefore, the inaccessible objects are not marked yet
  104. since that would prevent them from being recognized as
  105. inaccessible.
  106. The pairs that form the life list itself are marked, tho.
  107. */
  108. void
  109. scm_i_identify_inaccessible_guardeds ()
  110. {
  111. t_guardian *g;
  112. for (g = guardians; g; g = g->next)
  113. {
  114. SCM pair, next_pair;
  115. SCM *prev_ptr;
  116. for (pair = g->live.head, prev_ptr = &g->live.head;
  117. !scm_is_eq (pair, g->live.tail);
  118. pair = next_pair)
  119. {
  120. SCM obj = SCM_CAR (pair);
  121. next_pair = SCM_CDR (pair);
  122. if (!SCM_GC_MARK_P (obj))
  123. {
  124. /* Unmarked, move to 'inaccessible' list.
  125. */
  126. *prev_ptr = next_pair;
  127. TCONC_IN (g->zombies, obj, pair);
  128. }
  129. else
  130. {
  131. SCM_SET_GC_MARK (pair);
  132. prev_ptr = SCM_CDRLOC (pair);
  133. }
  134. }
  135. SCM_SET_GC_MARK (pair);
  136. }
  137. }
  138. int
  139. scm_i_mark_inaccessible_guardeds ()
  140. {
  141. t_guardian *g;
  142. int again = 0;
  143. /* We never need to see the guardians again that are processed here,
  144. so we clear the list. Calling scm_gc_mark below might find new
  145. guardians, however (and other things), and we inform the GC about
  146. this by returning non-zero. See scm_mark_all in gc-mark.c
  147. */
  148. g = guardians;
  149. guardians = NULL;
  150. for (; g; g = g->next)
  151. {
  152. SCM pair;
  153. for (pair = g->zombies.head;
  154. !scm_is_eq (pair, g->zombies.tail);
  155. pair = SCM_CDR (pair))
  156. {
  157. if (!SCM_GC_MARK_P (pair))
  158. {
  159. scm_gc_mark (SCM_CAR (pair));
  160. SCM_SET_GC_MARK (pair);
  161. again = 1;
  162. }
  163. }
  164. SCM_SET_GC_MARK (pair);
  165. }
  166. return again;
  167. }
  168. static size_t
  169. guardian_free (SCM ptr)
  170. {
  171. scm_gc_free (GUARDIAN_DATA (ptr), sizeof (t_guardian), "guardian");
  172. return 0;
  173. }
  174. static int
  175. guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED)
  176. {
  177. t_guardian *g = GUARDIAN_DATA (guardian);
  178. scm_puts ("#<guardian ", port);
  179. scm_uintprint ((scm_t_bits) g, 16, port);
  180. scm_puts (" (reachable: ", port);
  181. scm_display (scm_length (SCM_CDR (g->live.head)), port);
  182. scm_puts (" unreachable: ", port);
  183. scm_display (scm_length (SCM_CDR (g->zombies.head)), port);
  184. scm_puts (")", port);
  185. scm_puts (">", port);
  186. return 1;
  187. }
  188. static void
  189. scm_i_guard (SCM guardian, SCM obj)
  190. {
  191. t_guardian *g = GUARDIAN_DATA (guardian);
  192. if (!SCM_IMP (obj))
  193. {
  194. SCM z;
  195. z = scm_cons (SCM_BOOL_F, SCM_BOOL_F);
  196. TCONC_IN (g->live, obj, z);
  197. }
  198. }
  199. static SCM
  200. scm_i_get_one_zombie (SCM guardian)
  201. {
  202. t_guardian *g = GUARDIAN_DATA (guardian);
  203. SCM res = SCM_BOOL_F;
  204. if (!TCONC_EMPTYP (g->zombies))
  205. TCONC_OUT (g->zombies, res);
  206. return res;
  207. }
  208. /* This is the Scheme entry point for each guardian: If OBJ is an
  209. * object, it's added to the guardian's live list. If OBJ is unbound,
  210. * the next available unreachable object (or #f if none) is returned.
  211. *
  212. * If the second optional argument THROW_P is true (the default), then
  213. * an error is raised if GUARDIAN is greedy and OBJ is already greedily
  214. * guarded. If THROW_P is false, #f is returned instead of raising the
  215. * error, and #t is returned if everything is fine.
  216. */
  217. static SCM
  218. guardian_apply (SCM guardian, SCM obj, SCM throw_p)
  219. {
  220. #if ENABLE_DEPRECATED
  221. if (!SCM_UNBNDP (throw_p))
  222. scm_c_issue_deprecation_warning
  223. ("Using the 'throw?' argument of a guardian is deprecated "
  224. "and ineffective.");
  225. #endif
  226. if (!SCM_UNBNDP (obj))
  227. {
  228. scm_i_guard (guardian, obj);
  229. return SCM_UNSPECIFIED;
  230. }
  231. else
  232. return scm_i_get_one_zombie (guardian);
  233. }
  234. SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
  235. (),
  236. "Create a new guardian. A guardian protects a set of objects from\n"
  237. "garbage collection, allowing a program to apply cleanup or other\n"
  238. "actions.\n"
  239. "\n"
  240. "@code{make-guardian} returns a procedure representing the guardian.\n"
  241. "Calling the guardian procedure with an argument adds the argument to\n"
  242. "the guardian's set of protected objects. Calling the guardian\n"
  243. "procedure without an argument returns one of the protected objects\n"
  244. "which are ready for garbage collection, or @code{#f} if no such object\n"
  245. "is available. Objects which are returned in this way are removed from\n"
  246. "the guardian.\n"
  247. "\n"
  248. "You can put a single object into a guardian more than once and you can\n"
  249. "put a single object into more than one guardian. The object will then\n"
  250. "be returned multiple times by the guardian procedures.\n"
  251. "\n"
  252. "An object is eligible to be returned from a guardian when it is no\n"
  253. "longer referenced from outside any guardian.\n"
  254. "\n"
  255. "There is no guarantee about the order in which objects are returned\n"
  256. "from a guardian. If you want to impose an order on finalization\n"
  257. "actions, for example, you can do that by keeping objects alive in some\n"
  258. "global data structure until they are no longer needed for finalizing\n"
  259. "other objects.\n"
  260. "\n"
  261. "Being an element in a weak vector, a key in a hash table with weak\n"
  262. "keys, or a value in a hash table with weak value does not prevent an\n"
  263. "object from being returned by a guardian. But as long as an object\n"
  264. "can be returned from a guardian it will not be removed from such a\n"
  265. "weak vector or hash table. In other words, a weak link does not\n"
  266. "prevent an object from being considered collectable, but being inside\n"
  267. "a guardian prevents a weak link from being broken.\n"
  268. "\n"
  269. "A key in a weak key hash table can be though of as having a strong\n"
  270. "reference to its associated value as long as the key is accessible.\n"
  271. "Consequently, when the key only accessible from within a guardian, the\n"
  272. "reference from the key to the value is also considered to be coming\n"
  273. "from within a guardian. Thus, if there is no other reference to the\n"
  274. "value, it is eligible to be returned from a guardian.\n")
  275. #define FUNC_NAME s_scm_make_guardian
  276. {
  277. t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian");
  278. SCM z1 = scm_cons (SCM_BOOL_F, SCM_EOL);
  279. SCM z2 = scm_cons (SCM_BOOL_F, SCM_EOL);
  280. SCM z;
  281. /* A tconc starts out with one tail pair. */
  282. g->live.head = g->live.tail = z1;
  283. g->zombies.head = g->zombies.tail = z2;
  284. g->next = NULL;
  285. SCM_NEWSMOB (z, tc16_guardian, g);
  286. return z;
  287. }
  288. #undef FUNC_NAME
  289. void
  290. scm_init_guardians ()
  291. {
  292. tc16_guardian = scm_make_smob_type ("guardian", 0);
  293. scm_set_smob_mark (tc16_guardian, guardian_mark);
  294. scm_set_smob_free (tc16_guardian, guardian_free);
  295. scm_set_smob_print (tc16_guardian, guardian_print);
  296. #if ENABLE_DEPRECATED
  297. scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 2, 0);
  298. #else
  299. scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
  300. #endif
  301. #include "libguile/guardians.x"
  302. }
  303. /*
  304. Local Variables:
  305. c-file-style: "gnu"
  306. End:
  307. */