private-gc.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * private-gc.h - private declarations for garbage collection.
  3. *
  4. * Copyright (C) 2002, 03, 04, 05, 06, 07, 08, 09 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 3 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #ifndef PRIVATE_GC
  22. #define PRIVATE_GC
  23. #include "_scm.h"
  24. /* {heap tuning parameters}
  25. *
  26. * These are parameters for controlling memory allocation. The heap
  27. * is the area out of which scm_cons, and object headers are allocated.
  28. *
  29. * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
  30. * 64 bit machine. The units of the _SIZE parameters are bytes.
  31. * Cons pairs and object headers occupy one heap cell.
  32. *
  33. * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
  34. * is needed.
  35. */
  36. /*
  37. * Heap size 45000 and 40% min yield gives quick startup and no extra
  38. * heap allocation. Having higher values on min yield may lead to
  39. * large heaps, especially if code behaviour is varying its
  40. * maximum consumption between different freelists.
  41. */
  42. /*
  43. These values used to be global C variables. However, they're also
  44. available through the environment, and having a double interface is
  45. confusing. Now they're #defines --hwn.
  46. */
  47. #define SCM_DEFAULT_INIT_HEAP_SIZE_1 256*1024
  48. #define SCM_DEFAULT_MIN_YIELD_1 40
  49. #define SCM_DEFAULT_INIT_HEAP_SIZE_2 32*1024
  50. /*
  51. How many cells to collect during one sweep call. This is the pool
  52. size of each thread.
  53. */
  54. #define DEFAULT_SWEEP_AMOUNT 512
  55. /* The following value may seem large, but note that if we get to GC at
  56. * all, this means that we have a numerically intensive application
  57. */
  58. #define SCM_DEFAULT_MIN_YIELD_2 40
  59. #define SCM_DEFAULT_MAX_SEGMENT_SIZE (20*1024*1024L)
  60. #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_SIZEOF_CARD)
  61. #define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_t_cell))
  62. #define SCM_DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_t_cell) - 1) & SCM_UNPACK (x)) == 0)
  63. #define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
  64. ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
  65. #define SCM_GC_IN_CARD_HEADERP(x) \
  66. (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
  67. int scm_getenv_int (const char *var, int def);
  68. typedef enum { return_on_error, abort_on_error } policy_on_error;
  69. /* gc-freelist */
  70. /*
  71. FREELIST:
  72. A struct holding GC statistics on a particular type of cells.
  73. Counts in cells are mainly for heap statistics, and for
  74. double-cells, they are still measured in single-cell units.
  75. */
  76. typedef struct scm_t_cell_type_statistics {
  77. /*
  78. heap segment where the last cell was allocated
  79. */
  80. int heap_segment_idx;
  81. /* defines min_yield as fraction of total heap size
  82. */
  83. float min_yield_fraction;
  84. /* number of cells per object on this list */
  85. int span;
  86. /* number of collected cells during last GC. */
  87. unsigned long collected;
  88. unsigned long swept;
  89. /*
  90. Total number of cells in heap segments belonging to this list.
  91. */
  92. unsigned long heap_total_cells;
  93. } scm_t_cell_type_statistics;
  94. /* Sweep statistics. */
  95. typedef struct scm_sweep_statistics
  96. {
  97. /* Number of cells "swept", i.e., visited during the sweep operation. */
  98. unsigned swept;
  99. /* Number of cells collected during the sweep operation. This number must
  100. always be lower than or equal to SWEPT. */
  101. unsigned collected;
  102. } scm_t_sweep_statistics;
  103. SCM_INTERNAL scm_t_sweep_statistics scm_i_gc_sweep_stats;
  104. extern scm_t_cell_type_statistics scm_i_master_freelist;
  105. extern scm_t_cell_type_statistics scm_i_master_freelist2;
  106. SCM_INTERNAL
  107. void scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist,
  108. scm_t_sweep_statistics sweep_stats,
  109. scm_t_sweep_statistics sweep_stats_1);
  110. SCM_INTERNAL
  111. void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist);
  112. SCM_INTERNAL float
  113. scm_i_gc_heap_size_delta (scm_t_cell_type_statistics * freelist);
  114. #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
  115. #define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
  116. /* CELL_P checks a random word whether it has the right form for a
  117. pointer to a cell. Use scm_i_find_heap_segment_containing_object
  118. to find out whether it actually points to a real cell.
  119. The right form for a cell pointer is this: the low three bits must
  120. be scm_tc3_cons, and when the scm_tc3_cons tag is stripped, the
  121. resulting pointer must be correctly aligned.
  122. scm_i_initialize_heap_segment_data guarantees that the test below
  123. works.
  124. */
  125. #define CELL_P(x) ((SCM_UNPACK(x) & (sizeof(scm_t_cell)-1)) == scm_tc3_cons)
  126. /*
  127. gc-mark
  128. */
  129. /* Non-zero while in the mark phase. */
  130. SCM_INTERNAL int scm_i_marking;
  131. SCM_INTERNAL void scm_mark_all (void);
  132. /*
  133. gc-segment:
  134. */
  135. /*
  136. Cells are stored in a heap-segment: it is a contiguous chunk of
  137. memory, that associated with one freelist.
  138. */
  139. typedef struct scm_t_heap_segment
  140. {
  141. /*
  142. {lower, upper} bounds of the segment
  143. The upper bound is also the start of the mark space.
  144. */
  145. scm_t_cell *bounds[2];
  146. /*
  147. If we ever decide to give it back, we could do it with this ptr.
  148. Note that giving back memory is not very useful; as long we don't
  149. touch a chunk of memory, the virtual memory system will keep it
  150. swapped out. We could simply forget about a block.
  151. (not that we do that, but anyway.)
  152. */
  153. void *malloced;
  154. scm_t_cell *next_free_card;
  155. /* address of the head-of-freelist pointer for this segment's cells.
  156. All segments usually point to the same one, scm_i_freelist. */
  157. scm_t_cell_type_statistics *freelist;
  158. /* number of cells per object in this segment */
  159. int span;
  160. /*
  161. Is this the first time that the cells are accessed?
  162. */
  163. int first_time;
  164. } scm_t_heap_segment;
  165. /*
  166. A table of segment records is kept that records the upper and
  167. lower extents of the segment; this is used during the conservative
  168. phase of gc to identify probably gc roots (because they point
  169. into valid segments at reasonable offsets).
  170. */
  171. extern scm_t_heap_segment ** scm_i_heap_segment_table;
  172. extern size_t scm_i_heap_segment_table_size;
  173. SCM_INTERNAL int scm_i_init_card_freelist (scm_t_cell * card, SCM *free_list,
  174. scm_t_heap_segment*);
  175. SCM_INTERNAL int scm_i_sweep_card (scm_t_cell *card, SCM *free_list,
  176. scm_t_heap_segment *);
  177. SCM_INTERNAL int scm_i_card_marked_count (scm_t_cell *card, int span);
  178. SCM_INTERNAL void scm_i_card_statistics (scm_t_cell *p, SCM hashtab,
  179. scm_t_heap_segment *seg);
  180. SCM_INTERNAL char const *scm_i_tag_name (scm_t_bits tag); /* MOVEME */
  181. SCM_INTERNAL int scm_i_initialize_heap_segment_data (scm_t_heap_segment *seg,
  182. size_t requested);
  183. SCM_INTERNAL int scm_i_segment_cells_per_card (scm_t_heap_segment *seg);
  184. SCM_INTERNAL int scm_i_segment_card_number (scm_t_heap_segment *seg,
  185. scm_t_cell *card);
  186. SCM_INTERNAL int scm_i_segment_card_count (scm_t_heap_segment *seg);
  187. SCM_INTERNAL int scm_i_segment_cell_count (scm_t_heap_segment *seg);
  188. SCM_INTERNAL int scm_i_heap_segment_marked_count (scm_t_heap_segment *seg);
  189. SCM_INTERNAL void scm_i_clear_segment_mark_space (scm_t_heap_segment *seg);
  190. SCM_INTERNAL scm_t_heap_segment *
  191. scm_i_make_empty_heap_segment (scm_t_cell_type_statistics*);
  192. SCM_INTERNAL SCM scm_i_sweep_for_freelist (scm_t_cell_type_statistics *seg);
  193. SCM_INTERNAL SCM scm_i_sweep_some_cards (scm_t_heap_segment *seg,
  194. scm_t_sweep_statistics *sweep_stats,
  195. int threshold);
  196. SCM_INTERNAL void scm_i_sweep_segment (scm_t_heap_segment *seg,
  197. scm_t_sweep_statistics *sweep_stats);
  198. SCM_INTERNAL void scm_i_heap_segment_statistics (scm_t_heap_segment *seg,
  199. SCM tab);
  200. SCM_INTERNAL int scm_i_insert_segment (scm_t_heap_segment *seg);
  201. SCM_INTERNAL int scm_i_find_heap_segment_containing_object (SCM obj);
  202. SCM_INTERNAL int scm_i_get_new_heap_segment (scm_t_cell_type_statistics *freelist,
  203. size_t length,
  204. policy_on_error);
  205. SCM_INTERNAL int scm_i_marked_count (void);
  206. SCM_INTERNAL void scm_i_clear_mark_space (void);
  207. SCM_INTERNAL void scm_i_sweep_segments (void);
  208. SCM_INTERNAL SCM scm_i_sweep_some_segments (scm_t_cell_type_statistics *fl,
  209. scm_t_sweep_statistics *sweep_stats);
  210. SCM_INTERNAL void scm_i_reset_segments (void);
  211. SCM_INTERNAL void scm_i_sweep_all_segments (char const *reason,
  212. scm_t_sweep_statistics *sweep_stats);
  213. SCM_INTERNAL SCM scm_i_all_segments_statistics (SCM hashtab);
  214. SCM_INTERNAL unsigned long *scm_i_segment_table_info(int *size);
  215. SCM_INTERNAL long int scm_i_deprecated_memory_return;
  216. SCM_INTERNAL long int scm_i_find_heap_calls;
  217. SCM_INTERNAL long int scm_i_last_marked_cell_count;
  218. /*
  219. global init funcs.
  220. */
  221. void scm_gc_init_malloc (void);
  222. void scm_gc_init_freelist (void);
  223. void scm_gc_init_segments (void);
  224. void scm_gc_init_mark (void);
  225. #endif