Upgrade Boehm GC to 7.2alpha4.
[cacao.git] / src / mm / boehm-gc / blacklst.c
1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
4  *
5  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
7  *
8  * Permission is hereby granted to use or copy this program
9  * for any purpose,  provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  */
14
15 #include "private/gc_priv.h"
16
17 /*
18  * We maintain several hash tables of hblks that have had false hits.
19  * Each contains one bit per hash bucket;  If any page in the bucket
20  * has had a false hit, we assume that all of them have.
21  * See the definition of page_hash_table in gc_private.h.
22  * False hits from the stack(s) are much more dangerous than false hits
23  * from elsewhere, since the former can pin a large object that spans the
24  * block, eventhough it does not start on the dangerous block.
25  */
26
27 /*
28  * Externally callable routines are:
29
30  * GC_add_to_black_list_normal
31  * GC_add_to_black_list_stack
32  * GC_promote_black_lists
33  * GC_is_black_listed
34  *
35  * All require that the allocator lock is held.
36  */
37
38 /* Pointers to individual tables.  We replace one table by another by   */
39 /* switching these pointers.                                            */
40 STATIC word * GC_old_normal_bl = NULL;
41                 /* Nonstack false references seen at last full          */
42                 /* collection.                                          */
43 STATIC word * GC_incomplete_normal_bl = NULL;
44                 /* Nonstack false references seen since last            */
45                 /* full collection.                                     */
46 STATIC word * GC_old_stack_bl = NULL;
47 STATIC word * GC_incomplete_stack_bl = NULL;
48
49 STATIC word GC_total_stack_black_listed = 0;
50                         /* Number of bytes on stack blacklist.  */
51
52 GC_INNER word GC_black_list_spacing = MINHINCR * HBLKSIZE;
53                         /* Initial rough guess. */
54
55 STATIC void GC_clear_bl(word *);
56
57 GC_INNER void GC_default_print_heap_obj_proc(ptr_t p)
58 {
59     ptr_t base = GC_base(p);
60     GC_err_printf("start: %p, appr. length: %ld", base,
61                   (unsigned long)GC_size(base));
62 }
63
64 GC_INNER void (*GC_print_heap_obj)(ptr_t p) = GC_default_print_heap_obj_proc;
65
66 #ifdef PRINT_BLACK_LIST
67 STATIC void GC_print_source_ptr(ptr_t p)
68 {
69     ptr_t base = GC_base(p);
70     if (0 == base) {
71         if (0 == p) {
72             GC_err_printf("in register");
73         } else {
74             GC_err_printf("in root set");
75         }
76     } else {
77         GC_err_printf("in object at ");
78         /* FIXME: We can't call the debug version of GC_print_heap_obj  */
79         /* (with PRINT_CALL_CHAIN) here because the lock is held and    */
80         /* the world is stopped.                                        */
81         GC_default_print_heap_obj_proc(base);
82     }
83 }
84 #endif
85
86 GC_INNER void GC_bl_init(void)
87 {
88     if (!GC_all_interior_pointers) {
89       GC_old_normal_bl = (word *)
90                          GC_scratch_alloc((word)(sizeof (page_hash_table)));
91       GC_incomplete_normal_bl = (word *)GC_scratch_alloc
92                                         ((word)(sizeof(page_hash_table)));
93       if (GC_old_normal_bl == 0 || GC_incomplete_normal_bl == 0) {
94         GC_err_printf("Insufficient memory for black list\n");
95         EXIT();
96       }
97       GC_clear_bl(GC_old_normal_bl);
98       GC_clear_bl(GC_incomplete_normal_bl);
99     }
100     GC_old_stack_bl = (word *)GC_scratch_alloc((word)(sizeof(page_hash_table)));
101     GC_incomplete_stack_bl = (word *)GC_scratch_alloc
102                                         ((word)(sizeof(page_hash_table)));
103     if (GC_old_stack_bl == 0 || GC_incomplete_stack_bl == 0) {
104         GC_err_printf("Insufficient memory for black list\n");
105         EXIT();
106     }
107     GC_clear_bl(GC_old_stack_bl);
108     GC_clear_bl(GC_incomplete_stack_bl);
109 }
110
111 STATIC void GC_clear_bl(word *doomed)
112 {
113     BZERO(doomed, sizeof(page_hash_table));
114 }
115
116 STATIC void GC_copy_bl(word *old, word *new)
117 {
118     BCOPY(old, new, sizeof(page_hash_table));
119 }
120
121 static word total_stack_black_listed(void);
122
123 /* Signal the completion of a collection.  Turn the incomplete black    */
124 /* lists into new black lists, etc.                                     */
125 GC_INNER void GC_promote_black_lists(void)
126 {
127     word * very_old_normal_bl = GC_old_normal_bl;
128     word * very_old_stack_bl = GC_old_stack_bl;
129
130     GC_old_normal_bl = GC_incomplete_normal_bl;
131     GC_old_stack_bl = GC_incomplete_stack_bl;
132     if (!GC_all_interior_pointers) {
133       GC_clear_bl(very_old_normal_bl);
134     }
135     GC_clear_bl(very_old_stack_bl);
136     GC_incomplete_normal_bl = very_old_normal_bl;
137     GC_incomplete_stack_bl = very_old_stack_bl;
138     GC_total_stack_black_listed = total_stack_black_listed();
139     if (GC_print_stats == VERBOSE)
140         GC_log_printf("%ld bytes in heap blacklisted for interior pointers\n",
141                       (unsigned long)GC_total_stack_black_listed);
142     if (GC_total_stack_black_listed != 0) {
143         GC_black_list_spacing =
144                 HBLKSIZE*(GC_heapsize/GC_total_stack_black_listed);
145     }
146     if (GC_black_list_spacing < 3 * HBLKSIZE) {
147         GC_black_list_spacing = 3 * HBLKSIZE;
148     }
149     if (GC_black_list_spacing > MAXHINCR * HBLKSIZE) {
150         GC_black_list_spacing = MAXHINCR * HBLKSIZE;
151         /* Makes it easier to allocate really huge blocks, which otherwise */
152         /* may have problems with nonuniform blacklist distributions.      */
153         /* This way we should always succeed immediately after growing the */
154         /* heap.                                                           */
155     }
156 }
157
158 GC_INNER void GC_unpromote_black_lists(void)
159 {
160     if (!GC_all_interior_pointers) {
161       GC_copy_bl(GC_old_normal_bl, GC_incomplete_normal_bl);
162     }
163     GC_copy_bl(GC_old_stack_bl, GC_incomplete_stack_bl);
164 }
165
166 /* P is not a valid pointer reference, but it falls inside      */
167 /* the plausible heap bounds.                                   */
168 /* Add it to the normal incomplete black list if appropriate.   */
169 #ifdef PRINT_BLACK_LIST
170   GC_INNER void GC_add_to_black_list_normal(word p, ptr_t source)
171 #else
172   GC_INNER void GC_add_to_black_list_normal(word p)
173 #endif
174 {
175     if (!(GC_modws_valid_offsets[p & (sizeof(word)-1)])) return;
176     {
177         word index = PHT_HASH((word)p);
178
179         if (HDR(p) == 0 || get_pht_entry_from_index(GC_old_normal_bl, index)) {
180 #           ifdef PRINT_BLACK_LIST
181                 if (!get_pht_entry_from_index(GC_incomplete_normal_bl, index)) {
182                   GC_err_printf(
183                         "Black listing (normal) %p referenced from %p ",
184                         (ptr_t) p, source);
185                   GC_print_source_ptr(source);
186                   GC_err_puts("\n");
187                 }
188 #           endif
189             set_pht_entry_from_index(GC_incomplete_normal_bl, index);
190         } /* else this is probably just an interior pointer to an allocated */
191           /* object, and isn't worth black listing.                         */
192     }
193 }
194
195 /* And the same for false pointers from the stack. */
196 #ifdef PRINT_BLACK_LIST
197   GC_INNER void GC_add_to_black_list_stack(word p, ptr_t source)
198 #else
199   GC_INNER void GC_add_to_black_list_stack(word p)
200 #endif
201 {
202     word index = PHT_HASH((word)p);
203
204     if (HDR(p) == 0 || get_pht_entry_from_index(GC_old_stack_bl, index)) {
205 #       ifdef PRINT_BLACK_LIST
206             if (!get_pht_entry_from_index(GC_incomplete_stack_bl, index)) {
207                   GC_err_printf(
208                         "Black listing (stack) %p referenced from %p ",
209                         (ptr_t)p, source);
210                   GC_print_source_ptr(source);
211                   GC_err_puts("\n");
212             }
213 #       endif
214         set_pht_entry_from_index(GC_incomplete_stack_bl, index);
215     }
216 }
217
218 /*
219  * Is the block starting at h of size len bytes black listed?   If so,
220  * return the address of the next plausible r such that (r, len) might not
221  * be black listed.  (R may not actually be in the heap.  We guarantee only
222  * that every smaller value of r after h is also black listed.)
223  * If (h,len) is not black listed, return 0.
224  * Knows about the structure of the black list hash tables.
225  */
226 GC_INNER struct hblk * GC_is_black_listed(struct hblk *h, word len)
227 {
228     word index = PHT_HASH((word)h);
229     word i;
230     word nblocks = divHBLKSZ(len);
231
232     if (!GC_all_interior_pointers) {
233       if (get_pht_entry_from_index(GC_old_normal_bl, index)
234           || get_pht_entry_from_index(GC_incomplete_normal_bl, index)) {
235         return(h+1);
236       }
237     }
238
239     for (i = 0;;) {
240         if (GC_old_stack_bl[divWORDSZ(index)] == 0
241             && GC_incomplete_stack_bl[divWORDSZ(index)] == 0) {
242             /* An easy case */
243             i += WORDSZ - modWORDSZ(index);
244         } else {
245           if (get_pht_entry_from_index(GC_old_stack_bl, index)
246               || get_pht_entry_from_index(GC_incomplete_stack_bl, index)) {
247             return(h+i+1);
248           }
249           i++;
250         }
251         if (i >= nblocks) break;
252         index = PHT_HASH((word)(h+i));
253     }
254     return(0);
255 }
256
257 /* Return the number of blacklisted blocks in a given range.    */
258 /* Used only for statistical purposes.                          */
259 /* Looks only at the GC_incomplete_stack_bl.                    */
260 STATIC word GC_number_stack_black_listed(struct hblk *start,
261                                          struct hblk *endp1)
262 {
263     register struct hblk * h;
264     word result = 0;
265
266     for (h = start; h < endp1; h++) {
267         word index = PHT_HASH((word)h);
268
269         if (get_pht_entry_from_index(GC_old_stack_bl, index)) result++;
270     }
271     return(result);
272 }
273
274 /* Return the total number of (stack) black-listed bytes. */
275 static word total_stack_black_listed(void)
276 {
277     register unsigned i;
278     word total = 0;
279
280     for (i = 0; i < GC_n_heap_sects; i++) {
281         struct hblk * start = (struct hblk *) GC_heap_sects[i].hs_start;
282         size_t len = (word) GC_heap_sects[i].hs_bytes;
283         struct hblk * endp1 = start + len/HBLKSIZE;
284
285         total += GC_number_stack_black_listed(start, endp1);
286     }
287     return(total * HBLKSIZE);
288 }