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