Upgrade Boehm GC to 7.2alpha4.
[cacao.git] / src / mm / boehm-gc / include / private / gc_hdrs.h
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 /* Boehm, July 11, 1995 11:54 am PDT */
15 # ifndef GC_HEADERS_H
16 # define GC_HEADERS_H
17 typedef struct hblkhdr hdr;
18
19 # if CPP_WORDSZ != 32 && CPP_WORDSZ < 36
20         --> Get a real machine.
21 # endif
22
23 /*
24  * The 2 level tree data structure that is used to find block headers.
25  * If there are more than 32 bits in a pointer, the top level is a hash
26  * table.
27  *
28  * This defines HDR, GET_HDR, and SET_HDR, the main macros used to
29  * retrieve and set object headers.
30  *
31  * We take advantage of a header lookup
32  * cache.  This is a locally declared direct mapped cache, used inside
33  * the marker.  The HC_GET_HDR macro uses and maintains this
34  * cache.  Assuming we get reasonable hit rates, this shaves a few
35  * memory references from each pointer validation.
36  */
37
38 # if CPP_WORDSZ > 32
39 #   define HASH_TL
40 # endif
41
42 /* Define appropriate out-degrees for each of the two tree levels       */
43 # ifdef SMALL_CONFIG
44 #   define LOG_BOTTOM_SZ 11
45         /* Keep top index size reasonable with smaller blocks. */
46 # else
47 #   define LOG_BOTTOM_SZ 10
48 # endif
49 # ifndef HASH_TL
50 #   define LOG_TOP_SZ (WORDSZ - LOG_BOTTOM_SZ - LOG_HBLKSIZE)
51 # else
52 #   define LOG_TOP_SZ 11
53 # endif
54 # define TOP_SZ (1 << LOG_TOP_SZ)
55 # define BOTTOM_SZ (1 << LOG_BOTTOM_SZ)
56
57 /* #define COUNT_HDR_CACHE_HITS  */
58
59 # ifdef COUNT_HDR_CACHE_HITS
60     extern word GC_hdr_cache_hits; /* used for debugging/profiling */
61     extern word GC_hdr_cache_misses;
62 #   define HC_HIT() ++GC_hdr_cache_hits
63 #   define HC_MISS() ++GC_hdr_cache_misses
64 # else
65 #   define HC_HIT()
66 #   define HC_MISS()
67 # endif
68
69   typedef struct hce {
70     word block_addr;    /* right shifted by LOG_HBLKSIZE */
71     hdr * hce_hdr;
72   } hdr_cache_entry;
73
74 # define HDR_CACHE_SIZE 8  /* power of 2 */
75
76 # define DECLARE_HDR_CACHE \
77         hdr_cache_entry hdr_cache[HDR_CACHE_SIZE]
78
79 # define INIT_HDR_CACHE BZERO(hdr_cache, sizeof(hdr_cache))
80
81 # define HCE(h) hdr_cache + (((word)(h) >> LOG_HBLKSIZE) & (HDR_CACHE_SIZE-1))
82
83 # define HCE_VALID_FOR(hce,h) ((hce) -> block_addr == \
84                                 ((word)(h) >> LOG_HBLKSIZE))
85
86 # define HCE_HDR(h) ((hce) -> hce_hdr)
87
88 #ifdef PRINT_BLACK_LIST
89   GC_INNER hdr * GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce,
90                                       ptr_t source);
91 # define HEADER_CACHE_MISS(p, hce, source) \
92           GC_header_cache_miss(p, hce, source)
93 #else
94   GC_INNER hdr * GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce);
95 # define HEADER_CACHE_MISS(p, hce, source) GC_header_cache_miss(p, hce)
96 #endif
97
98 /* Set hhdr to the header for p.  Analogous to GET_HDR below,           */
99 /* except that in the case of large objects, it                         */
100 /* gets the header for the object beginning, if GC_all_interior_ptrs    */
101 /* is set.                                                              */
102 /* Returns zero if p points to somewhere other than the first page      */
103 /* of an object, and it is not a valid pointer to the object.           */
104 # define HC_GET_HDR(p, hhdr, source, exit_label) \
105         { \
106           hdr_cache_entry * hce = HCE(p); \
107           if (EXPECT(HCE_VALID_FOR(hce, p), 1)) { \
108             HC_HIT(); \
109             hhdr = hce -> hce_hdr; \
110           } else { \
111             hhdr = HEADER_CACHE_MISS(p, hce, source); \
112             if (0 == hhdr) goto exit_label; \
113           } \
114         }
115
116 typedef struct bi {
117     hdr * index[BOTTOM_SZ];
118         /*
119          * The bottom level index contains one of three kinds of values:
120          * 0 means we're not responsible for this block,
121          *   or this is a block other than the first one in a free block.
122          * 1 < (long)X <= MAX_JUMP means the block starts at least
123          *        X * HBLKSIZE bytes before the current address.
124          * A valid pointer points to a hdr structure. (The above can't be
125          * valid pointers due to the GET_MEM return convention.)
126          */
127     struct bi * asc_link;       /* All indices are linked in    */
128                                 /* ascending order...           */
129     struct bi * desc_link;      /* ... and in descending order. */
130     word key;                   /* high order address bits.     */
131 # ifdef HASH_TL
132     struct bi * hash_link;      /* Hash chain link.             */
133 # endif
134 } bottom_index;
135
136 /* bottom_index GC_all_nils; - really part of GC_arrays */
137
138 /* extern bottom_index * GC_top_index []; - really part of GC_arrays */
139                                 /* Each entry points to a bottom_index. */
140                                 /* On a 32 bit machine, it points to    */
141                                 /* the index for a set of high order    */
142                                 /* bits equal to the index.  For longer */
143                                 /* addresses, we hash the high order    */
144                                 /* bits to compute the index in         */
145                                 /* GC_top_index, and each entry points  */
146                                 /* to a hash chain.                     */
147                                 /* The last entry in each chain is      */
148                                 /* GC_all_nils.                         */
149
150
151 # define MAX_JUMP (HBLKSIZE - 1)
152
153 # define HDR_FROM_BI(bi, p) \
154                 ((bi)->index[((word)(p) >> LOG_HBLKSIZE) & (BOTTOM_SZ - 1)])
155 # ifndef HASH_TL
156 #   define BI(p) (GC_top_index \
157                 [(word)(p) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE)])
158 #   define HDR_INNER(p) HDR_FROM_BI(BI(p),p)
159 #   ifdef SMALL_CONFIG
160 #       define HDR(p) GC_find_header((ptr_t)(p))
161 #   else
162 #       define HDR(p) HDR_INNER(p)
163 #   endif
164 #   define GET_BI(p, bottom_indx) (bottom_indx) = BI(p)
165 #   define GET_HDR(p, hhdr) (hhdr) = HDR(p)
166 #   define SET_HDR(p, hhdr) HDR_INNER(p) = (hhdr)
167 #   define GET_HDR_ADDR(p, ha) (ha) = &(HDR_INNER(p))
168 # else /* hash */
169 /*  Hash function for tree top level */
170 #   define TL_HASH(hi) ((hi) & (TOP_SZ - 1))
171 /*  Set bottom_indx to point to the bottom index for address p */
172 #   define GET_BI(p, bottom_indx) \
173         { \
174             register word hi = \
175                 (word)(p) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE); \
176             register bottom_index * _bi = GC_top_index[TL_HASH(hi)]; \
177             \
178             while (_bi -> key != hi && _bi != GC_all_nils) \
179                 _bi = _bi -> hash_link; \
180             (bottom_indx) = _bi; \
181         }
182 #   define GET_HDR_ADDR(p, ha) \
183         { \
184             register bottom_index * bi; \
185             \
186             GET_BI(p, bi);      \
187             (ha) = &(HDR_FROM_BI(bi, p)); \
188         }
189 #   define GET_HDR(p, hhdr) { register hdr ** _ha; GET_HDR_ADDR(p, _ha); \
190                               (hhdr) = *_ha; }
191 #   define SET_HDR(p, hhdr) { register hdr ** _ha; GET_HDR_ADDR(p, _ha); \
192                               *_ha = (hhdr); }
193 #   define HDR(p) GC_find_header((ptr_t)(p))
194 # endif
195
196 /* Is the result a forwarding address to someplace closer to the        */
197 /* beginning of the block or NIL?                                       */
198 # define IS_FORWARDING_ADDR_OR_NIL(hhdr) ((size_t) (hhdr) <= MAX_JUMP)
199
200 /* Get an HBLKSIZE aligned address closer to the beginning of the block */
201 /* h.  Assumes hhdr == HDR(h) and IS_FORWARDING_ADDR(hhdr).             */
202 # define FORWARDED_ADDR(h, hhdr) ((struct hblk *)(h) - (size_t)(hhdr))
203 # endif /*  GC_HEADERS_H */