gc-segment-table.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/pairs.h"
  26. #include "libguile/gc.h"
  27. #include "libguile/private-gc.h"
  28. /*
  29. Heap segment table.
  30. The table is sorted by the address of the data itself. This makes
  31. for easy lookups. This is not portable: according to ANSI C,
  32. pointers can only be compared within the same object (i.e. the same
  33. block of malloced memory.). For machines with weird architectures,
  34. this should be revised.
  35. (Apparently, for this reason 1.6 and earlier had macros for pointer
  36. comparison. )
  37. perhaps it is worthwhile to remove the 2nd level of indirection in
  38. the table, but this certainly makes for cleaner code.
  39. */
  40. scm_t_heap_segment **scm_i_heap_segment_table;
  41. size_t scm_i_heap_segment_table_size;
  42. static scm_t_cell *lowest_cell;
  43. static scm_t_cell *highest_cell;
  44. /*
  45. RETURN: index of inserted segment.
  46. */
  47. int
  48. scm_i_insert_segment (scm_t_heap_segment *seg)
  49. {
  50. size_t size = (scm_i_heap_segment_table_size + 1) * sizeof (scm_t_heap_segment *);
  51. SCM_SYSCALL (scm_i_heap_segment_table
  52. = ((scm_t_heap_segment **)
  53. realloc ((char *)scm_i_heap_segment_table, size)));
  54. /*
  55. We can't alloc 4 more bytes. This is hopeless.
  56. */
  57. if (!scm_i_heap_segment_table)
  58. {
  59. fprintf (stderr, "scm_i_get_new_heap_segment: Could not grow heap segment table.\n");
  60. abort ();
  61. }
  62. if (!lowest_cell)
  63. {
  64. lowest_cell = seg->bounds[0];
  65. highest_cell = seg->bounds[1];
  66. }
  67. else
  68. {
  69. lowest_cell = SCM_MIN (lowest_cell, seg->bounds[0]);
  70. highest_cell = SCM_MAX (highest_cell, seg->bounds[1]);
  71. }
  72. {
  73. int i = 0;
  74. int j = 0;
  75. while (i < scm_i_heap_segment_table_size
  76. && scm_i_heap_segment_table[i]->bounds[0] <= seg->bounds[0])
  77. i++;
  78. /*
  79. We insert a new entry; if that happens to be before the
  80. "current" segment of a freelist, we must move the freelist index
  81. as well.
  82. */
  83. if (scm_i_master_freelist.heap_segment_idx >= i)
  84. scm_i_master_freelist.heap_segment_idx ++;
  85. if (scm_i_master_freelist2.heap_segment_idx >= i)
  86. scm_i_master_freelist2.heap_segment_idx ++;
  87. for (j = scm_i_heap_segment_table_size; j > i; --j)
  88. scm_i_heap_segment_table[j] = scm_i_heap_segment_table[j - 1];
  89. scm_i_heap_segment_table[i] = seg;
  90. scm_i_heap_segment_table_size ++;
  91. return i;
  92. }
  93. }
  94. /*
  95. Determine whether the given value does actually represent a cell in
  96. some heap segment. If this is the case, the number of the heap
  97. segment is returned. Otherwise, -1 is returned. Binary search is
  98. used to determine the heap segment that contains the cell.
  99. I think this function is too long to be inlined. --hwn
  100. */
  101. int
  102. scm_i_find_heap_segment_containing_object (SCM obj)
  103. {
  104. if (!CELL_P (obj))
  105. return -1;
  106. scm_i_find_heap_calls ++;
  107. if ((scm_t_cell *) obj < lowest_cell || (scm_t_cell *) obj >= highest_cell)
  108. return -1;
  109. {
  110. scm_t_cell *ptr = SCM2PTR (obj);
  111. unsigned int i = 0;
  112. unsigned int j = scm_i_heap_segment_table_size - 1;
  113. if (ptr < scm_i_heap_segment_table[i]->bounds[0])
  114. return -1;
  115. else if (scm_i_heap_segment_table[j]->bounds[1] <= ptr)
  116. return -1;
  117. else
  118. {
  119. while (i < j)
  120. {
  121. if (ptr < scm_i_heap_segment_table[i]->bounds[1])
  122. {
  123. break;
  124. }
  125. else if (scm_i_heap_segment_table[j]->bounds[0] <= ptr)
  126. {
  127. i = j;
  128. break;
  129. }
  130. else
  131. {
  132. unsigned long int k = (i + j) / 2;
  133. if (k == i)
  134. return -1;
  135. else if (ptr < scm_i_heap_segment_table[k]->bounds[1])
  136. {
  137. j = k;
  138. ++i;
  139. if (ptr < scm_i_heap_segment_table[i]->bounds[0])
  140. return -1;
  141. }
  142. else if (scm_i_heap_segment_table[k]->bounds[0] <= ptr)
  143. {
  144. i = k;
  145. --j;
  146. if (scm_i_heap_segment_table[j]->bounds[1] <= ptr)
  147. return -1;
  148. }
  149. }
  150. }
  151. if (!SCM_DOUBLECELL_ALIGNED_P (obj) && scm_i_heap_segment_table[i]->span == 2)
  152. return -1;
  153. else if (SCM_GC_IN_CARD_HEADERP (ptr))
  154. return -1;
  155. else
  156. return i;
  157. }
  158. }
  159. }
  160. int
  161. scm_i_marked_count (void)
  162. {
  163. int i = 0;
  164. int c = 0;
  165. for (; i < scm_i_heap_segment_table_size; i++)
  166. {
  167. c += scm_i_heap_segment_marked_count (scm_i_heap_segment_table[i]);
  168. }
  169. return c;
  170. }
  171. SCM
  172. scm_i_sweep_some_segments (scm_t_cell_type_statistics *freelist,
  173. scm_t_sweep_statistics *sweep_stats)
  174. {
  175. int i = freelist->heap_segment_idx;
  176. SCM collected = SCM_EOL;
  177. if (i == -1) /* huh? --hwn */
  178. i++;
  179. for (;
  180. i < scm_i_heap_segment_table_size; i++)
  181. {
  182. if (scm_i_heap_segment_table[i]->freelist != freelist)
  183. continue;
  184. collected = scm_i_sweep_some_cards (scm_i_heap_segment_table[i],
  185. sweep_stats,
  186. DEFAULT_SWEEP_AMOUNT);
  187. if (collected != SCM_EOL) /* Don't increment i */
  188. break;
  189. }
  190. freelist->heap_segment_idx = i;
  191. return collected;
  192. }
  193. void
  194. scm_i_reset_segments (void)
  195. {
  196. int i = 0;
  197. for (; i < scm_i_heap_segment_table_size; i++)
  198. {
  199. scm_t_heap_segment *seg = scm_i_heap_segment_table[i];
  200. seg->next_free_card = seg->bounds[0];
  201. }
  202. }
  203. /*
  204. Return a hashtab with counts of live objects, with tags as keys.
  205. */
  206. SCM
  207. scm_i_all_segments_statistics (SCM tab)
  208. {
  209. int i = 0;
  210. for (; i < scm_i_heap_segment_table_size; i++)
  211. {
  212. scm_t_heap_segment *seg = scm_i_heap_segment_table[i];
  213. scm_i_heap_segment_statistics (seg, tab);
  214. }
  215. return tab;
  216. }
  217. unsigned long*
  218. scm_i_segment_table_info (int* size)
  219. {
  220. *size = scm_i_heap_segment_table_size;
  221. unsigned long *bounds = malloc (sizeof (unsigned long) * *size * 2);
  222. int i;
  223. if (!bounds)
  224. abort ();
  225. for (i = *size; i-- > 0; )
  226. {
  227. bounds[2*i] = (unsigned long)scm_i_heap_segment_table[i]->bounds[0];
  228. bounds[2*i+1] = (unsigned long)scm_i_heap_segment_table[i]->bounds[1];
  229. }
  230. return bounds;
  231. }
  232. void
  233. scm_i_sweep_all_segments (char const *reason,
  234. scm_t_sweep_statistics *sweep_stats)
  235. {
  236. unsigned i= 0;
  237. for (i = 0; i < scm_i_heap_segment_table_size; i++)
  238. {
  239. scm_i_sweep_segment (scm_i_heap_segment_table[i], sweep_stats);
  240. }
  241. }
  242. void
  243. scm_i_clear_mark_space (void)
  244. {
  245. int i = 0;
  246. for (; i < scm_i_heap_segment_table_size; i++)
  247. {
  248. scm_i_clear_segment_mark_space (scm_i_heap_segment_table[i]);
  249. }
  250. }