boehm-gc: revert all CACAO-specific modifications; this is now an exact copy of the...
[cacao.git] / src / mm / boehm-gc / headers.c
1 /* 
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
5  *
6  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
8  *
9  * Permission is hereby granted to use or copy this program
10  * for any purpose,  provided the above notices are retained on all copies.
11  * Permission to modify the code and to distribute modified code is granted,
12  * provided the above notices are retained, and a notice that the code was
13  * modified is included with the above copyright notice.
14  */
15  
16 /*
17  * This implements:
18  * 1. allocation of heap block headers
19  * 2. A map from addresses to heap block addresses to heap block headers
20  *
21  * Access speed is crucial.  We implement an index structure based on a 2
22  * level tree.
23  */
24  
25 # include "private/gc_priv.h"
26
27 STATIC bottom_index * GC_all_bottom_indices = 0;
28                                 /* Pointer to first (lowest addr) */
29                                 /* bottom_index.                  */
30
31 STATIC bottom_index * GC_all_bottom_indices_end = 0;
32                                 /* Pointer to last (highest addr) */
33                                 /* bottom_index.                  */
34  
35 /* Non-macro version of header location routine */
36 hdr * GC_find_header(ptr_t h)
37 {
38 #   ifdef HASH_TL
39         hdr * result;
40         GET_HDR(h, result);
41         return(result);
42 #   else
43         return(HDR_INNER(h));
44 #   endif
45 }
46
47 /* Handle a header cache miss.  Returns a pointer to the        */
48 /* header corresponding to p, if p can possibly be a valid      */
49 /* object pointer, and 0 otherwise.                             */
50 /* GUARANTEED to return 0 for a pointer past the first page     */
51 /* of an object unless both GC_all_interior_pointers is set     */
52 /* and p is in fact a valid object pointer.                     */
53 /* Never returns a pointer to a free hblk.                      */
54 #ifdef PRINT_BLACK_LIST
55   hdr * GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce, ptr_t source)
56 #else
57   hdr * GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce)
58 #endif
59 {
60   hdr *hhdr;
61   HC_MISS();
62   GET_HDR(p, hhdr);
63   if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
64     if (GC_all_interior_pointers) {
65       if (hhdr != 0) {
66         ptr_t current = p;
67             
68         current = (ptr_t)HBLKPTR(current);
69         do {
70             current = current - HBLKSIZE*(word)hhdr;
71             hhdr = HDR(current);
72         } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
73         /* current points to near the start of the large object */
74         if (hhdr -> hb_flags & IGNORE_OFF_PAGE)
75             return 0;
76         if (HBLK_IS_FREE(hhdr)
77             || p - current >= (ptrdiff_t)(hhdr->hb_sz)) {
78             GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
79             /* Pointer past the end of the block */
80             return 0;
81         }
82       } else {
83         GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
84         /* And return zero: */
85       }
86       GC_ASSERT(hhdr == 0 || !HBLK_IS_FREE(hhdr));
87       return hhdr;
88       /* Pointers past the first page are probably too rare     */
89       /* to add them to the cache.  We don't.                   */
90       /* And correctness relies on the fact that we don't.      */
91     } else {
92       if (hhdr == 0) {
93         GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
94       }
95       return 0;
96     }
97   } else {
98     if (HBLK_IS_FREE(hhdr)) {
99       GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
100       return 0;
101     } else {
102       hce -> block_addr = (word)(p) >> LOG_HBLKSIZE;
103       hce -> hce_hdr = hhdr; 
104       return hhdr;
105     }
106   } 
107 }
108  
109 /* Routines to dynamically allocate collector data structures that will */
110 /* never be freed.                                                       */
111  
112 static ptr_t scratch_free_ptr = 0;
113  
114 /* GC_scratch_last_end_ptr is end point of last obtained scratch area.  */
115 /* GC_scratch_end_ptr is end point of current scratch area.             */
116  
117 ptr_t GC_scratch_alloc(size_t bytes)
118 {
119     register ptr_t result = scratch_free_ptr;
120
121     bytes += GRANULE_BYTES-1;
122     bytes &= ~(GRANULE_BYTES-1);
123     scratch_free_ptr += bytes;
124     if (scratch_free_ptr <= GC_scratch_end_ptr) {
125         return(result);
126     }
127     {
128         word bytes_to_get = MINHINCR * HBLKSIZE;
129          
130         if (bytes_to_get <= bytes) {
131           /* Undo the damage, and get memory directly */
132             bytes_to_get = bytes;
133 #           ifdef USE_MMAP
134                 bytes_to_get += GC_page_size - 1;
135                 bytes_to_get &= ~(GC_page_size - 1);
136 #           endif
137             result = (ptr_t)GET_MEM(bytes_to_get);
138             GC_add_to_our_memory(result, bytes_to_get);
139             scratch_free_ptr -= bytes;
140             GC_scratch_last_end_ptr = result + bytes;
141             return(result);
142         }
143         result = (ptr_t)GET_MEM(bytes_to_get);
144         GC_add_to_our_memory(result, bytes_to_get);
145         if (result == 0) {
146             if (GC_print_stats)
147                 GC_printf("Out of memory - trying to allocate less\n");
148             scratch_free_ptr -= bytes;
149             bytes_to_get = bytes;
150 #           ifdef USE_MMAP
151                 bytes_to_get += GC_page_size - 1;
152                 bytes_to_get &= ~(GC_page_size - 1);
153 #           endif
154             result = (ptr_t)GET_MEM(bytes_to_get);
155             GC_add_to_our_memory(result, bytes_to_get);
156             return result;
157         }
158         scratch_free_ptr = result;
159         GC_scratch_end_ptr = scratch_free_ptr + bytes_to_get;
160         GC_scratch_last_end_ptr = GC_scratch_end_ptr;
161         return(GC_scratch_alloc(bytes));
162     }
163 }
164
165 static hdr * hdr_free_list = 0;
166
167 /* Return an uninitialized header */
168 static hdr * alloc_hdr(void)
169 {
170     register hdr * result;
171     
172     if (hdr_free_list == 0) {
173         result = (hdr *) GC_scratch_alloc((word)(sizeof(hdr)));
174     } else {
175         result = hdr_free_list;
176         hdr_free_list = (hdr *) (result -> hb_next);
177     }
178     return(result);
179 }
180
181 static void free_hdr(hdr * hhdr)
182 {
183     hhdr -> hb_next = (struct hblk *) hdr_free_list;
184     hdr_free_list = hhdr;
185 }
186
187 #ifdef COUNT_HDR_CACHE_HITS
188   word GC_hdr_cache_hits = 0;
189   word GC_hdr_cache_misses = 0;
190 #endif
191  
192 void GC_init_headers(void)
193 {
194     register unsigned i;
195     
196     GC_all_nils = (bottom_index *)GC_scratch_alloc((word)sizeof(bottom_index));
197     BZERO(GC_all_nils, sizeof(bottom_index));
198     for (i = 0; i < TOP_SZ; i++) {
199         GC_top_index[i] = GC_all_nils;
200     }
201 }
202
203 /* Make sure that there is a bottom level index block for address addr  */
204 /* Return FALSE on failure.                                             */
205 static GC_bool get_index(word addr)
206 {
207     word hi = (word)(addr) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
208     bottom_index * r;
209     bottom_index * p;
210     bottom_index ** prev;
211     bottom_index *pi;
212     
213 #   ifdef HASH_TL
214       word i = TL_HASH(hi);
215       bottom_index * old;
216       
217       old = p = GC_top_index[i];
218       while(p != GC_all_nils) {
219           if (p -> key == hi) return(TRUE);
220           p = p -> hash_link;
221       }
222       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
223       if (r == 0) return(FALSE);
224       BZERO(r, sizeof (bottom_index));
225       r -> hash_link = old;
226       GC_top_index[i] = r;
227 #   else
228       if (GC_top_index[hi] != GC_all_nils) return(TRUE);
229       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
230       if (r == 0) return(FALSE);
231       GC_top_index[hi] = r;
232       BZERO(r, sizeof (bottom_index));
233 #   endif
234     r -> key = hi;
235     /* Add it to the list of bottom indices */
236       prev = &GC_all_bottom_indices;    /* pointer to p */
237       pi = 0;                           /* bottom_index preceding p */
238       while ((p = *prev) != 0 && p -> key < hi) {
239         pi = p;
240         prev = &(p -> asc_link);
241       }
242       r -> desc_link = pi;
243       if (0 == p) {
244         GC_all_bottom_indices_end = r;
245       } else {
246         p -> desc_link = r;
247       }
248       r -> asc_link = p;
249       *prev = r;
250     return(TRUE);
251 }
252
253 /* Install a header for block h.        */
254 /* The header is uninitialized.         */
255 /* Returns the header or 0 on failure.  */
256 struct hblkhdr * GC_install_header(struct hblk *h)
257 {
258     hdr * result;
259     
260     if (!get_index((word) h)) return(0);
261     result = alloc_hdr();
262     SET_HDR(h, result);
263 #   ifdef USE_MUNMAP
264         result -> hb_last_reclaimed = (unsigned short)GC_gc_no;
265 #   endif
266     return(result);
267 }
268
269 /* Set up forwarding counts for block h of size sz */
270 GC_bool GC_install_counts(struct hblk *h, size_t sz/* bytes */)
271 {
272     struct hblk * hbp;
273     word i;
274     
275     for (hbp = h; (char *)hbp < (char *)h + sz; hbp += BOTTOM_SZ) {
276         if (!get_index((word) hbp)) return(FALSE);
277     }
278     if (!get_index((word)h + sz - 1)) return(FALSE);
279     for (hbp = h + 1; (char *)hbp < (char *)h + sz; hbp += 1) {
280         i = HBLK_PTR_DIFF(hbp, h);
281         SET_HDR(hbp, (hdr *)(i > MAX_JUMP? MAX_JUMP : i));
282     }
283     return(TRUE);
284 }
285
286 /* Remove the header for block h */
287 void GC_remove_header(struct hblk *h)
288 {
289     hdr ** ha;
290     
291     GET_HDR_ADDR(h, ha);
292     free_hdr(*ha);
293     *ha = 0;
294 }
295
296 /* Remove forwarding counts for h */
297 void GC_remove_counts(struct hblk *h, size_t sz/* bytes */)
298 {
299     register struct hblk * hbp;
300     
301     for (hbp = h+1; (char *)hbp < (char *)h + sz; hbp += 1) {
302         SET_HDR(hbp, 0);
303     }
304 }
305
306 /* Apply fn to all allocated blocks */
307 /*VARARGS1*/
308 void GC_apply_to_all_blocks(void (*fn)(struct hblk *h, word client_data),
309                             word client_data)
310 {
311     signed_word j;
312     bottom_index * index_p;
313     
314     for (index_p = GC_all_bottom_indices; index_p != 0;
315          index_p = index_p -> asc_link) {
316         for (j = BOTTOM_SZ-1; j >= 0;) {
317             if (!IS_FORWARDING_ADDR_OR_NIL(index_p->index[j])) {
318                 if (!HBLK_IS_FREE(index_p->index[j])) {
319                     (*fn)(((struct hblk *)
320                               (((index_p->key << LOG_BOTTOM_SZ) + (word)j)
321                                << LOG_HBLKSIZE)),
322                           client_data);
323                 }
324                 j--;
325              } else if (index_p->index[j] == 0) {
326                 j--;
327              } else {
328                 j -= (signed_word)(index_p->index[j]);
329              }
330          }
331      }
332 }
333
334 /* Get the next valid block whose address is at least h */
335 /* Return 0 if there is none.                           */
336 struct hblk * GC_next_used_block(struct hblk *h)
337 {
338     register bottom_index * bi;
339     register word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
340     
341     GET_BI(h, bi);
342     if (bi == GC_all_nils) {
343         register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
344         bi = GC_all_bottom_indices;
345         while (bi != 0 && bi -> key < hi) bi = bi -> asc_link;
346         j = 0;
347     }
348     while(bi != 0) {
349         while (j < BOTTOM_SZ) {
350             hdr * hhdr = bi -> index[j];
351             if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
352                 j++;
353             } else {
354                 if (!HBLK_IS_FREE(hhdr)) {
355                     return((struct hblk *)
356                               (((bi -> key << LOG_BOTTOM_SZ) + j)
357                                << LOG_HBLKSIZE));
358                 } else {
359                     j += divHBLKSZ(hhdr -> hb_sz);
360                 }
361             }
362         }
363         j = 0;
364         bi = bi -> asc_link;
365     }
366     return(0);
367 }
368
369 /* Get the last (highest address) block whose address is        */
370 /* at most h.  Return 0 if there is none.                       */
371 /* Unlike the above, this may return a free block.              */
372 struct hblk * GC_prev_block(struct hblk *h)
373 {
374     register bottom_index * bi;
375     register signed_word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
376     
377     GET_BI(h, bi);
378     if (bi == GC_all_nils) {
379         register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
380         bi = GC_all_bottom_indices_end;
381         while (bi != 0 && bi -> key > hi) bi = bi -> desc_link;
382         j = BOTTOM_SZ - 1;
383     }
384     while(bi != 0) {
385         while (j >= 0) {
386             hdr * hhdr = bi -> index[j];
387             if (0 == hhdr) {
388                 --j;
389             } else if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
390                 j -= (signed_word)hhdr;
391             } else {
392                 return((struct hblk *)
393                           (((bi -> key << LOG_BOTTOM_SZ) + j)
394                                << LOG_HBLKSIZE));
395             }
396         }
397         j = BOTTOM_SZ - 1;
398         bi = bi -> desc_link;
399     }
400     return(0);
401 }