blob: b9f81645cad14748a48fdcf9d9e53737c719a5c5 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef _DALVIK_HEAP_BITMAP
17#define _DALVIK_HEAP_BITMAP
18
19#include <stdint.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080020
21#define HB_OBJECT_ALIGNMENT 8
22#define HB_BITS_PER_WORD (sizeof (unsigned long int) * 8)
23
24/* <offset> is the difference from .base to a pointer address.
25 * <index> is the index of .bits that contains the bit representing
26 * <offset>.
27 */
28#define HB_OFFSET_TO_INDEX(offset_) \
29 ((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT / HB_BITS_PER_WORD)
30#define HB_INDEX_TO_OFFSET(index_) \
31 ((uintptr_t)(index_) * HB_OBJECT_ALIGNMENT * HB_BITS_PER_WORD)
32
33/* Pack the bits in backwards so they come out in address order
34 * when using CLZ.
35 */
36#define HB_OFFSET_TO_MASK(offset_) \
37 (1 << \
38 (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD)))
39
40/* Return the maximum offset (exclusive) that <hb> can represent.
41 */
42#define HB_MAX_OFFSET(hb_) \
43 HB_INDEX_TO_OFFSET((hb_)->bitsLen / sizeof(*(hb_)->bits))
44
45#define HB_INLINE_PROTO(p) \
46 static inline p __attribute__((always_inline)); \
47 static inline p
48
49
50typedef struct {
51 /* The bitmap data, which points to an mmap()ed area of zeroed
52 * anonymous memory.
53 */
54 unsigned long int *bits;
55
Carl Shapiro8d724802010-02-14 18:40:47 -080056 /* The size of the used memory pointed to by bits, in bytes. This
57 * value changes when the bitmap is shrunk.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080058 */
59 size_t bitsLen;
60
Carl Shapiro8d724802010-02-14 18:40:47 -080061 /* The real size of the memory pointed to by bits. This is the
62 * number of bytes we requested from the allocator and does not
63 * change.
64 */
65 size_t allocLen;
66
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080067 /* The base address, which corresponds to the first bit in
68 * the bitmap.
69 */
70 uintptr_t base;
71
72 /* The highest pointer value ever returned by an allocation
73 * from this heap. I.e., the highest address that may correspond
74 * to a set bit. If there are no bits set, (max < base).
75 */
76 uintptr_t max;
77} HeapBitmap;
78
79
80/*
81 * Initialize a HeapBitmap so that it points to a bitmap large
82 * enough to cover a heap at <base> of <maxSize> bytes, where
83 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
84 */
85bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
86 const char *name);
87
88/*
89 * Initialize <hb> so that it covers the same extent as <templateBitmap>.
90 */
91bool dvmHeapBitmapInitFromTemplate(HeapBitmap *hb,
92 const HeapBitmap *templateBitmap, const char *name);
93
94/*
95 * Initialize the bitmaps in <out> so that they cover the same extent as
96 * the corresponding bitmaps in <templates>.
97 */
98bool dvmHeapBitmapInitListFromTemplates(HeapBitmap out[],
99 HeapBitmap templates[], size_t numBitmaps, const char *name);
100
101/*
102 * Clean up any resources associated with the bitmap.
103 */
104void dvmHeapBitmapDelete(HeapBitmap *hb);
105
106/*
107 * Clean up any resources associated with the bitmaps.
108 */
109void dvmHeapBitmapDeleteList(HeapBitmap hbs[], size_t numBitmaps);
110
111/*
112 * Fill the bitmap with zeroes. Returns the bitmap's memory to
113 * the system as a side-effect.
114 */
115void dvmHeapBitmapZero(HeapBitmap *hb);
116
117/*
118 * Walk through the bitmaps in increasing address order, and find the
119 * object pointers that correspond to places where the bitmaps differ.
120 * Call <callback> zero or more times with lists of these object pointers.
121 *
122 * The <finger> argument to the callback indicates the next-highest
123 * address that hasn't been visited yet; setting bits for objects whose
124 * addresses are less than <finger> are not guaranteed to be seen by
125 * the current XorWalk. <finger> will be set to ULONG_MAX when the
126 * end of the bitmap is reached.
127 */
128bool dvmHeapBitmapXorWalk(const HeapBitmap *hb1, const HeapBitmap *hb2,
129 bool (*callback)(size_t numPtrs, void **ptrs,
130 const void *finger, void *arg),
131 void *callbackArg);
132
133/*
134 * Similar to dvmHeapBitmapXorWalk(), but compare multiple bitmaps.
135 * Regardless of the order of the arrays, the bitmaps will be visited
136 * in address order, so that finger will increase monotonically.
137 */
138bool dvmHeapBitmapXorWalkLists(const HeapBitmap hbs1[], const HeapBitmap hbs2[],
139 size_t numBitmaps,
140 bool (*callback)(size_t numPtrs, void **ptrs,
141 const void *finger, void *arg),
142 void *callbackArg);
143
144/*
145 * Similar to dvmHeapBitmapXorWalk(), but visit the set bits
146 * in a single bitmap.
147 */
148bool dvmHeapBitmapWalk(const HeapBitmap *hb,
149 bool (*callback)(size_t numPtrs, void **ptrs,
150 const void *finger, void *arg),
151 void *callbackArg);
152
153/*
154 * Similar to dvmHeapBitmapXorWalkList(), but visit the set bits
155 * in a single list of bitmaps. Regardless of the order of the array,
156 * the bitmaps will be visited in address order, so that finger will
157 * increase monotonically.
158 */
159bool dvmHeapBitmapWalkList(const HeapBitmap hbs[], size_t numBitmaps,
160 bool (*callback)(size_t numPtrs, void **ptrs,
161 const void *finger, void *arg),
162 void *callbackArg);
163/*
164 * Return true iff <obj> is within the range of pointers that
165 * have had corresponding bits set in this bitmap.
166 */
167HB_INLINE_PROTO(
168 bool
169 dvmHeapBitmapMayContainObject(const HeapBitmap *hb,
170 const void *obj)
171)
172{
173 const uintptr_t p = (const uintptr_t)obj;
174
175 assert((p & (HB_OBJECT_ALIGNMENT - 1)) == 0);
176
177 return p >= hb->base && p <= hb->max;
178}
179
180/*
181 * Return true iff <obj> is within the range of pointers that this
182 * bitmap could potentially cover, even if a bit has not been set
183 * for it.
184 */
185HB_INLINE_PROTO(
186 bool
187 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
188)
189{
190 assert(hb != NULL);
191
192 if (obj != NULL) {
193 const uintptr_t offset = (uintptr_t)obj - hb->base;
194 const size_t index = HB_OFFSET_TO_INDEX(offset);
195 return index < hb->bitsLen / sizeof(*hb->bits);
196 }
197 return false;
198}
199
200/*
201 * Internal function; do not call directly.
202 */
203HB_INLINE_PROTO(
204 unsigned long int
205 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
206 bool setBit, bool returnOld)
207)
208{
209 const uintptr_t offset = (uintptr_t)obj - hb->base;
210 const size_t index = HB_OFFSET_TO_INDEX(offset);
211 const unsigned long int mask = HB_OFFSET_TO_MASK(offset);
212
213#ifndef NDEBUG
214 assert(hb->bits != NULL);
215 assert((uintptr_t)obj >= hb->base);
216 assert(index < hb->bitsLen / sizeof(*hb->bits));
217#endif
218
219 if (setBit) {
220 if ((uintptr_t)obj > hb->max) {
221 hb->max = (uintptr_t)obj;
222 }
223 if (returnOld) {
224 unsigned long int *p = hb->bits + index;
225 const unsigned long int word = *p;
226 *p |= mask;
227 return word & mask;
228 } else {
229 hb->bits[index] |= mask;
230 }
231 } else {
232 hb->bits[index] &= ~mask;
233 }
234 return false;
235}
236
237/*
238 * Sets the bit corresponding to <obj>, and returns the previous value
239 * of that bit (as zero or non-zero). Does no range checking to see if
240 * <obj> is outside of the coverage of the bitmap.
241 *
242 * NOTE: casting this value to a bool is dangerous, because higher
243 * set bits will be lost.
244 */
245HB_INLINE_PROTO(
246 unsigned long int
247 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
248)
249{
250 return _heapBitmapModifyObjectBit(hb, obj, true, true);
251}
252
253/*
254 * Like dvmHeapBitmapSetAndReturnObjectBit(), but sets/returns the bit
255 * in the appropriate bitmap. Results are undefined if <obj> is not
256 * covered by any bitmap.
257 */
258HB_INLINE_PROTO(
259 unsigned long int
260 dvmHeapBitmapSetAndReturnObjectBitInList(HeapBitmap hbs[],
261 size_t numBitmaps, const void *obj)
262)
263{
264 size_t i;
265
266 for (i = 0; i < numBitmaps; i++) {
267 if (dvmHeapBitmapCoversAddress(&hbs[i], obj)) {
268 return dvmHeapBitmapSetAndReturnObjectBit(&hbs[i], obj);
269 }
270 }
271
272 assert(!"object not covered by any bitmap");
273 return false;
274}
275
276/*
277 * Sets the bit corresponding to <obj>, and widens the range of seen
278 * pointers if necessary. Does no range checking.
279 */
280HB_INLINE_PROTO(
281 void
282 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
283)
284{
285 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
286}
287
288/*
289 * Clears the bit corresponding to <obj>. Does no range checking.
290 */
291HB_INLINE_PROTO(
292 void
293 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
294)
295{
296 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
297}
298
299/*
300 * Returns the current value of the bit corresponding to <obj>,
301 * as zero or non-zero. Does no range checking.
302 *
303 * NOTE: casting this value to a bool is dangerous, because higher
304 * set bits will be lost.
305 */
306HB_INLINE_PROTO(
307 unsigned long int
308 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
309)
310{
311 assert(dvmHeapBitmapCoversAddress(hb, obj));
312 assert(hb->bits != NULL);
313 assert((uintptr_t)obj >= hb->base);
314
315 if ((uintptr_t)obj <= hb->max) {
316 const uintptr_t offset = (uintptr_t)obj - hb->base;
317 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
318 } else {
319 return 0;
320 }
321}
322
323/*
324 * Looks through the list of bitmaps and returns the current value of the
325 * bit corresponding to <obj>, which may be covered by any of the bitmaps.
326 * Does no range checking.
327 */
328HB_INLINE_PROTO(
329 long
330 dvmHeapBitmapIsObjectBitSetInList(const HeapBitmap hbs[], size_t numBitmaps,
331 const void *obj)
332)
333{
334 size_t i;
335
336 for (i = 0; i < numBitmaps; i++) {
337 if (dvmHeapBitmapCoversAddress(&hbs[i], obj)) {
338 return dvmHeapBitmapIsObjectBitSet(&hbs[i], obj);
339 }
340 }
341 return false;
342}
343
344#undef HB_INLINE_PROTO
345
346#endif // _DALVIK_HEAP_BITMAP