guardians.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. /* This is an implementation of guardians as described in
  42. * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
  43. * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
  44. * Programming Language Design and Implementation, June 1993
  45. * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
  46. *
  47. * Author: Michael N. Livshin
  48. * Modified by: Mikael Djurfeldt
  49. */
  50. #include <stdio.h>
  51. #include <assert.h>
  52. #include "libguile/_scm.h"
  53. #include "libguile/ports.h"
  54. #include "libguile/print.h"
  55. #include "libguile/smob.h"
  56. #include "libguile/vectors.h"
  57. #include "libguile/validate.h"
  58. #include "libguile/guardians.h"
  59. static long scm_tc16_guardian;
  60. /* The live and zombies FIFOs are implemented as tconcs as described
  61. in Dybvig's paper. This decouples addition and removal of elements
  62. so that no synchronization between these needs to take place.
  63. */
  64. #define TCONC_IN(tc, obj, pair) \
  65. do { \
  66. SCM_SETCAR ((tc).tail, obj); \
  67. SCM_SETCAR (pair, SCM_BOOL_F); \
  68. SCM_SETCDR (pair, SCM_EOL); \
  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. #define TCONC_EMPTYP(tc) (SCM_EQ_P ((tc).head, (tc).tail))
  78. typedef struct tconc_t
  79. {
  80. SCM head;
  81. SCM tail;
  82. } tconc_t;
  83. typedef struct guardian_t
  84. {
  85. tconc_t live;
  86. tconc_t zombies;
  87. struct guardian_t *next;
  88. } guardian_t;
  89. #define GUARDIAN(x) ((guardian_t *) SCM_CELL_WORD_1 (x))
  90. #define GUARDIAN_LIVE(x) (GUARDIAN (x)->live)
  91. #define GUARDIAN_ZOMBIES(x) (GUARDIAN (x)->zombies)
  92. #define GUARDIAN_NEXT(x) (GUARDIAN (x)->next)
  93. #define CCLO_G(cclo) (SCM_VELTS (cclo)[1])
  94. /* subr constructed from guard below. */
  95. static SCM guard1;
  96. /* this is wrapped in a compiled closure and is the Scheme entry point
  97. for each guardian: if arg is an object, it's added to the
  98. guardian's live list. if arg is unbound, the next available
  99. zombified object (or #f if none) is returned. */
  100. static SCM
  101. guard (SCM cclo, SCM arg)
  102. {
  103. if (!SCM_UNBNDP (arg))
  104. {
  105. scm_guard (cclo, arg);
  106. return SCM_UNSPECIFIED;
  107. }
  108. else
  109. return scm_get_one_zombie (cclo);
  110. }
  111. void
  112. scm_guard (SCM guardian, SCM obj)
  113. {
  114. SCM g = CCLO_G (guardian);
  115. if (SCM_NIMP (obj))
  116. {
  117. SCM z;
  118. SCM_NEWCELL (z);
  119. /* This critical section barrier will be replaced by a mutex. */
  120. SCM_DEFER_INTS;
  121. TCONC_IN (GUARDIAN_LIVE (g), obj, z);
  122. SCM_ALLOW_INTS;
  123. }
  124. }
  125. SCM
  126. scm_get_one_zombie (SCM guardian)
  127. {
  128. SCM g = CCLO_G (guardian);
  129. SCM res = SCM_BOOL_F;
  130. /* This critical section barrier will be replaced by a mutex. */
  131. SCM_DEFER_INTS;
  132. if (!TCONC_EMPTYP (GUARDIAN_ZOMBIES (g)))
  133. TCONC_OUT (GUARDIAN_ZOMBIES (g), res);
  134. SCM_ALLOW_INTS;
  135. return res;
  136. }
  137. SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
  138. (),
  139. "Create a new guardian.\n"
  140. "A guardian protects a set of objects from garbage collection,\n"
  141. "allowing a program to apply cleanup or other actions.\n\n"
  142. "make-guardian returns a procedure representing the guardian.\n"
  143. "Calling the guardian procedure with an argument adds the\n"
  144. "argument to the guardian's set of protected objects.\n"
  145. "Calling the guardian procedure without an argument returns\n"
  146. "one of the protected objects which are ready for garbage\n"
  147. "collection or @code{#f} if no such object is available.\n"
  148. "Objects which are returned in this way are removed from\n"
  149. "the guardian.\n\n".
  150. "See R. Kent Dybvig, Carl Bruggeman, and David Eby (1993)\n"
  151. "\"Guardians in a Generation-Based Garbage Collector\".\n"
  152. "ACM SIGPLAN Conference on Programming Language Design\n"
  153. "and Implementation, June 1993.")
  154. #define FUNC_NAME s_scm_make_guardian
  155. {
  156. SCM cclo = scm_makcclo (guard1, 2L);
  157. guardian_t *g = SCM_MUST_MALLOC_TYPE(guardian_t);
  158. SCM z1 = scm_cons (SCM_BOOL_F, SCM_EOL);
  159. SCM z2 = scm_cons (SCM_BOOL_F, SCM_EOL);
  160. SCM z;
  161. /* A tconc starts out with one tail pair. */
  162. g->live.head = g->live.tail = z1;
  163. g->zombies.head = g->zombies.tail = z2;
  164. SCM_NEWSMOB (z, scm_tc16_guardian, g);
  165. CCLO_G (cclo) = z;
  166. return cclo;
  167. }
  168. #undef FUNC_NAME
  169. /* during the gc mark phase, live guardians are linked into a list
  170. here. */
  171. static guardian_t *first_live_guardian = NULL;
  172. static guardian_t **current_link_field = NULL;
  173. /* called before gc mark phase begins to initialise the live guardian
  174. list. */
  175. static void *
  176. scm_guardian_gc_init (void *dummy1, void *dummy2, void *dummy3)
  177. {
  178. current_link_field = &first_live_guardian;
  179. first_live_guardian = NULL;
  180. return 0;
  181. }
  182. /* mark a guardian by adding it to the live guardian list. */
  183. static SCM
  184. g_mark (SCM ptr)
  185. {
  186. *current_link_field = GUARDIAN (ptr);
  187. current_link_field = &GUARDIAN_NEXT (ptr);
  188. GUARDIAN_NEXT (ptr) = NULL;
  189. /* the objects protected by the guardian are not marked here: that
  190. would prevent them from ever getting collected. instead marking
  191. is done at the end of the mark phase by scm_guardian_zombify. */
  192. return SCM_BOOL_F;
  193. }
  194. /* this is called by the garbage collector between the mark and sweep
  195. phases. for each marked guardian, it moves any unmarked object in
  196. its live list (tconc) to its zombie list (tconc). */
  197. static void *
  198. scm_guardian_zombify (void *dummy1, void *dummy2, void *dummy3)
  199. {
  200. guardian_t *first_guardian;
  201. guardian_t **link_field = &first_live_guardian;
  202. /* Note that new guardians may be stuck on the end of the live
  203. guardian list as we run this loop. As we move unmarked objects
  204. to the zombie list and mark them, we may find some guarded
  205. guardians. The guardian mark function will stick them on the end
  206. of this list, so they'll be processed properly. */
  207. do {
  208. guardian_t *g;
  209. first_guardian = *link_field;
  210. link_field = current_link_field;
  211. /* first, scan all the guardians that are currently known to be live
  212. and move their unmarked objects to zombie lists. */
  213. for (g = first_guardian; g; g = g->next)
  214. {
  215. SCM tconc_tail = g->live.tail;
  216. SCM *prev_ptr = &g->live.head;
  217. SCM pair = g->live.head;
  218. while (! SCM_EQ_P (pair, tconc_tail))
  219. {
  220. SCM next_pair = SCM_CDR (pair);
  221. if (SCM_NMARKEDP (SCM_CAR (pair)))
  222. {
  223. /* got you, zombie! */
  224. /* out of the live list! */
  225. *prev_ptr = next_pair;
  226. /* into the zombie list! */
  227. TCONC_IN (g->zombies, SCM_CAR (pair), pair);
  228. }
  229. else
  230. prev_ptr = SCM_CDRLOC (pair);
  231. pair = next_pair;
  232. }
  233. /* Mark the cells of the live list (yes, the cells in the list,
  234. even though we don't care about objects pointed to by the list
  235. cars, since we know they are already marked). */
  236. for (pair = g->live.head; SCM_NIMP (pair); pair = SCM_GCCDR (pair))
  237. SCM_SETGCMARK (pair);
  238. }
  239. /* ghouston: Doesn't it seem a bit disturbing that if a zombie
  240. is returned to full life after getting returned from the
  241. guardian procedure, it may reference objects which are in a
  242. guardian's zombie list? Is it not necessary to move such
  243. zombies back to the live list, to avoid allowing the
  244. guardian procedure to return an object which is referenced,
  245. so not collectable? The paper doesn't give this
  246. impression.
  247. cmm: the paper does explicitly say that an object that is
  248. guarded more than once should be returned more than once.
  249. I believe this covers the above scenario. */
  250. /* Preserve the zombies in their undead state, by marking to
  251. prevent collection. Note that this may uncover zombified
  252. guardians -- if so, they'll be processed in the next loop. */
  253. for (g = first_guardian; g && (!*link_field || g != *link_field); g = g->next)
  254. scm_gc_mark (g->zombies.head);
  255. } while (current_link_field != link_field);
  256. return 0;
  257. }
  258. /* not generally used, since guardian smob is wrapped in a closure.
  259. maybe useful for debugging. */
  260. static int
  261. g_print (SCM exp, SCM port, scm_print_state *pstate)
  262. {
  263. char buf[256];
  264. sprintf (buf, "#<guardian live objs: %lu zombies: %lu>",
  265. scm_ilength (SCM_CDR (GUARDIAN_LIVE (exp).head)),
  266. scm_ilength (SCM_CDR (GUARDIAN_ZOMBIES (exp).head)));
  267. scm_puts (buf, port);
  268. return 1;
  269. }
  270. void
  271. scm_init_guardian()
  272. {
  273. scm_tc16_guardian = scm_make_smob_type_mfpe ("guardian", sizeof (guardian_t),
  274. g_mark, NULL, g_print, NULL);
  275. guard1 = scm_make_subr_opt ("guardian", scm_tc7_subr_2o, guard, 0);
  276. scm_c_hook_add (&scm_before_mark_c_hook, scm_guardian_gc_init, 0, 0);
  277. scm_c_hook_add (&scm_before_sweep_c_hook, scm_guardian_zombify, 0, 0);
  278. #include "libguile/guardians.x"
  279. }
  280. /*
  281. Local Variables:
  282. c-file-style: "gnu"
  283. End:
  284. */