gc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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,
  5. * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include "libguile/__scm.h"
  23. #include "libguile/hooks.h"
  24. #include "libguile/threads.h"
  25. typedef struct scm_t_cell
  26. {
  27. SCM word_0;
  28. SCM word_1;
  29. } scm_t_cell;
  30. /* FIXME: deprecate. */
  31. #define PTR2SCM(x) (SCM_PACK_POINTER (x))
  32. #define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK_POINTER (x)))
  33. /* Low level cell data accessing macros. These macros should only be used
  34. * from within code related to garbage collection issues, since they will
  35. * never check the cells they are applied to - not even if guile is compiled
  36. * in debug mode. In particular these macros will even work for free cells,
  37. * which should never be encountered by user code. */
  38. #define SCM_GC_CELL_OBJECT(x, n) (((SCM *)SCM2PTR (x)) [n])
  39. #define SCM_GC_CELL_WORD(x, n) (SCM_UNPACK (SCM_GC_CELL_OBJECT ((x), (n))))
  40. #define SCM_GC_SET_CELL_OBJECT(x, n, v) ((((SCM *)SCM2PTR (x)) [n]) = (v))
  41. #define SCM_GC_SET_CELL_WORD(x, n, v) \
  42. (SCM_GC_SET_CELL_OBJECT ((x), (n), SCM_PACK (v)))
  43. #define SCM_GC_CELL_TYPE(x) (SCM_GC_CELL_OBJECT ((x), 0))
  44. /* Except for the garbage collector, no part of guile should ever run over a
  45. * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
  46. * SCM_SET_CELL_* macros below report an error if they are applied to a free
  47. * cell. Some other plausibility checks are also performed. However, if
  48. * guile is not compiled in debug mode, there won't be any time penalty at all
  49. * when using these macros. */
  50. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  51. # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
  52. #else
  53. # define SCM_VALIDATE_CELL(cell, expr) (expr)
  54. #endif
  55. #define SCM_CELL_WORD(x, n) \
  56. SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
  57. #define SCM_CELL_WORD_0(x) SCM_CELL_WORD ((x), 0)
  58. #define SCM_CELL_WORD_1(x) SCM_CELL_WORD ((x), 1)
  59. #define SCM_CELL_WORD_2(x) SCM_CELL_WORD ((x), 2)
  60. #define SCM_CELL_WORD_3(x) SCM_CELL_WORD ((x), 3)
  61. #define SCM_CELL_OBJECT(x, n) \
  62. SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
  63. #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT ((x), 0)
  64. #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT ((x), 1)
  65. #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT ((x), 2)
  66. #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT ((x), 3)
  67. #define SCM_SET_CELL_WORD(x, n, v) \
  68. SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
  69. #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD ((x), 0, (v))
  70. #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD ((x), 1, (v))
  71. #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD ((x), 2, (v))
  72. #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD ((x), 3, (v))
  73. #define SCM_SET_CELL_OBJECT(x, n, v) \
  74. SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
  75. #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT ((x), 0, (v))
  76. #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT ((x), 1, (v))
  77. #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT ((x), 2, (v))
  78. #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT ((x), 3, (v))
  79. #define SCM_CELL_OBJECT_LOC(x, n) (SCM_VALIDATE_CELL((x), &SCM_GC_CELL_OBJECT ((x), (n))))
  80. #define SCM_CARLOC(x) (SCM_CELL_OBJECT_LOC ((x), 0))
  81. #define SCM_CDRLOC(x) (SCM_CELL_OBJECT_LOC ((x), 1))
  82. #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
  83. #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
  84. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  85. /* Set this to != 0 if every cell that is accessed shall be checked:
  86. */
  87. SCM_API int scm_debug_cell_accesses_p;
  88. SCM_API int scm_expensive_debug_cell_accesses_p;
  89. SCM_API int scm_debug_cells_gc_interval ;
  90. SCM_API void scm_i_expensive_validation_check (SCM cell);
  91. #endif
  92. SCM_INTERNAL scm_i_pthread_mutex_t scm_i_gc_admin_mutex;
  93. #define scm_gc_running_p 0
  94. SCM_INTERNAL scm_i_pthread_mutex_t scm_i_sweep_mutex;
  95. #ifdef __ia64__
  96. void *scm_ia64_register_backing_store_base (void);
  97. void *scm_ia64_ar_bsp (const void *);
  98. #endif
  99. SCM_API unsigned long scm_gc_ports_collected;
  100. SCM_API SCM scm_after_gc_hook;
  101. SCM_API scm_t_c_hook scm_before_gc_c_hook;
  102. SCM_API scm_t_c_hook scm_before_mark_c_hook;
  103. SCM_API scm_t_c_hook scm_before_sweep_c_hook;
  104. SCM_API scm_t_c_hook scm_after_sweep_c_hook;
  105. SCM_API scm_t_c_hook scm_after_gc_c_hook;
  106. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  107. SCM_API void scm_assert_cell_valid (SCM);
  108. #endif
  109. SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
  110. SCM_API SCM scm_object_address (SCM obj);
  111. SCM_API SCM scm_gc_enable (void);
  112. SCM_API SCM scm_gc_disable (void);
  113. SCM_API SCM scm_gc_dump (void);
  114. SCM_API SCM scm_gc_stats (void);
  115. SCM_API SCM scm_gc (void);
  116. SCM_INTERNAL void scm_i_gc (const char *what);
  117. SCM_API void scm_gc_mark (SCM p);
  118. SCM_API void scm_gc_sweep (void);
  119. SCM_API void scm_gc_register_allocation (size_t size);
  120. SCM_API void *scm_malloc (size_t size) SCM_MALLOC;
  121. SCM_API void *scm_calloc (size_t size) SCM_MALLOC;
  122. SCM_API void *scm_realloc (void *mem, size_t size);
  123. SCM_API char *scm_strdup (const char *str) SCM_MALLOC;
  124. SCM_API char *scm_strndup (const char *str, size_t n) SCM_MALLOC;
  125. SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
  126. const char *what);
  127. SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
  128. const char *what);
  129. SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what)
  130. SCM_MALLOC;
  131. SCM_API void *scm_gc_calloc (size_t size, const char *what)
  132. SCM_MALLOC;
  133. SCM_API void *scm_gc_malloc (size_t size, const char *what)
  134. SCM_MALLOC;
  135. SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
  136. size_t new_size, const char *what);
  137. SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
  138. SCM_API char *scm_gc_strdup (const char *str, const char *what)
  139. SCM_MALLOC;
  140. SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what)
  141. SCM_MALLOC;
  142. #define scm_gc_typed_calloc(t) ((t *) scm_gc_calloc (sizeof (t), #t))
  143. #ifdef BUILDING_LIBGUILE
  144. #include "libguile/bdw-gc.h"
  145. #define SCM_GC_MALLOC(size) GC_MALLOC (size)
  146. #define SCM_GC_MALLOC_POINTERLESS(size) GC_MALLOC_ATOMIC (size)
  147. #else
  148. #define SCM_GC_MALLOC(size) scm_gc_malloc (size, NULL)
  149. #define SCM_GC_MALLOC_POINTERLESS(size) scm_gc_malloc_pointerless (size, NULL)
  150. #endif
  151. SCM_INLINE SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
  152. SCM_INLINE SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
  153. scm_t_bits ccr, scm_t_bits cdr);
  154. SCM_INLINE SCM scm_words (scm_t_bits car, scm_t_uint32 n_words);
  155. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  156. SCM_INLINE_IMPLEMENTATION SCM
  157. scm_cell (scm_t_bits car, scm_t_bits cdr)
  158. {
  159. SCM cell = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_cell)));
  160. /* Initialize the type slot last so that the cell is ignored by the GC
  161. until it is completely initialized. This is only relevant when the GC
  162. can actually run during this code, which it can't since the GC only runs
  163. when all other threads are stopped. */
  164. SCM_GC_SET_CELL_WORD (cell, 1, cdr);
  165. SCM_GC_SET_CELL_WORD (cell, 0, car);
  166. return cell;
  167. }
  168. SCM_INLINE_IMPLEMENTATION SCM
  169. scm_double_cell (scm_t_bits car, scm_t_bits cbr,
  170. scm_t_bits ccr, scm_t_bits cdr)
  171. {
  172. SCM z;
  173. z = SCM_PACK_POINTER (SCM_GC_MALLOC (2 * sizeof (scm_t_cell)));
  174. /* Initialize the type slot last so that the cell is ignored by the
  175. GC until it is completely initialized. This is only relevant
  176. when the GC can actually run during this code, which it can't
  177. since the GC only runs when all other threads are stopped.
  178. */
  179. SCM_GC_SET_CELL_WORD (z, 1, cbr);
  180. SCM_GC_SET_CELL_WORD (z, 2, ccr);
  181. SCM_GC_SET_CELL_WORD (z, 3, cdr);
  182. SCM_GC_SET_CELL_WORD (z, 0, car);
  183. /* When this function is inlined, it's possible that the last
  184. SCM_GC_SET_CELL_WORD above will be adjacent to a following
  185. initialization of z. E.g., it occurred in scm_make_real. GCC
  186. from around version 3 (e.g., certainly 3.2) began taking
  187. advantage of strict C aliasing rules which say that it's OK to
  188. interchange the initialization above and the one below when the
  189. pointer types appear to differ sufficiently. We don't want that,
  190. of course. GCC allows this behaviour to be disabled with the
  191. -fno-strict-aliasing option, but would also need to be supplied
  192. by Guile users. Instead, the following statements prevent the
  193. reordering.
  194. */
  195. #ifdef __GNUC__
  196. __asm__ volatile ("" : : : "memory");
  197. #else
  198. /* portable version, just in case any other compiler does the same
  199. thing. */
  200. scm_remember_upto_here_1 (z);
  201. #endif
  202. return z;
  203. }
  204. SCM_INLINE_IMPLEMENTATION SCM
  205. scm_words (scm_t_bits car, scm_t_uint32 n_words)
  206. {
  207. SCM z;
  208. z = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_bits) * n_words));
  209. SCM_GC_SET_CELL_WORD (z, 0, car);
  210. /* FIXME: is the following concern even relevant with BDW-GC? */
  211. /* When this function is inlined, it's possible that the last
  212. SCM_GC_SET_CELL_WORD above will be adjacent to a following
  213. initialization of z. E.g., it occurred in scm_make_real. GCC
  214. from around version 3 (e.g., certainly 3.2) began taking
  215. advantage of strict C aliasing rules which say that it's OK to
  216. interchange the initialization above and the one below when the
  217. pointer types appear to differ sufficiently. We don't want that,
  218. of course. GCC allows this behaviour to be disabled with the
  219. -fno-strict-aliasing option, but would also need to be supplied
  220. by Guile users. Instead, the following statements prevent the
  221. reordering.
  222. */
  223. #ifdef __GNUC__
  224. __asm__ volatile ("" : : : "memory");
  225. #else
  226. /* portable version, just in case any other compiler does the same
  227. thing. */
  228. scm_remember_upto_here_1 (z);
  229. #endif
  230. return z;
  231. }
  232. #endif /* SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES */
  233. SCM_API void scm_remember_upto_here_1 (SCM obj);
  234. SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
  235. SCM_API void scm_remember_upto_here (SCM obj1, ...);
  236. /* In GCC we can force a reference to an SCM by making it an input to an
  237. empty asm. This avoids the code size and slowdown of an actual function
  238. call. Unfortunately there doesn't seem to be any way to do the varargs
  239. scm_remember_upto_here like this.
  240. __volatile__ ensures nothing will be moved across the asm, and it won't
  241. be optimized away (or only if proved unreachable). Constraint "g" can be
  242. used on all processors and allows any memory or general register (or
  243. immediate) operand. The actual asm syntax doesn't matter, we don't want
  244. to use it, just ensure the operand is still alive. See "Extended Asm" in
  245. the GCC manual for more. */
  246. #ifdef __GNUC__
  247. #define scm_remember_upto_here_1(x) \
  248. do { \
  249. __asm__ __volatile__ ("" : : "g" (x)); \
  250. } while (0)
  251. #define scm_remember_upto_here_2(x, y) \
  252. do { \
  253. scm_remember_upto_here_1 (x); \
  254. scm_remember_upto_here_1 (y); \
  255. } while (0)
  256. #endif
  257. SCM_API SCM scm_return_first (SCM elt, ...);
  258. SCM_API int scm_return_first_int (int x, ...);
  259. SCM_API SCM scm_permanent_object (SCM obj);
  260. SCM_API SCM scm_gc_protect_object (SCM obj);
  261. SCM_API SCM scm_gc_unprotect_object (SCM obj);
  262. SCM_API void scm_gc_register_root (SCM *p);
  263. SCM_API void scm_gc_unregister_root (SCM *p);
  264. SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
  265. SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
  266. SCM_INTERNAL void scm_gc_after_nonlocal_exit (void);
  267. SCM_INTERNAL void scm_storage_prehistory (void);
  268. SCM_INTERNAL void scm_init_gc_protect_object (void);
  269. SCM_INTERNAL void scm_init_gc (void);
  270. #endif /* SCM_GC_H */
  271. /*
  272. Local Variables:
  273. c-file-style: "gnu"
  274. End:
  275. */