ggc.h 9.5 KB

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