gc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #ifndef SCM_GC_H
  2. #define SCM_GC_H
  3. /* Copyright 1995-1996,1998-2004,2006-2014,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/inline.h"
  18. #include "libguile/chooks.h"
  19. /* Before Guile 2.0, Guile had a custom garbage collector and memory
  20. management system that largely worked in terms of "cells", two-word
  21. heap-tagged objects. This is no longer the case, and the "cell"
  22. concept is obsolete; the allocator can now make objects of any size.
  23. Still, some old code uses "cell" to mean a two-word allocation, so
  24. for that reason you'll see the word around Guile. */
  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. #define SCM_GC_CELL_OBJECT(x, n) (((SCM *)SCM2PTR (x)) [n])
  34. #define SCM_GC_CELL_WORD(x, n) (SCM_UNPACK (SCM_GC_CELL_OBJECT ((x), (n))))
  35. #define SCM_GC_SET_CELL_OBJECT(x, n, v) ((((SCM *)SCM2PTR (x)) [n]) = (v))
  36. #define SCM_GC_SET_CELL_WORD(x, n, v) \
  37. (SCM_GC_SET_CELL_OBJECT ((x), (n), SCM_PACK (v)))
  38. #define SCM_GC_CELL_TYPE(x) (SCM_GC_CELL_OBJECT ((x), 0))
  39. #define SCM_CELL_WORD(x, n) SCM_GC_CELL_WORD ((x), (n))
  40. #define SCM_CELL_WORD_0(x) SCM_CELL_WORD ((x), 0)
  41. #define SCM_CELL_WORD_1(x) SCM_CELL_WORD ((x), 1)
  42. #define SCM_CELL_WORD_2(x) SCM_CELL_WORD ((x), 2)
  43. #define SCM_CELL_WORD_3(x) SCM_CELL_WORD ((x), 3)
  44. #define SCM_CELL_OBJECT(x, n) SCM_GC_CELL_OBJECT ((x), (n))
  45. #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT ((x), 0)
  46. #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT ((x), 1)
  47. #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT ((x), 2)
  48. #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT ((x), 3)
  49. #define SCM_SET_CELL_WORD(x, n, v) SCM_GC_SET_CELL_WORD ((x), (n), (v))
  50. #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD ((x), 0, (v))
  51. #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD ((x), 1, (v))
  52. #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD ((x), 2, (v))
  53. #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD ((x), 3, (v))
  54. #define SCM_SET_CELL_OBJECT(x, n, v) SCM_GC_SET_CELL_OBJECT ((x), (n), (v))
  55. #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT ((x), 0, (v))
  56. #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT ((x), 1, (v))
  57. #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT ((x), 2, (v))
  58. #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT ((x), 3, (v))
  59. #define SCM_CELL_OBJECT_LOC(x, n) (&SCM_GC_CELL_OBJECT ((x), (n)))
  60. #define SCM_ADD_POINTER_TAG(tag, x) (SCM_PACK (SCM_UNPACK (x) + (tag)))
  61. #define SCM_REMOVE_POINTER_TAG(tag, x) (SCM_PACK (SCM_UNPACK (x) - (tag)))
  62. #define SCM_ADD_PAIR_TAG(x) (SCM_ADD_POINTER_TAG (scm_pair_tag, (x)))
  63. #define SCM_REMOVE_PAIR_TAG(x) (SCM_REMOVE_POINTER_TAG (scm_pair_tag, (x)))
  64. #define SCM_CARLOC(x) (SCM_CELL_OBJECT_LOC (SCM_REMOVE_PAIR_TAG (x), 0))
  65. #define SCM_CDRLOC(x) (SCM_CELL_OBJECT_LOC (SCM_REMOVE_PAIR_TAG (x), 1))
  66. #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
  67. #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
  68. SCM_API unsigned long scm_gc_ports_collected;
  69. SCM_API SCM scm_after_gc_hook;
  70. SCM_API scm_t_c_hook scm_before_gc_c_hook;
  71. SCM_API scm_t_c_hook scm_before_mark_c_hook;
  72. SCM_API scm_t_c_hook scm_before_sweep_c_hook;
  73. SCM_API scm_t_c_hook scm_after_sweep_c_hook;
  74. SCM_API scm_t_c_hook scm_after_gc_c_hook;
  75. SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
  76. SCM_API SCM scm_object_address (SCM obj);
  77. SCM_API SCM scm_gc_enable (void);
  78. SCM_API SCM scm_gc_disable (void);
  79. SCM_API SCM scm_gc_dump (void);
  80. SCM_API SCM scm_gc_stats (void);
  81. SCM_API SCM scm_gc (void);
  82. SCM_INTERNAL void scm_i_gc (const char *what);
  83. SCM_API void scm_gc_mark (SCM p);
  84. SCM_API void scm_gc_sweep (void);
  85. SCM_API void scm_gc_register_allocation (size_t size);
  86. SCM_API void *scm_malloc (size_t size) SCM_MALLOC;
  87. SCM_API void *scm_calloc (size_t size) SCM_MALLOC;
  88. SCM_API void *scm_realloc (void *mem, size_t size);
  89. SCM_API char *scm_strdup (const char *str) SCM_MALLOC;
  90. SCM_API char *scm_strndup (const char *str, size_t n) SCM_MALLOC;
  91. SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
  92. const char *what);
  93. SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
  94. const char *what);
  95. SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what)
  96. SCM_MALLOC;
  97. SCM_API void *scm_gc_calloc (size_t size, const char *what)
  98. SCM_MALLOC;
  99. SCM_API void *scm_gc_malloc (size_t size, const char *what)
  100. SCM_MALLOC;
  101. SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
  102. size_t new_size, const char *what);
  103. SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
  104. SCM_API char *scm_gc_strdup (const char *str, const char *what)
  105. SCM_MALLOC;
  106. SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what)
  107. SCM_MALLOC;
  108. #define scm_gc_typed_calloc(t) ((t *) scm_gc_calloc (sizeof (t), #t))
  109. #ifdef BUILDING_LIBGUILE
  110. #include "libguile/bdw-gc.h"
  111. #define SCM_GC_MALLOC(size) GC_MALLOC (size)
  112. #define SCM_GC_MALLOC_POINTERLESS(size) GC_MALLOC_ATOMIC (size)
  113. #else
  114. #define SCM_GC_MALLOC(size) scm_gc_malloc (size, NULL)
  115. #define SCM_GC_MALLOC_POINTERLESS(size) scm_gc_malloc_pointerless (size, NULL)
  116. #endif
  117. SCM_INLINE SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
  118. SCM_INLINE SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
  119. scm_t_bits ccr, scm_t_bits cdr);
  120. SCM_INLINE SCM scm_words (scm_t_bits car, uint32_t n_words);
  121. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  122. SCM_INLINE_IMPLEMENTATION SCM
  123. scm_cell (scm_t_bits car, scm_t_bits cdr)
  124. {
  125. SCM cell = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_cell)));
  126. /* Initialize the type slot last so that the cell is ignored by the GC
  127. until it is completely initialized. This is only relevant when the GC
  128. can actually run during this code, which it can't since the GC only runs
  129. when all other threads are stopped. */
  130. SCM_GC_SET_CELL_WORD (cell, 1, cdr);
  131. SCM_GC_SET_CELL_WORD (cell, 0, car);
  132. return cell;
  133. }
  134. SCM_INLINE_IMPLEMENTATION SCM
  135. scm_double_cell (scm_t_bits car, scm_t_bits cbr,
  136. scm_t_bits ccr, scm_t_bits cdr)
  137. {
  138. SCM z;
  139. z = SCM_PACK_POINTER (SCM_GC_MALLOC (2 * sizeof (scm_t_cell)));
  140. /* Initialize the type slot last so that the cell is ignored by the
  141. GC until it is completely initialized. This is only relevant
  142. when the GC can actually run during this code, which it can't
  143. since the GC only runs when all other threads are stopped.
  144. */
  145. SCM_GC_SET_CELL_WORD (z, 1, cbr);
  146. SCM_GC_SET_CELL_WORD (z, 2, ccr);
  147. SCM_GC_SET_CELL_WORD (z, 3, cdr);
  148. SCM_GC_SET_CELL_WORD (z, 0, car);
  149. /* When this function is inlined, it's possible that the last
  150. SCM_GC_SET_CELL_WORD above will be adjacent to a following
  151. initialization of z. E.g., it occurred in scm_make_real. GCC
  152. from around version 3 (e.g., certainly 3.2) began taking
  153. advantage of strict C aliasing rules which say that it's OK to
  154. interchange the initialization above and the one below when the
  155. pointer types appear to differ sufficiently. We don't want that,
  156. of course. GCC allows this behaviour to be disabled with the
  157. -fno-strict-aliasing option, but would also need to be supplied
  158. by Guile users. Instead, the following statements prevent the
  159. reordering.
  160. */
  161. #ifdef __GNUC__
  162. __asm__ volatile ("" : : : "memory");
  163. #else
  164. /* portable version, just in case any other compiler does the same
  165. thing. */
  166. scm_remember_upto_here_1 (z);
  167. #endif
  168. return z;
  169. }
  170. SCM_INLINE_IMPLEMENTATION SCM
  171. scm_words (scm_t_bits car, uint32_t n_words)
  172. {
  173. SCM z;
  174. z = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_bits) * n_words));
  175. SCM_GC_SET_CELL_WORD (z, 0, car);
  176. /* FIXME: is the following concern even relevant with BDW-GC? */
  177. /* When this function is inlined, it's possible that the last
  178. SCM_GC_SET_CELL_WORD above will be adjacent to a following
  179. initialization of z. E.g., it occurred in scm_make_real. GCC
  180. from around version 3 (e.g., certainly 3.2) began taking
  181. advantage of strict C aliasing rules which say that it's OK to
  182. interchange the initialization above and the one below when the
  183. pointer types appear to differ sufficiently. We don't want that,
  184. of course. GCC allows this behaviour to be disabled with the
  185. -fno-strict-aliasing option, but would also need to be supplied
  186. by Guile users. Instead, the following statements prevent the
  187. reordering.
  188. */
  189. #ifdef __GNUC__
  190. __asm__ volatile ("" : : : "memory");
  191. #else
  192. /* portable version, just in case any other compiler does the same
  193. thing. */
  194. scm_remember_upto_here_1 (z);
  195. #endif
  196. return z;
  197. }
  198. #endif /* SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES */
  199. SCM_API void scm_remember_upto_here_1 (SCM obj);
  200. SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
  201. SCM_API void scm_remember_upto_here (SCM obj1, ...);
  202. /* In GCC we can force a reference to an SCM by making it an input to an
  203. empty asm. This avoids the code size and slowdown of an actual function
  204. call. Unfortunately there doesn't seem to be any way to do the varargs
  205. scm_remember_upto_here like this.
  206. __volatile__ ensures nothing will be moved across the asm, and it won't
  207. be optimized away (or only if proved unreachable). Constraint "g" can be
  208. used on all processors and allows any memory or general register (or
  209. immediate) operand. The actual asm syntax doesn't matter, we don't want
  210. to use it, just ensure the operand is still alive. See "Extended Asm" in
  211. the GCC manual for more. */
  212. #ifdef __GNUC__
  213. #define scm_remember_upto_here_1(x) \
  214. do { \
  215. __asm__ __volatile__ ("" : : "g" (x)); \
  216. } while (0)
  217. #define scm_remember_upto_here_2(x, y) \
  218. do { \
  219. scm_remember_upto_here_1 (x); \
  220. scm_remember_upto_here_1 (y); \
  221. } while (0)
  222. #endif
  223. SCM_API SCM scm_return_first (SCM elt, ...);
  224. SCM_API int scm_return_first_int (int x, ...);
  225. SCM_API SCM scm_permanent_object (SCM obj);
  226. SCM_API SCM scm_gc_protect_object (SCM obj);
  227. SCM_API SCM scm_gc_unprotect_object (SCM obj);
  228. SCM_API void scm_gc_register_root (SCM *p);
  229. SCM_API void scm_gc_unregister_root (SCM *p);
  230. SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
  231. SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
  232. SCM_INTERNAL void scm_gc_after_nonlocal_exit (void);
  233. SCM_INTERNAL void scm_storage_prehistory (void);
  234. SCM_INTERNAL void scm_init_gc_protect_object (void);
  235. SCM_INTERNAL void scm_init_gc (void);
  236. #endif /* SCM_GC_H */