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