Debian Codegraph @

gc/include/private/gc_hdrs.h

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