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 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
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but 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 02110-1301 USA
  19. */
  20. #include "libguile/__scm.h"
  21. #include "libguile/hooks.h"
  22. #include "libguile/threads.h"
  23. /* Cell allocation and garbage collection work rouhgly in the
  24. following manner:
  25. Each thread has a 'freelist', which is a list of available cells.
  26. (It actually has two freelists, one for single cells and one for
  27. double cells. Everything works analogous for double cells.)
  28. When a thread wants to allocate a cell and the freelist is empty,
  29. it refers to a global list of unswept 'cards'. A card is a small
  30. block of cells that are contigous in memory, together with the
  31. corresponding mark bits. A unswept card is one where the mark bits
  32. are set for cells that have been in use during the last global mark
  33. phase, but the unmarked cells of the card have not been scanned and
  34. freed yet.
  35. The thread takes one of the unswept cards and sweeps it, thereby
  36. building a new freelist that it then uses. Sweeping a card will
  37. call the smob free functions of unmarked cells, for example, and
  38. thus, these free functions can run at any time, in any thread.
  39. When there are no more unswept cards available, the thread performs
  40. a global garbage collection. For this, all other threads are
  41. stopped. A global mark is performed and all cards are put into the
  42. global list of unswept cards. Whennecessary, new cards are
  43. allocated and initialized at this time. The other threads are then
  44. started again.
  45. */
  46. typedef struct scm_t_cell
  47. {
  48. SCM word_0;
  49. SCM word_1;
  50. } scm_t_cell;
  51. /*
  52. CARDS
  53. A card is a small `page' of memory; it will be the unit for lazy
  54. sweeping, generations, etc. The first cell of a card contains a
  55. pointer to the mark bitvector, so that we can find the bitvector
  56. efficiently: we knock off some lowerorder bits.
  57. The size on a 32 bit machine is 256 cells = 2kb. The card [XXX]
  58. */
  59. /* Cray machines have pointers that are incremented once for each
  60. * word, rather than each byte, the 3 most significant bits encode the
  61. * byte within the word. The following macros deal with this by
  62. * storing the native Cray pointers like the ones that looks like scm
  63. * expects. This is done for any pointers that point to a cell,
  64. * pointers to scm_vector elts, functions, &c are not munged.
  65. */
  66. #ifdef _UNICOS
  67. # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x) >> 3))
  68. # define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
  69. #else
  70. # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x)))
  71. # define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
  72. #endif /* def _UNICOS */
  73. #define SCM_GC_CARD_N_HEADER_CELLS 1
  74. #define SCM_GC_CARD_N_CELLS 256
  75. #define SCM_GC_SIZEOF_CARD SCM_GC_CARD_N_CELLS * sizeof (scm_t_cell)
  76. #define SCM_GC_CARD_BVEC(card) ((scm_t_c_bvec_long *) ((card)->word_0))
  77. #define SCM_GC_SET_CARD_BVEC(card, bvec) \
  78. ((card)->word_0 = (SCM) (bvec))
  79. #define SCM_GC_GET_CARD_FLAGS(card) ((long) ((card)->word_1))
  80. #define SCM_GC_SET_CARD_FLAGS(card, flags) \
  81. ((card)->word_1 = (SCM) (flags))
  82. #define SCM_GC_GET_CARD_FLAG(card, shift) \
  83. (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
  84. #define SCM_GC_SET_CARD_FLAG(card, shift) \
  85. (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) | (1L << (shift))))
  86. #define SCM_GC_CLEAR_CARD_FLAG(card, shift) \
  87. (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) & ~(1L << (shift))))
  88. /*
  89. Remove card flags. They hamper lazy initialization, and aren't used
  90. anyways.
  91. */
  92. /* card addressing. for efficiency, cards are *always* aligned to
  93. SCM_GC_CARD_SIZE. */
  94. #define SCM_GC_CARD_SIZE_MASK (SCM_GC_SIZEOF_CARD-1)
  95. #define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
  96. #define SCM_GC_CELL_CARD(x) ((scm_t_cell *) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
  97. #define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
  98. #define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
  99. #define SCM_GC_SET_CELL_BVEC(x, bvec) SCM_GC_SET_CARD_BVEC (SCM_GC_CELL_CARD (x), bvec)
  100. #define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
  101. #define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
  102. #define SCM_GC_CELL_CLEAR_BIT(x) SCM_C_BVEC_CLEAR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
  103. #define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_SIZEOF_CARD - 1)
  104. #define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
  105. /* low level bit banging aids */
  106. typedef unsigned long scm_t_c_bvec_long;
  107. #if (SCM_SIZEOF_UNSIGNED_LONG == 8)
  108. # define SCM_C_BVEC_LONG_BITS 64
  109. # define SCM_C_BVEC_OFFSET_SHIFT 6
  110. # define SCM_C_BVEC_POS_MASK 63
  111. # define SCM_CELL_SIZE_SHIFT 4
  112. #else
  113. # define SCM_C_BVEC_LONG_BITS 32
  114. # define SCM_C_BVEC_OFFSET_SHIFT 5
  115. # define SCM_C_BVEC_POS_MASK 31
  116. # define SCM_CELL_SIZE_SHIFT 3
  117. #endif
  118. #define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
  119. #define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
  120. #define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
  121. #define SCM_C_BVEC_CLEAR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
  122. /* testing and changing GC marks */
  123. #define SCM_GC_MARK_P(x) SCM_GC_CELL_GET_BIT (x)
  124. #define SCM_SET_GC_MARK(x) SCM_GC_CELL_SET_BIT (x)
  125. #define SCM_CLEAR_GC_MARK(x) SCM_GC_CELL_CLEAR_BIT (x)
  126. /* Low level cell data accessing macros. These macros should only be used
  127. * from within code related to garbage collection issues, since they will
  128. * never check the cells they are applied to - not even if guile is compiled
  129. * in debug mode. In particular these macros will even work for free cells,
  130. * which should never be encountered by user code. */
  131. #define SCM_GC_CELL_OBJECT(x, n) (((SCM *)SCM2PTR (x)) [n])
  132. #define SCM_GC_CELL_WORD(x, n) (SCM_UNPACK (SCM_GC_CELL_OBJECT ((x), (n))))
  133. #define SCM_GC_SET_CELL_OBJECT(x, n, v) ((((SCM *)SCM2PTR (x)) [n]) = (v))
  134. #define SCM_GC_SET_CELL_WORD(x, n, v) \
  135. (SCM_GC_SET_CELL_OBJECT ((x), (n), SCM_PACK (v)))
  136. #define SCM_GC_CELL_TYPE(x) (SCM_GC_CELL_OBJECT ((x), 0))
  137. /* Except for the garbage collector, no part of guile should ever run over a
  138. * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
  139. * SCM_SET_CELL_* macros below report an error if they are applied to a free
  140. * cell. Some other plausibility checks are also performed. However, if
  141. * guile is not compiled in debug mode, there won't be any time penalty at all
  142. * when using these macros. */
  143. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  144. # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
  145. #else
  146. # define SCM_VALIDATE_CELL(cell, expr) (expr)
  147. #endif
  148. #define SCM_CELL_WORD(x, n) \
  149. SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
  150. #define SCM_CELL_WORD_0(x) SCM_CELL_WORD ((x), 0)
  151. #define SCM_CELL_WORD_1(x) SCM_CELL_WORD ((x), 1)
  152. #define SCM_CELL_WORD_2(x) SCM_CELL_WORD ((x), 2)
  153. #define SCM_CELL_WORD_3(x) SCM_CELL_WORD ((x), 3)
  154. #define SCM_CELL_OBJECT(x, n) \
  155. SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
  156. #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT ((x), 0)
  157. #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT ((x), 1)
  158. #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT ((x), 2)
  159. #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT ((x), 3)
  160. #define SCM_SET_CELL_WORD(x, n, v) \
  161. SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
  162. #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD ((x), 0, (v))
  163. #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD ((x), 1, (v))
  164. #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD ((x), 2, (v))
  165. #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD ((x), 3, (v))
  166. #define SCM_SET_CELL_OBJECT(x, n, v) \
  167. SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
  168. #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT ((x), 0, (v))
  169. #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT ((x), 1, (v))
  170. #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT ((x), 2, (v))
  171. #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT ((x), 3, (v))
  172. #define SCM_CELL_OBJECT_LOC(x, n) (SCM_VALIDATE_CELL((x), &SCM_GC_CELL_OBJECT ((x), (n))))
  173. #define SCM_CARLOC(x) (SCM_CELL_OBJECT_LOC ((x), 0))
  174. #define SCM_CDRLOC(x) (SCM_CELL_OBJECT_LOC ((x), 1))
  175. #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
  176. #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
  177. /* Freelists consist of linked cells where the type entry holds the value
  178. * scm_tc_free_cell and the second entry holds a pointer to the next cell of
  179. * the freelist. Due to this structure, freelist cells are not cons cells
  180. * and thus may not be accessed using SCM_CAR and SCM_CDR. */
  181. #define SCM_FREE_CELL_CDR(x) \
  182. (SCM_GC_CELL_OBJECT ((x), 1))
  183. #define SCM_SET_FREE_CELL_CDR(x, v) \
  184. (SCM_GC_SET_CELL_OBJECT ((x), 1, (v)))
  185. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  186. /* Set this to != 0 if every cell that is accessed shall be checked:
  187. */
  188. SCM_API int scm_debug_cell_accesses_p;
  189. SCM_API int scm_expensive_debug_cell_accesses_p;
  190. SCM_API int scm_debug_cells_gc_interval ;
  191. void scm_i_expensive_validation_check (SCM cell);
  192. #endif
  193. SCM_API scm_i_pthread_mutex_t scm_i_gc_admin_mutex;
  194. #define scm_gc_running_p (SCM_I_CURRENT_THREAD->gc_running_p)
  195. SCM_API scm_i_pthread_mutex_t scm_i_sweep_mutex;
  196. #ifdef __ia64__
  197. void *scm_ia64_register_backing_store_base (void);
  198. void *scm_ia64_ar_bsp (const void *);
  199. #endif
  200. #if (SCM_ENABLE_DEPRECATED == 1)
  201. SCM_API size_t scm_default_init_heap_size_1;
  202. SCM_API int scm_default_min_yield_1;
  203. SCM_API size_t scm_default_init_heap_size_2;
  204. SCM_API int scm_default_min_yield_2;
  205. SCM_API size_t scm_default_max_segment_size;
  206. #else
  207. #define scm_default_init_heap_size_1 deprecated
  208. #define scm_default_min_yield_1 deprecated
  209. #define scm_default_init_heap_size_2 deprecated
  210. #define scm_default_min_yield_2 deprecated
  211. #define scm_default_max_segment_size deprecated
  212. #endif
  213. SCM_API size_t scm_max_segment_size;
  214. #define SCM_SET_FREELIST_LOC(key,ptr) scm_i_pthread_setspecific ((key), (ptr))
  215. #define SCM_FREELIST_LOC(key) ((SCM *) scm_i_pthread_getspecific (key))
  216. SCM_API scm_i_pthread_key_t scm_i_freelist;
  217. SCM_API scm_i_pthread_key_t scm_i_freelist2;
  218. SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist;
  219. SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist2;
  220. SCM_API unsigned long scm_gc_cells_swept;
  221. SCM_API unsigned long scm_gc_cells_collected;
  222. SCM_API unsigned long scm_gc_malloc_collected;
  223. SCM_API unsigned long scm_gc_ports_collected;
  224. SCM_API unsigned long scm_cells_allocated;
  225. SCM_API unsigned long scm_last_cells_allocated;
  226. SCM_API int scm_gc_cell_yield_percentage;
  227. SCM_API int scm_gc_malloc_yield_percentage;
  228. SCM_API unsigned long scm_mallocated;
  229. SCM_API unsigned long scm_mtrigger;
  230. SCM_API double scm_gc_cells_allocated_acc;
  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_API 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_API 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. */