gc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_CARLOC(x) (SCM_CELL_OBJECT_LOC ((x), 0))
  61. #define SCM_CDRLOC(x) (SCM_CELL_OBJECT_LOC ((x), 1))
  62. #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
  63. #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
  64. SCM_API unsigned long scm_gc_ports_collected;
  65. SCM_API SCM scm_after_gc_hook;
  66. SCM_API scm_t_c_hook scm_before_gc_c_hook;
  67. SCM_API scm_t_c_hook scm_before_mark_c_hook;
  68. SCM_API scm_t_c_hook scm_before_sweep_c_hook;
  69. SCM_API scm_t_c_hook scm_after_sweep_c_hook;
  70. SCM_API scm_t_c_hook scm_after_gc_c_hook;
  71. SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
  72. SCM_API SCM scm_object_address (SCM obj);
  73. SCM_API SCM scm_gc_enable (void);
  74. SCM_API SCM scm_gc_disable (void);
  75. SCM_API SCM scm_gc_dump (void);
  76. SCM_API SCM scm_gc_stats (void);
  77. SCM_API SCM scm_gc (void);
  78. SCM_INTERNAL void scm_i_gc (const char *what);
  79. SCM_API void scm_gc_mark (SCM p);
  80. SCM_API void scm_gc_sweep (void);
  81. SCM_API void scm_gc_register_allocation (size_t size);
  82. SCM_API void *scm_malloc (size_t size) SCM_MALLOC;
  83. SCM_API void *scm_calloc (size_t size) SCM_MALLOC;
  84. SCM_API void *scm_realloc (void *mem, size_t size);
  85. SCM_API char *scm_strdup (const char *str) SCM_MALLOC;
  86. SCM_API char *scm_strndup (const char *str, size_t n) SCM_MALLOC;
  87. SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
  88. const char *what);
  89. SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
  90. const char *what);
  91. SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what)
  92. SCM_MALLOC;
  93. SCM_API void *scm_gc_calloc (size_t size, const char *what)
  94. SCM_MALLOC;
  95. SCM_API void *scm_gc_malloc (size_t size, const char *what)
  96. SCM_MALLOC;
  97. SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
  98. size_t new_size, const char *what);
  99. SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
  100. SCM_API char *scm_gc_strdup (const char *str, const char *what)
  101. SCM_MALLOC;
  102. SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what)
  103. SCM_MALLOC;
  104. #define scm_gc_typed_calloc(t) ((t *) scm_gc_calloc (sizeof (t), #t))
  105. #ifdef BUILDING_LIBGUILE
  106. #include "libguile/bdw-gc.h"
  107. #define SCM_GC_MALLOC(size) GC_MALLOC (size)
  108. #define SCM_GC_MALLOC_POINTERLESS(size) GC_MALLOC_ATOMIC (size)
  109. #else
  110. #define SCM_GC_MALLOC(size) scm_gc_malloc (size, NULL)
  111. #define SCM_GC_MALLOC_POINTERLESS(size) scm_gc_malloc_pointerless (size, NULL)
  112. #endif
  113. SCM_INLINE SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
  114. SCM_INLINE SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
  115. scm_t_bits ccr, scm_t_bits cdr);
  116. SCM_INLINE SCM scm_words (scm_t_bits car, uint32_t n_words);
  117. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  118. SCM_INLINE_IMPLEMENTATION SCM
  119. scm_cell (scm_t_bits car, scm_t_bits cdr)
  120. {
  121. SCM cell = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_cell)));
  122. /* Initialize the type slot last so that the cell is ignored by the GC
  123. until it is completely initialized. This is only relevant when the GC
  124. can actually run during this code, which it can't since the GC only runs
  125. when all other threads are stopped. */
  126. SCM_GC_SET_CELL_WORD (cell, 1, cdr);
  127. SCM_GC_SET_CELL_WORD (cell, 0, car);
  128. return cell;
  129. }
  130. SCM_INLINE_IMPLEMENTATION SCM
  131. scm_double_cell (scm_t_bits car, scm_t_bits cbr,
  132. scm_t_bits ccr, scm_t_bits cdr)
  133. {
  134. SCM z;
  135. z = SCM_PACK_POINTER (SCM_GC_MALLOC (2 * sizeof (scm_t_cell)));
  136. /* Initialize the type slot last so that the cell is ignored by the
  137. GC until it is completely initialized. This is only relevant
  138. when the GC can actually run during this code, which it can't
  139. since the GC only runs when all other threads are stopped.
  140. */
  141. SCM_GC_SET_CELL_WORD (z, 1, cbr);
  142. SCM_GC_SET_CELL_WORD (z, 2, ccr);
  143. SCM_GC_SET_CELL_WORD (z, 3, cdr);
  144. SCM_GC_SET_CELL_WORD (z, 0, car);
  145. /* When this function is inlined, it's possible that the last
  146. SCM_GC_SET_CELL_WORD above will be adjacent to a following
  147. initialization of z. E.g., it occurred in scm_make_real. GCC
  148. from around version 3 (e.g., certainly 3.2) began taking
  149. advantage of strict C aliasing rules which say that it's OK to
  150. interchange the initialization above and the one below when the
  151. pointer types appear to differ sufficiently. We don't want that,
  152. of course. GCC allows this behaviour to be disabled with the
  153. -fno-strict-aliasing option, but would also need to be supplied
  154. by Guile users. Instead, the following statements prevent the
  155. reordering.
  156. */
  157. #ifdef __GNUC__
  158. __asm__ volatile ("" : : : "memory");
  159. #else
  160. /* portable version, just in case any other compiler does the same
  161. thing. */
  162. scm_remember_upto_here_1 (z);
  163. #endif
  164. return z;
  165. }
  166. SCM_INLINE_IMPLEMENTATION SCM
  167. scm_words (scm_t_bits car, uint32_t n_words)
  168. {
  169. SCM z;
  170. z = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_bits) * n_words));
  171. SCM_GC_SET_CELL_WORD (z, 0, car);
  172. /* FIXME: is the following concern even relevant with BDW-GC? */
  173. /* When this function is inlined, it's possible that the last
  174. SCM_GC_SET_CELL_WORD above will be adjacent to a following
  175. initialization of z. E.g., it occurred in scm_make_real. GCC
  176. from around version 3 (e.g., certainly 3.2) began taking
  177. advantage of strict C aliasing rules which say that it's OK to
  178. interchange the initialization above and the one below when the
  179. pointer types appear to differ sufficiently. We don't want that,
  180. of course. GCC allows this behaviour to be disabled with the
  181. -fno-strict-aliasing option, but would also need to be supplied
  182. by Guile users. Instead, the following statements prevent the
  183. reordering.
  184. */
  185. #ifdef __GNUC__
  186. __asm__ volatile ("" : : : "memory");
  187. #else
  188. /* portable version, just in case any other compiler does the same
  189. thing. */
  190. scm_remember_upto_here_1 (z);
  191. #endif
  192. return z;
  193. }
  194. #endif /* SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES */
  195. SCM_API void scm_remember_upto_here_1 (SCM obj);
  196. SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
  197. SCM_API void scm_remember_upto_here (SCM obj1, ...);
  198. /* In GCC we can force a reference to an SCM by making it an input to an
  199. empty asm. This avoids the code size and slowdown of an actual function
  200. call. Unfortunately there doesn't seem to be any way to do the varargs
  201. scm_remember_upto_here like this.
  202. __volatile__ ensures nothing will be moved across the asm, and it won't
  203. be optimized away (or only if proved unreachable). Constraint "g" can be
  204. used on all processors and allows any memory or general register (or
  205. immediate) operand. The actual asm syntax doesn't matter, we don't want
  206. to use it, just ensure the operand is still alive. See "Extended Asm" in
  207. the GCC manual for more. */
  208. #ifdef __GNUC__
  209. #define scm_remember_upto_here_1(x) \
  210. do { \
  211. __asm__ __volatile__ ("" : : "g" (x)); \
  212. } while (0)
  213. #define scm_remember_upto_here_2(x, y) \
  214. do { \
  215. scm_remember_upto_here_1 (x); \
  216. scm_remember_upto_here_1 (y); \
  217. } while (0)
  218. #endif
  219. SCM_API SCM scm_return_first (SCM elt, ...);
  220. SCM_API int scm_return_first_int (int x, ...);
  221. SCM_API SCM scm_permanent_object (SCM obj);
  222. SCM_API SCM scm_gc_protect_object (SCM obj);
  223. SCM_API SCM scm_gc_unprotect_object (SCM obj);
  224. SCM_API void scm_gc_register_root (SCM *p);
  225. SCM_API void scm_gc_unregister_root (SCM *p);
  226. SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
  227. SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
  228. SCM_INTERNAL void scm_gc_after_nonlocal_exit (void);
  229. SCM_INTERNAL void scm_storage_prehistory (void);
  230. SCM_INTERNAL void scm_init_gc_protect_object (void);
  231. SCM_INTERNAL void scm_init_gc (void);
  232. #endif /* SCM_GC_H */