ggc.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Garbage collection for the GNU compiler.
  2. Copyright (C) 1998-2019 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_GGC_H
  16. #define GCC_GGC_H
  17. /* Symbols are marked with `ggc' for `gcc gc' so as not to interfere with
  18. an external gc library that might be linked in. */
  19. /* Internal functions and data structures used by the GTY
  20. machinery, including the generated gt*.[hc] files. */
  21. #include "gtype-desc.h"
  22. /* One of these applies its third parameter (with cookie in the fourth
  23. parameter) to each pointer in the object pointed to by the first
  24. parameter, using the second parameter. */
  25. typedef void (*gt_note_pointers) (void *, void *, gt_pointer_operator,
  26. void *);
  27. /* One of these is called before objects are re-ordered in memory.
  28. The first parameter is the original object, the second is the
  29. subobject that has had its pointers reordered, the third parameter
  30. can compute the new values of a pointer when given the cookie in
  31. the fourth parameter. */
  32. typedef void (*gt_handle_reorder) (void *, void *, gt_pointer_operator,
  33. void *);
  34. /* Used by the gt_pch_n_* routines. Register an object in the hash table. */
  35. extern int gt_pch_note_object (void *, void *, gt_note_pointers);
  36. /* Used by the gt_pch_n_* routines. Register that an object has a reorder
  37. function. */
  38. extern void gt_pch_note_reorder (void *, void *, gt_handle_reorder);
  39. /* generated function to clear caches in gc memory. */
  40. extern void gt_clear_caches ();
  41. /* Mark the object in the first parameter and anything it points to. */
  42. typedef void (*gt_pointer_walker) (void *);
  43. /* Structures for the easy way to mark roots.
  44. In an array, terminated by having base == NULL. */
  45. struct ggc_root_tab {
  46. void *base;
  47. size_t nelt;
  48. size_t stride;
  49. gt_pointer_walker cb;
  50. gt_pointer_walker pchw;
  51. };
  52. #define LAST_GGC_ROOT_TAB { NULL, 0, 0, NULL, NULL }
  53. /* Pointers to arrays of ggc_root_tab, terminated by NULL. */
  54. extern const struct ggc_root_tab * const gt_ggc_rtab[];
  55. extern const struct ggc_root_tab * const gt_ggc_deletable_rtab[];
  56. extern const struct ggc_root_tab * const gt_pch_scalar_rtab[];
  57. /* If EXPR is not NULL and previously unmarked, mark it and evaluate
  58. to true. Otherwise evaluate to false. */
  59. #define ggc_test_and_set_mark(EXPR) \
  60. ((EXPR) != NULL && ((void *) (EXPR)) != (void *) 1 && ! ggc_set_mark (EXPR))
  61. #define ggc_mark(EXPR) \
  62. do { \
  63. const void *const a__ = (EXPR); \
  64. if (a__ != NULL && a__ != (void *) 1) \
  65. ggc_set_mark (a__); \
  66. } while (0)
  67. /* Actually set the mark on a particular region of memory, but don't
  68. follow pointers. This function is called by ggc_mark_*. It
  69. returns zero if the object was not previously marked; nonzero if
  70. the object was already marked, or if, for any other reason,
  71. pointers in this data structure should not be traversed. */
  72. extern int ggc_set_mark (const void *);
  73. /* Return 1 if P has been marked, zero otherwise.
  74. P must have been allocated by the GC allocator; it mustn't point to
  75. static objects, stack variables, or memory allocated with malloc. */
  76. extern int ggc_marked_p (const void *);
  77. /* PCH and GGC handling for strings, mostly trivial. */
  78. extern void gt_pch_n_S (const void *);
  79. extern void gt_ggc_m_S (const void *);
  80. /* End of GTY machinery API. */
  81. /* Initialize the string pool. */
  82. extern void init_stringpool (void);
  83. /* Initialize the garbage collector. */
  84. extern void init_ggc (void);
  85. /* When true, identifier nodes are considered as GC roots. When
  86. false, identifier nodes are treated like any other GC-allocated
  87. object, and the identifier hash table is treated as a weak
  88. hash. */
  89. extern bool ggc_protect_identifiers;
  90. /* Write out all GCed objects to F. */
  91. extern void gt_pch_save (FILE *f);
  92. /* Allocation. */
  93. /* The internal primitive. */
  94. extern void *ggc_internal_alloc (size_t, void (*)(void *), size_t,
  95. size_t CXX_MEM_STAT_INFO)
  96. ATTRIBUTE_MALLOC;
  97. inline void *
  98. ggc_internal_alloc (size_t s CXX_MEM_STAT_INFO)
  99. {
  100. return ggc_internal_alloc (s, NULL, 0, 1 PASS_MEM_STAT);
  101. }
  102. extern size_t ggc_round_alloc_size (size_t requested_size);
  103. /* Allocates cleared memory. */
  104. extern void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
  105. size_t, size_t
  106. CXX_MEM_STAT_INFO) ATTRIBUTE_MALLOC;
  107. inline void *
  108. ggc_internal_cleared_alloc (size_t s CXX_MEM_STAT_INFO)
  109. {
  110. return ggc_internal_cleared_alloc (s, NULL, 0, 1 PASS_MEM_STAT);
  111. }
  112. /* Resize a block. */
  113. extern void *ggc_realloc (void *, size_t CXX_MEM_STAT_INFO);
  114. /* Free a block. To be used when known for certain it's not reachable. */
  115. extern void ggc_free (void *);
  116. extern void dump_ggc_loc_statistics (bool);
  117. /* Reallocator. */
  118. #define GGC_RESIZEVEC(T, P, N) \
  119. ((T *) ggc_realloc ((P), (N) * sizeof (T) MEM_STAT_INFO))
  120. template<typename T>
  121. void
  122. finalize (void *p)
  123. {
  124. static_cast<T *> (p)->~T ();
  125. }
  126. template<typename T>
  127. inline bool
  128. need_finalization_p ()
  129. {
  130. #if GCC_VERSION >= 4003
  131. return !__has_trivial_destructor (T);
  132. #else
  133. return true;
  134. #endif
  135. }
  136. template<typename T>
  137. inline T *
  138. ggc_alloc (ALONE_CXX_MEM_STAT_INFO)
  139. {
  140. if (need_finalization_p<T> ())
  141. return static_cast<T *> (ggc_internal_alloc (sizeof (T), finalize<T>, 0, 1
  142. PASS_MEM_STAT));
  143. else
  144. return static_cast<T *> (ggc_internal_alloc (sizeof (T), NULL, 0, 1
  145. PASS_MEM_STAT));
  146. }
  147. template<typename T>
  148. inline T *
  149. ggc_cleared_alloc (ALONE_CXX_MEM_STAT_INFO)
  150. {
  151. if (need_finalization_p<T> ())
  152. return static_cast<T *> (ggc_internal_cleared_alloc (sizeof (T),
  153. finalize<T>, 0, 1
  154. PASS_MEM_STAT));
  155. else
  156. return static_cast<T *> (ggc_internal_cleared_alloc (sizeof (T), NULL, 0, 1
  157. PASS_MEM_STAT));
  158. }
  159. template<typename T>
  160. inline T *
  161. ggc_vec_alloc (size_t c CXX_MEM_STAT_INFO)
  162. {
  163. if (need_finalization_p<T> ())
  164. return static_cast<T *> (ggc_internal_alloc (c * sizeof (T), finalize<T>,
  165. sizeof (T), c PASS_MEM_STAT));
  166. else
  167. return static_cast<T *> (ggc_internal_alloc (c * sizeof (T), NULL, 0, 0
  168. PASS_MEM_STAT));
  169. }
  170. template<typename T>
  171. inline T *
  172. ggc_cleared_vec_alloc (size_t c CXX_MEM_STAT_INFO)
  173. {
  174. if (need_finalization_p<T> ())
  175. return static_cast<T *> (ggc_internal_cleared_alloc (c * sizeof (T),
  176. finalize<T>,
  177. sizeof (T), c
  178. PASS_MEM_STAT));
  179. else
  180. return static_cast<T *> (ggc_internal_cleared_alloc (c * sizeof (T), NULL,
  181. 0, 0 PASS_MEM_STAT));
  182. }
  183. inline void *
  184. ggc_alloc_atomic (size_t s CXX_MEM_STAT_INFO)
  185. {
  186. return ggc_internal_alloc (s PASS_MEM_STAT);
  187. }
  188. /* Allocate a gc-able string, and fill it with LENGTH bytes from CONTENTS.
  189. If LENGTH is -1, then CONTENTS is assumed to be a
  190. null-terminated string and the memory sized accordingly. */
  191. extern const char *ggc_alloc_string (const char *contents, int length
  192. CXX_MEM_STAT_INFO);
  193. /* Make a copy of S, in GC-able memory. */
  194. #define ggc_strdup(S) ggc_alloc_string ((S), -1 MEM_STAT_INFO)
  195. /* Invoke the collector. Garbage collection occurs only when this
  196. function is called, not during allocations. */
  197. extern void ggc_collect (void);
  198. /* Return unused memory pages to the system. */
  199. extern void ggc_trim (void);
  200. /* Assume that all GGC memory is reachable and grow the limits for next collection. */
  201. extern void ggc_grow (void);
  202. /* Register an additional root table. This can be useful for some
  203. plugins. Does nothing if the passed pointer is NULL. */
  204. extern void ggc_register_root_tab (const struct ggc_root_tab *);
  205. /* Read objects previously saved with gt_pch_save from F. */
  206. extern void gt_pch_restore (FILE *f);
  207. /* Statistics. */
  208. /* Print allocation statistics. */
  209. extern void ggc_print_statistics (void);
  210. extern void stringpool_statistics (void);
  211. /* Heuristics. */
  212. extern void init_ggc_heuristics (void);
  213. #define ggc_alloc_rtvec_sized(NELT) \
  214. (rtvec_def *) ggc_internal_alloc (sizeof (struct rtvec_def) \
  215. + ((NELT) - 1) * sizeof (rtx)) \
  216. /* Memory statistics passing versions of some allocators. Too few of them to
  217. make gengtype produce them, so just define the needed ones here. */
  218. inline struct rtx_def *
  219. ggc_alloc_rtx_def_stat (size_t s CXX_MEM_STAT_INFO)
  220. {
  221. return (struct rtx_def *) ggc_internal_alloc (s PASS_MEM_STAT);
  222. }
  223. inline union tree_node *
  224. ggc_alloc_tree_node_stat (size_t s CXX_MEM_STAT_INFO)
  225. {
  226. return (union tree_node *) ggc_internal_alloc (s PASS_MEM_STAT);
  227. }
  228. inline union tree_node *
  229. ggc_alloc_cleared_tree_node_stat (size_t s CXX_MEM_STAT_INFO)
  230. {
  231. return (union tree_node *) ggc_internal_cleared_alloc (s PASS_MEM_STAT);
  232. }
  233. inline gimple *
  234. ggc_alloc_cleared_gimple_statement_stat (size_t s CXX_MEM_STAT_INFO)
  235. {
  236. return (gimple *) ggc_internal_cleared_alloc (s PASS_MEM_STAT);
  237. }
  238. inline void
  239. gt_ggc_mx (const char *s)
  240. {
  241. ggc_test_and_set_mark (const_cast<char *> (s));
  242. }
  243. inline void
  244. gt_pch_nx (const char *)
  245. {
  246. }
  247. inline void
  248. gt_ggc_mx (int)
  249. {
  250. }
  251. inline void
  252. gt_pch_nx (int)
  253. {
  254. }
  255. inline void
  256. gt_pch_nx (unsigned int)
  257. {
  258. }
  259. #endif