private-gc.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. (c) FSF 2002.
  3. */
  4. #ifndef PRIVATE_GC
  5. #define PRIVATE_GC
  6. #include "_scm.h"
  7. /* {heap tuning parameters}
  8. *
  9. * These are parameters for controlling memory allocation. The heap
  10. * is the area out of which scm_cons, and object headers are allocated.
  11. *
  12. * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
  13. * 64 bit machine. The units of the _SIZE parameters are bytes.
  14. * Cons pairs and object headers occupy one heap cell.
  15. *
  16. * SCM_INIT_HEAP_SIZE is the initial size of heap. If this much heap is
  17. * allocated initially the heap will grow by half its current size
  18. * each subsequent time more heap is needed.
  19. *
  20. * If SCM_INIT_HEAP_SIZE heap cannot be allocated initially, SCM_HEAP_SEG_SIZE
  21. * will be used, and the heap will grow by SCM_HEAP_SEG_SIZE when more
  22. * heap is needed. SCM_HEAP_SEG_SIZE must fit into type size_t. This code
  23. * is in scm_init_storage() and alloc_some_heap() in sys.c
  24. *
  25. * If SCM_INIT_HEAP_SIZE can be allocated initially, the heap will grow by
  26. * SCM_EXPHEAP(scm_heap_size) when more heap is needed.
  27. *
  28. * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
  29. * is needed.
  30. */
  31. /*
  32. * Heap size 45000 and 40% min yield gives quick startup and no extra
  33. * heap allocation. Having higher values on min yield may lead to
  34. * large heaps, especially if code behaviour is varying its
  35. * maximum consumption between different freelists.
  36. */
  37. /*
  38. These values used to be global C variables. However, they're also
  39. available through the environment, and having a double interface is
  40. confusing. Now they're #defines --hwn.
  41. */
  42. #define SCM_DEFAULT_INIT_HEAP_SIZE_1 256*1024
  43. #define SCM_DEFAULT_MIN_YIELD_1 40
  44. #define SCM_DEFAULT_INIT_HEAP_SIZE_2 32*1024
  45. /* The following value may seem large, but note that if we get to GC at
  46. * all, this means that we have a numerically intensive application
  47. */
  48. #define SCM_DEFAULT_MIN_YIELD_2 40
  49. #define SCM_DEFAULT_MAX_SEGMENT_SIZE (20*1024*1024L)
  50. #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_SIZEOF_CARD)
  51. #define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_t_cell))
  52. #define SCM_DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_t_cell) - 1) & SCM_UNPACK (x)) == 0)
  53. #define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
  54. ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
  55. #define SCM_GC_IN_CARD_HEADERP(x) \
  56. (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
  57. int scm_getenv_int (const char *var, int def);
  58. typedef enum { return_on_error, abort_on_error } policy_on_error;
  59. /* gc-freelist*/
  60. /*
  61. FREELIST:
  62. A struct holding GC statistics on a particular type of cells.
  63. */
  64. typedef struct scm_t_cell_type_statistics {
  65. /*
  66. heap segment where the last cell was allocated
  67. */
  68. int heap_segment_idx;
  69. /* minimum yield on this list in order not to grow the heap
  70. */
  71. long min_yield;
  72. /* defines min_yield as percent of total heap size
  73. */
  74. int min_yield_fraction;
  75. /* number of cells per object on this list */
  76. int span;
  77. /* number of collected cells during last GC */
  78. unsigned long collected;
  79. /* number of collected cells during penultimate GC */
  80. unsigned long collected_1;
  81. /* total number of cells in heap segments
  82. * belonging to this list.
  83. */
  84. unsigned long heap_size;
  85. } scm_t_cell_type_statistics;
  86. extern scm_t_cell_type_statistics scm_i_master_freelist;
  87. extern scm_t_cell_type_statistics scm_i_master_freelist2;
  88. extern unsigned long scm_gc_cells_collected_1;
  89. void scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist);
  90. void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist);
  91. int scm_i_gc_grow_heap_p (scm_t_cell_type_statistics * freelist);
  92. #define SCM_HEAP_SIZE \
  93. (scm_i_master_freelist.heap_size + scm_i_master_freelist2.heap_size)
  94. #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
  95. #define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
  96. /* CELL_P checks a random word whether it has the right form for a
  97. pointer to a cell. Use scm_i_find_heap_segment_containing_object
  98. to find out whether it actually points to a real cell.
  99. The right form for a cell pointer is this: the low three bits must
  100. be scm_tc3_cons, and when the scm_tc3_cons tag is stripped, the
  101. resulting pointer must be correctly aligned.
  102. scm_i_initialize_heap_segment_data guarantees that the test below
  103. works.
  104. */
  105. #define CELL_P(x) ((SCM_UNPACK(x) & (sizeof(scm_t_cell)-1)) == scm_tc3_cons)
  106. /*
  107. gc-mark
  108. */
  109. void scm_mark_all (void);
  110. /*
  111. gc-segment:
  112. */
  113. /*
  114. Cells are stored in a heap-segment: it is a contiguous chunk of
  115. memory, that associated with one freelist.
  116. */
  117. typedef struct scm_t_heap_segment
  118. {
  119. /*
  120. {lower, upper} bounds of the segment
  121. The upper bound is also the start of the mark space.
  122. */
  123. scm_t_cell *bounds[2];
  124. /*
  125. If we ever decide to give it back, we could do it with this ptr.
  126. Note that giving back memory is not very useful; as long we don't
  127. touch a chunk of memory, the virtual memory system will keep it
  128. swapped out. We could simply forget about a block.
  129. (not that we do that, but anyway.)
  130. */
  131. void* malloced;
  132. scm_t_cell * next_free_card;
  133. /* address of the head-of-freelist pointer for this segment's cells.
  134. All segments usually point to the same one, scm_i_freelist. */
  135. scm_t_cell_type_statistics *freelist;
  136. /* number of cells per object in this segment */
  137. int span;
  138. /*
  139. Is this the first time that the cells are accessed?
  140. */
  141. int first_time;
  142. } scm_t_heap_segment;
  143. /*
  144. A table of segment records is kept that records the upper and
  145. lower extents of the segment; this is used during the conservative
  146. phase of gc to identify probably gc roots (because they point
  147. into valid segments at reasonable offsets).
  148. */
  149. extern scm_t_heap_segment ** scm_i_heap_segment_table;
  150. extern size_t scm_i_heap_segment_table_size;
  151. int scm_i_init_card_freelist (scm_t_cell * card, SCM *free_list,scm_t_heap_segment*);
  152. int scm_i_sweep_card (scm_t_cell * card, SCM *free_list, scm_t_heap_segment*);
  153. void scm_i_card_statistics (scm_t_cell *p, SCM hashtab, scm_t_heap_segment *seg);
  154. char const *scm_i_tag_name (scm_t_bits tag); /* MOVEME */
  155. int scm_i_initialize_heap_segment_data (scm_t_heap_segment * segment, size_t requested);
  156. int scm_i_segment_card_count (scm_t_heap_segment * seg);
  157. int scm_i_segment_cell_count (scm_t_heap_segment * seg);
  158. void scm_i_clear_segment_mark_space (scm_t_heap_segment *seg);
  159. scm_t_heap_segment * scm_i_make_empty_heap_segment (scm_t_cell_type_statistics*);
  160. SCM scm_i_sweep_some_cards (scm_t_heap_segment *seg);
  161. void scm_i_sweep_segment (scm_t_heap_segment * seg);
  162. void scm_i_heap_segment_statistics (scm_t_heap_segment *seg, SCM tab);
  163. int scm_i_insert_segment (scm_t_heap_segment * seg);
  164. long int scm_i_find_heap_segment_containing_object (SCM obj);
  165. int scm_i_get_new_heap_segment (scm_t_cell_type_statistics *, policy_on_error);
  166. void scm_i_clear_mark_space (void);
  167. void scm_i_sweep_segments (void);
  168. SCM scm_i_sweep_some_segments (scm_t_cell_type_statistics * fl);
  169. void scm_i_reset_segments (void);
  170. void scm_i_sweep_all_segments (char const *reason);
  171. SCM scm_i_all_segments_statistics (SCM hashtab);
  172. void scm_i_make_initial_segment (int init_heap_size, scm_t_cell_type_statistics *freelist);
  173. extern long int scm_i_deprecated_memory_return;
  174. /*
  175. global init funcs.
  176. */
  177. void scm_gc_init_malloc (void);
  178. void scm_gc_init_freelist (void);
  179. void scm_gc_init_segments (void);
  180. void scm_gc_init_mark (void);
  181. #endif