gc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* classes: h_files */
  2. #ifndef SCM_GC_H
  3. #define SCM_GC_H
  4. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 3 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #include "libguile/__scm.h"
  22. #include "libguile/hooks.h"
  23. #include "libguile/threads.h"
  24. /* Cell allocation and garbage collection work rouhgly in the
  25. following manner:
  26. Each thread has a 'freelist', which is a list of available cells.
  27. (It actually has two freelists, one for single cells and one for
  28. double cells. Everything works analogous for double cells.)
  29. When a thread wants to allocate a cell and the freelist is empty,
  30. it refers to a global list of unswept 'cards'. A card is a small
  31. block of cells that are contigous in memory, together with the
  32. corresponding mark bits. A unswept card is one where the mark bits
  33. are set for cells that have been in use during the last global mark
  34. phase, but the unmarked cells of the card have not been scanned and
  35. freed yet.
  36. The thread takes one of the unswept cards and sweeps it, thereby
  37. building a new freelist that it then uses. Sweeping a card will
  38. call the smob free functions of unmarked cells, for example, and
  39. thus, these free functions can run at any time, in any thread.
  40. When there are no more unswept cards available, the thread performs
  41. a global garbage collection. For this, all other threads are
  42. stopped. A global mark is performed and all cards are put into the
  43. global list of unswept cards. Whennecessary, new cards are
  44. allocated and initialized at this time. The other threads are then
  45. started again.
  46. */
  47. typedef struct scm_t_cell
  48. {
  49. SCM word_0;
  50. SCM word_1;
  51. } scm_t_cell;
  52. /*
  53. CARDS
  54. A card is a small `page' of memory; it will be the unit for lazy
  55. sweeping, generations, etc. The first cell of a card contains a
  56. pointer to the mark bitvector, so that we can find the bitvector
  57. efficiently: we knock off some lowerorder bits.
  58. The size on a 32 bit machine is 256 cells = 2kb. The card [XXX]
  59. */
  60. /* Cray machines have pointers that are incremented once for each
  61. * word, rather than each byte, the 3 most significant bits encode the
  62. * byte within the word. The following macros deal with this by
  63. * storing the native Cray pointers like the ones that looks like scm
  64. * expects. This is done for any pointers that point to a cell,
  65. * pointers to scm_vector elts, functions, &c are not munged.
  66. */
  67. #ifdef _UNICOS
  68. # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x) >> 3))
  69. # define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
  70. #else
  71. # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x)))
  72. # define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
  73. #endif /* def _UNICOS */
  74. #define SCM_GC_CARD_N_HEADER_CELLS 1
  75. #define SCM_GC_CARD_N_CELLS 256
  76. #define SCM_GC_SIZEOF_CARD SCM_GC_CARD_N_CELLS * sizeof (scm_t_cell)
  77. #define SCM_GC_CARD_BVEC(card) ((scm_t_c_bvec_long *) ((card)->word_0))
  78. #define SCM_GC_SET_CARD_BVEC(card, bvec) \
  79. ((card)->word_0 = (SCM) (bvec))
  80. #define SCM_GC_GET_CARD_FLAGS(card) ((long) ((card)->word_1))
  81. #define SCM_GC_SET_CARD_FLAGS(card, flags) \
  82. ((card)->word_1 = (SCM) (flags))
  83. #define SCM_GC_GET_CARD_FLAG(card, shift) \
  84. (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
  85. #define SCM_GC_SET_CARD_FLAG(card, shift) \
  86. (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) | (1L << (shift))))
  87. #define SCM_GC_CLEAR_CARD_FLAG(card, shift) \
  88. (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) & ~(1L << (shift))))
  89. /*
  90. Remove card flags. They hamper lazy initialization, and aren't used
  91. anyways.
  92. */
  93. /* card addressing. for efficiency, cards are *always* aligned to
  94. SCM_GC_CARD_SIZE. */
  95. #define SCM_GC_CARD_SIZE_MASK (SCM_GC_SIZEOF_CARD-1)
  96. #define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
  97. #define SCM_GC_CELL_CARD(x) ((scm_t_cell *) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
  98. #define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
  99. #define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
  100. #define SCM_GC_SET_CELL_BVEC(x, bvec) SCM_GC_SET_CARD_BVEC (SCM_GC_CELL_CARD (x), bvec)
  101. #define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
  102. #define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
  103. #define SCM_GC_CELL_CLEAR_BIT(x) SCM_C_BVEC_CLEAR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
  104. #define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_SIZEOF_CARD - 1)
  105. #define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
  106. /* low level bit banging aids */
  107. typedef unsigned long scm_t_c_bvec_long;
  108. #if (SCM_SIZEOF_UNSIGNED_LONG == 8)
  109. # define SCM_C_BVEC_LONG_BITS 64
  110. # define SCM_C_BVEC_OFFSET_SHIFT 6
  111. # define SCM_C_BVEC_POS_MASK 63
  112. # define SCM_CELL_SIZE_SHIFT 4
  113. #else
  114. # define SCM_C_BVEC_LONG_BITS 32
  115. # define SCM_C_BVEC_OFFSET_SHIFT 5
  116. # define SCM_C_BVEC_POS_MASK 31
  117. # define SCM_CELL_SIZE_SHIFT 3
  118. #endif
  119. #define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
  120. #define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
  121. #define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
  122. #define SCM_C_BVEC_CLEAR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
  123. /* testing and changing GC marks */
  124. #define SCM_GC_MARK_P(x) SCM_GC_CELL_GET_BIT (x)
  125. SCM_INTERNAL void scm_i_ensure_marking(void);
  126. #if (SCM_DEBUG_MARKING_API == 1)
  127. #define SCM_I_ENSURE_MARKING scm_i_ensure_marking(),
  128. #else
  129. #define SCM_I_ENSURE_MARKING
  130. #endif
  131. #define SCM_SET_GC_MARK(x) SCM_I_ENSURE_MARKING SCM_GC_CELL_SET_BIT (x)
  132. #define SCM_CLEAR_GC_MARK(x) SCM_I_ENSURE_MARKING SCM_GC_CELL_CLEAR_BIT (x)
  133. /* Low level cell data accessing macros. These macros should only be used
  134. * from within code related to garbage collection issues, since they will
  135. * never check the cells they are applied to - not even if guile is compiled
  136. * in debug mode. In particular these macros will even work for free cells,
  137. * which should never be encountered by user code. */
  138. #define SCM_GC_CELL_OBJECT(x, n) (((SCM *)SCM2PTR (x)) [n])
  139. #define SCM_GC_CELL_WORD(x, n) (SCM_UNPACK (SCM_GC_CELL_OBJECT ((x), (n))))
  140. #define SCM_GC_SET_CELL_OBJECT(x, n, v) ((((SCM *)SCM2PTR (x)) [n]) = (v))
  141. #define SCM_GC_SET_CELL_WORD(x, n, v) \
  142. (SCM_GC_SET_CELL_OBJECT ((x), (n), SCM_PACK (v)))
  143. #define SCM_GC_CELL_TYPE(x) (SCM_GC_CELL_OBJECT ((x), 0))
  144. /* Except for the garbage collector, no part of guile should ever run over a
  145. * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
  146. * SCM_SET_CELL_* macros below report an error if they are applied to a free
  147. * cell. Some other plausibility checks are also performed. However, if
  148. * guile is not compiled in debug mode, there won't be any time penalty at all
  149. * when using these macros. */
  150. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  151. # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
  152. #else
  153. # define SCM_VALIDATE_CELL(cell, expr) (expr)
  154. #endif
  155. #define SCM_CELL_WORD(x, n) \
  156. SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
  157. #define SCM_CELL_WORD_0(x) SCM_CELL_WORD ((x), 0)
  158. #define SCM_CELL_WORD_1(x) SCM_CELL_WORD ((x), 1)
  159. #define SCM_CELL_WORD_2(x) SCM_CELL_WORD ((x), 2)
  160. #define SCM_CELL_WORD_3(x) SCM_CELL_WORD ((x), 3)
  161. #define SCM_CELL_OBJECT(x, n) \
  162. SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
  163. #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT ((x), 0)
  164. #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT ((x), 1)
  165. #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT ((x), 2)
  166. #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT ((x), 3)
  167. #define SCM_SET_CELL_WORD(x, n, v) \
  168. SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
  169. #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD ((x), 0, (v))
  170. #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD ((x), 1, (v))
  171. #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD ((x), 2, (v))
  172. #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD ((x), 3, (v))
  173. #define SCM_SET_CELL_OBJECT(x, n, v) \
  174. SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
  175. #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT ((x), 0, (v))
  176. #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT ((x), 1, (v))
  177. #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT ((x), 2, (v))
  178. #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT ((x), 3, (v))
  179. #define SCM_CELL_OBJECT_LOC(x, n) (SCM_VALIDATE_CELL((x), &SCM_GC_CELL_OBJECT ((x), (n))))
  180. #define SCM_CARLOC(x) (SCM_CELL_OBJECT_LOC ((x), 0))
  181. #define SCM_CDRLOC(x) (SCM_CELL_OBJECT_LOC ((x), 1))
  182. #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
  183. #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
  184. /* Freelists consist of linked cells where the type entry holds the value
  185. * scm_tc_free_cell and the second entry holds a pointer to the next cell of
  186. * the freelist. Due to this structure, freelist cells are not cons cells
  187. * and thus may not be accessed using SCM_CAR and SCM_CDR. */
  188. #define SCM_FREE_CELL_CDR(x) \
  189. (SCM_GC_CELL_OBJECT ((x), 1))
  190. #define SCM_SET_FREE_CELL_CDR(x, v) \
  191. (SCM_GC_SET_CELL_OBJECT ((x), 1, (v)))
  192. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  193. /* Set this to != 0 if every cell that is accessed shall be checked:
  194. */
  195. SCM_API int scm_debug_cell_accesses_p;
  196. SCM_API int scm_expensive_debug_cell_accesses_p;
  197. SCM_API int scm_debug_cells_gc_interval ;
  198. void scm_i_expensive_validation_check (SCM cell);
  199. #endif
  200. SCM_INTERNAL scm_i_pthread_mutex_t scm_i_gc_admin_mutex;
  201. #define scm_gc_running_p (SCM_I_CURRENT_THREAD->gc_running_p)
  202. SCM_INTERNAL scm_i_pthread_mutex_t scm_i_sweep_mutex;
  203. #ifdef __ia64__
  204. void *scm_ia64_register_backing_store_base (void);
  205. void *scm_ia64_ar_bsp (const void *);
  206. #endif
  207. #if (SCM_ENABLE_DEPRECATED == 1)
  208. SCM_API size_t scm_default_init_heap_size_1;
  209. SCM_API int scm_default_min_yield_1;
  210. SCM_API size_t scm_default_init_heap_size_2;
  211. SCM_API int scm_default_min_yield_2;
  212. SCM_API size_t scm_default_max_segment_size;
  213. #else
  214. #define scm_default_init_heap_size_1 deprecated
  215. #define scm_default_min_yield_1 deprecated
  216. #define scm_default_init_heap_size_2 deprecated
  217. #define scm_default_min_yield_2 deprecated
  218. #define scm_default_max_segment_size deprecated
  219. #endif
  220. SCM_API size_t scm_max_segment_size;
  221. #define SCM_SET_FREELIST_LOC(key,ptr) scm_i_pthread_setspecific ((key), (ptr))
  222. #define SCM_FREELIST_LOC(key) ((SCM *) scm_i_pthread_getspecific (key))
  223. SCM_API scm_i_pthread_key_t scm_i_freelist;
  224. SCM_API scm_i_pthread_key_t scm_i_freelist2;
  225. SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist;
  226. SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist2;
  227. SCM_API unsigned long scm_gc_malloc_collected;
  228. SCM_API int scm_gc_malloc_yield_percentage;
  229. SCM_API unsigned long scm_mallocated;
  230. SCM_API unsigned long scm_mtrigger;
  231. SCM_API SCM scm_after_gc_hook;
  232. SCM_API scm_t_c_hook scm_before_gc_c_hook;
  233. SCM_API scm_t_c_hook scm_before_mark_c_hook;
  234. SCM_API scm_t_c_hook scm_before_sweep_c_hook;
  235. SCM_API scm_t_c_hook scm_after_sweep_c_hook;
  236. SCM_API scm_t_c_hook scm_after_gc_c_hook;
  237. #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
  238. #if (SCM_ENABLE_DEPRECATED == 1)
  239. SCM scm_map_free_list (void);
  240. #else
  241. #define scm_map_free_list deprecated
  242. #define scm_free_list_length deprecated
  243. #endif
  244. #endif
  245. #if (SCM_ENABLE_DEPRECATED == 1) && defined (GUILE_DEBUG_FREELIST)
  246. SCM_API SCM scm_gc_set_debug_check_freelist_x (SCM flag);
  247. #endif
  248. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  249. SCM_API void scm_assert_cell_valid (SCM);
  250. #endif
  251. SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
  252. SCM_API SCM scm_object_address (SCM obj);
  253. SCM_API SCM scm_gc_stats (void);
  254. SCM_API SCM scm_gc_live_object_stats (void);
  255. SCM_API SCM scm_gc (void);
  256. SCM_API void scm_gc_for_alloc (struct scm_t_cell_type_statistics *freelist);
  257. SCM_API SCM scm_gc_for_newcell (struct scm_t_cell_type_statistics *master, SCM *freelist);
  258. SCM_INTERNAL void scm_i_gc (const char *what);
  259. SCM_API void scm_gc_mark (SCM p);
  260. SCM_API void scm_gc_mark_dependencies (SCM p);
  261. SCM_API void scm_mark_locations (SCM_STACKITEM x[], unsigned long n);
  262. SCM_API int scm_in_heap_p (SCM value);
  263. SCM_API void scm_gc_sweep (void);
  264. SCM_API void *scm_malloc (size_t size);
  265. SCM_API void *scm_calloc (size_t size);
  266. SCM_API void *scm_realloc (void *mem, size_t size);
  267. SCM_API char *scm_strdup (const char *str);
  268. SCM_API char *scm_strndup (const char *str, size_t n);
  269. SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
  270. const char *what);
  271. SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
  272. const char *what);
  273. SCM_API void *scm_gc_calloc (size_t size, const char *what);
  274. SCM_API void *scm_gc_malloc (size_t size, const char *what);
  275. SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
  276. size_t new_size, const char *what);
  277. SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
  278. SCM_API char *scm_gc_strdup (const char *str, const char *what);
  279. SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what);
  280. SCM_API void scm_remember_upto_here_1 (SCM obj);
  281. SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
  282. SCM_API void scm_remember_upto_here (SCM obj1, ...);
  283. /* In GCC we can force a reference to an SCM by making it an input to an
  284. empty asm. This avoids the code size and slowdown of an actual function
  285. call. Unfortunately there doesn't seem to be any way to do the varargs
  286. scm_remember_upto_here like this.
  287. __volatile__ ensures nothing will be moved across the asm, and it won't
  288. be optimized away (or only if proved unreachable). Constraint "g" can be
  289. used on all processors and allows any memory or general register (or
  290. immediate) operand. The actual asm syntax doesn't matter, we don't want
  291. to use it, just ensure the operand is still alive. See "Extended Asm" in
  292. the GCC manual for more. */
  293. #ifdef __GNUC__
  294. #define scm_remember_upto_here_1(x) \
  295. do { \
  296. __asm__ __volatile__ ("" : : "g" (x)); \
  297. } while (0)
  298. #define scm_remember_upto_here_2(x, y) \
  299. do { \
  300. scm_remember_upto_here_1 (x); \
  301. scm_remember_upto_here_1 (y); \
  302. } while (0)
  303. #endif
  304. SCM_API SCM scm_return_first (SCM elt, ...);
  305. SCM_API int scm_return_first_int (int x, ...);
  306. SCM_API SCM scm_permanent_object (SCM obj);
  307. SCM_API SCM scm_gc_protect_object (SCM obj);
  308. SCM_API SCM scm_gc_unprotect_object (SCM obj);
  309. SCM_API void scm_gc_register_root (SCM *p);
  310. SCM_API void scm_gc_unregister_root (SCM *p);
  311. SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
  312. SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
  313. SCM_API void scm_storage_prehistory (void);
  314. SCM_API int scm_init_storage (void);
  315. SCM_API void *scm_get_stack_base (void);
  316. SCM_INTERNAL void scm_init_gc (void);
  317. #if SCM_ENABLE_DEPRECATED == 1
  318. SCM_API SCM scm_deprecated_newcell (void);
  319. SCM_API SCM scm_deprecated_newcell2 (void);
  320. #define SCM_NEWCELL(_into) \
  321. do { _into = scm_deprecated_newcell (); } while (0)
  322. #define SCM_NEWCELL2(_into) \
  323. do { _into = scm_deprecated_newcell2 (); } while (0)
  324. SCM_API void * scm_must_malloc (size_t len, const char *what);
  325. SCM_API void * scm_must_realloc (void *where,
  326. size_t olen, size_t len,
  327. const char *what);
  328. SCM_API char *scm_must_strdup (const char *str);
  329. SCM_API char *scm_must_strndup (const char *str, size_t n);
  330. SCM_API void scm_done_malloc (long size);
  331. SCM_API void scm_done_free (long size);
  332. SCM_API void scm_must_free (void *obj);
  333. #endif
  334. #endif /* SCM_GC_H */
  335. /*
  336. Local Variables:
  337. c-file-style: "gnu"
  338. End:
  339. */