blob: 5f7f029875784f434154da461732ccb1a7f0ad1e [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
Carl Shapiroeff16fb2010-08-19 14:29:41 -070019#include <limits.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080020#include <stdint.h>
Carl Shapiro57ee2702010-08-27 13:06:48 -070021#include "clz.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080022
23#define HB_OBJECT_ALIGNMENT 8
Carl Shapiroeff16fb2010-08-19 14:29:41 -070024#define HB_BITS_PER_WORD (sizeof(unsigned long) * CHAR_BIT)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080025
26/* <offset> is the difference from .base to a pointer address.
27 * <index> is the index of .bits that contains the bit representing
28 * <offset>.
29 */
30#define HB_OFFSET_TO_INDEX(offset_) \
31 ((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT / HB_BITS_PER_WORD)
32#define HB_INDEX_TO_OFFSET(index_) \
33 ((uintptr_t)(index_) * HB_OBJECT_ALIGNMENT * HB_BITS_PER_WORD)
34
Carl Shapirod25566d2010-03-11 20:39:47 -080035#define HB_OFFSET_TO_BYTE_INDEX(offset_) \
36 (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits))
37
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080038/* Pack the bits in backwards so they come out in address order
39 * when using CLZ.
40 */
41#define HB_OFFSET_TO_MASK(offset_) \
42 (1 << \
43 (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD)))
44
45/* Return the maximum offset (exclusive) that <hb> can represent.
46 */
47#define HB_MAX_OFFSET(hb_) \
48 HB_INDEX_TO_OFFSET((hb_)->bitsLen / sizeof(*(hb_)->bits))
49
50#define HB_INLINE_PROTO(p) \
51 static inline p __attribute__((always_inline)); \
52 static inline p
53
Carl Shapiro7511ae12010-08-19 16:54:01 -070054typedef struct {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080055 /* The bitmap data, which points to an mmap()ed area of zeroed
56 * anonymous memory.
57 */
Carl Shapiro9cbece22010-07-12 17:45:51 -070058 unsigned long *bits;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080059
Carl Shapiro8d724802010-02-14 18:40:47 -080060 /* The size of the used memory pointed to by bits, in bytes. This
61 * value changes when the bitmap is shrunk.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080062 */
63 size_t bitsLen;
64
Carl Shapiro8d724802010-02-14 18:40:47 -080065 /* The real size of the memory pointed to by bits. This is the
66 * number of bytes we requested from the allocator and does not
67 * change.
68 */
69 size_t allocLen;
70
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080071 /* The base address, which corresponds to the first bit in
72 * the bitmap.
73 */
74 uintptr_t base;
75
76 /* The highest pointer value ever returned by an allocation
77 * from this heap. I.e., the highest address that may correspond
78 * to a set bit. If there are no bits set, (max < base).
79 */
80 uintptr_t max;
Carl Shapiro7511ae12010-08-19 16:54:01 -070081} HeapBitmap;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080082
Carl Shapiro57ee2702010-08-27 13:06:48 -070083typedef void BitmapCallback(void *addr, void *arg);
Carl Shapiro38d710b2010-08-31 16:48:31 -070084typedef void BitmapScanCallback(void *addr, void *finger, void *arg);
Carl Shapiro57ee2702010-08-27 13:06:48 -070085typedef void BitmapSweepCallback(size_t numPtrs, void **ptrs, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080086
87/*
88 * Initialize a HeapBitmap so that it points to a bitmap large
89 * enough to cover a heap at <base> of <maxSize> bytes, where
90 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
91 */
92bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
93 const char *name);
94
95/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080096 * Clean up any resources associated with the bitmap.
97 */
98void dvmHeapBitmapDelete(HeapBitmap *hb);
99
100/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800101 * Fill the bitmap with zeroes. Returns the bitmap's memory to
102 * the system as a side-effect.
103 */
104void dvmHeapBitmapZero(HeapBitmap *hb);
105
106/*
Carl Shapiro38d710b2010-08-31 16:48:31 -0700107 * Visits set bits in address order. The callback is not permitted to
108 * change the bitmap bits or max during the traversal.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800109 */
Carl Shapiro57ee2702010-08-27 13:06:48 -0700110HB_INLINE_PROTO(
111 void
112 dvmHeapBitmapWalk(const HeapBitmap *bitmap,
113 BitmapCallback *callback, void *arg)
114)
115{
116 assert(bitmap != NULL);
117 assert(bitmap->bits != NULL);
118 assert(callback != NULL);
119 uintptr_t end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base);
120 uintptr_t i;
121 for (i = 0; i <= end; ++i) {
122 unsigned long word = bitmap->bits[i];
123 if (UNLIKELY(word != 0)) {
124 unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1);
125 uintptr_t ptrBase = HB_INDEX_TO_OFFSET(i) + bitmap->base;
126 while (word != 0) {
127 const int shift = CLZ(word);
Carl Shapiro57ee2702010-08-27 13:06:48 -0700128 void *addr = (void *)(ptrBase + shift * HB_OBJECT_ALIGNMENT);
129 (*callback)(addr, arg);
Carl Shapiro38d710b2010-08-31 16:48:31 -0700130 word &= ~(highBit >> shift);
131 }
132 }
133 }
134}
135
136/*
137 * Similar to dvmHeapBitmapWalk but the callback routine is permitted
138 * to change the bitmap bits and max during traversal. Used by the
139 * the root marking scan exclusively.
140 *
141 * The callback is invoked with a finger argument. The finger is a
142 * pointer to an address not yet visited by the traversal. If the
143 * callback sets a bit for an address at or above the finger, this
144 * address will be visited by the traversal. If the callback sets a
145 * bit for an address below the finger, this address will not be
146 * visited.
147 */
148HB_INLINE_PROTO(
149 void
150 dvmHeapBitmapScanWalk(HeapBitmap *bitmap,
151 BitmapScanCallback *callback, void *arg)
152)
153{
154 assert(bitmap != NULL);
155 assert(bitmap->bits != NULL);
156 assert(callback != NULL);
157 uintptr_t end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base);
158 uintptr_t i;
159 for (i = 0; i <= end; ++i) {
160 unsigned long word = bitmap->bits[i];
161 if (UNLIKELY(word != 0)) {
162 unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1);
163 uintptr_t ptrBase = HB_INDEX_TO_OFFSET(i) + bitmap->base;
164 void *finger = (void *)(HB_INDEX_TO_OFFSET(i + 1) + bitmap->base);
165 while (word != 0) {
166 const int shift = CLZ(word);
167 void *addr = (void *)(ptrBase + shift * HB_OBJECT_ALIGNMENT);
168 (*callback)(addr, finger, arg);
169 word &= ~(highBit >> shift);
Carl Shapiro57ee2702010-08-27 13:06:48 -0700170 }
Carl Shapiroa634c852010-08-30 11:10:45 -0700171 end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base);
Carl Shapiro57ee2702010-08-27 13:06:48 -0700172 }
173 }
174}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800175
176/*
Carl Shapiro38d710b2010-08-31 16:48:31 -0700177 * Walk through the bitmaps in increasing address order, and find the
178 * object pointers that correspond to garbage objects. Call
179 * <callback> zero or more times with lists of these object pointers.
180 *
Carl Shapiro879011d2010-12-02 16:41:28 -0800181 * The callback is not permitted to increase the max of either bitmap.
Carl Shapiro38d710b2010-08-31 16:48:31 -0700182 */
183void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb,
Carl Shapiro21260712010-12-09 15:24:11 -0800184 uintptr_t base, uintptr_t max,
Carl Shapiro38d710b2010-08-31 16:48:31 -0700185 BitmapSweepCallback *callback, void *callbackArg);
186
187/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800188 * Return true iff <obj> is within the range of pointers that this
189 * bitmap could potentially cover, even if a bit has not been set
190 * for it.
191 */
192HB_INLINE_PROTO(
193 bool
194 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
195)
196{
197 assert(hb != NULL);
198
199 if (obj != NULL) {
200 const uintptr_t offset = (uintptr_t)obj - hb->base;
201 const size_t index = HB_OFFSET_TO_INDEX(offset);
202 return index < hb->bitsLen / sizeof(*hb->bits);
203 }
204 return false;
205}
206
207/*
208 * Internal function; do not call directly.
209 */
210HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700211 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800212 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
213 bool setBit, bool returnOld)
214)
215{
216 const uintptr_t offset = (uintptr_t)obj - hb->base;
217 const size_t index = HB_OFFSET_TO_INDEX(offset);
Carl Shapiro9cbece22010-07-12 17:45:51 -0700218 const unsigned long mask = HB_OFFSET_TO_MASK(offset);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800219
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800220 assert(hb->bits != NULL);
221 assert((uintptr_t)obj >= hb->base);
222 assert(index < hb->bitsLen / sizeof(*hb->bits));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800223
224 if (setBit) {
225 if ((uintptr_t)obj > hb->max) {
226 hb->max = (uintptr_t)obj;
227 }
228 if (returnOld) {
Carl Shapiro9cbece22010-07-12 17:45:51 -0700229 unsigned long *p = hb->bits + index;
230 const unsigned long word = *p;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800231 *p |= mask;
232 return word & mask;
233 } else {
234 hb->bits[index] |= mask;
235 }
236 } else {
237 hb->bits[index] &= ~mask;
238 }
239 return false;
240}
241
242/*
243 * Sets the bit corresponding to <obj>, and returns the previous value
244 * of that bit (as zero or non-zero). Does no range checking to see if
245 * <obj> is outside of the coverage of the bitmap.
246 *
247 * NOTE: casting this value to a bool is dangerous, because higher
248 * set bits will be lost.
249 */
250HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700251 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800252 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
253)
254{
255 return _heapBitmapModifyObjectBit(hb, obj, true, true);
256}
257
258/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800259 * Sets the bit corresponding to <obj>, and widens the range of seen
260 * pointers if necessary. Does no range checking.
261 */
262HB_INLINE_PROTO(
263 void
264 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
265)
266{
267 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
268}
269
270/*
271 * Clears the bit corresponding to <obj>. Does no range checking.
272 */
273HB_INLINE_PROTO(
274 void
275 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
276)
277{
278 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
279}
280
281/*
282 * Returns the current value of the bit corresponding to <obj>,
283 * as zero or non-zero. Does no range checking.
284 *
285 * NOTE: casting this value to a bool is dangerous, because higher
286 * set bits will be lost.
287 */
288HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700289 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800290 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
291)
292{
293 assert(dvmHeapBitmapCoversAddress(hb, obj));
294 assert(hb->bits != NULL);
295 assert((uintptr_t)obj >= hb->base);
296
297 if ((uintptr_t)obj <= hb->max) {
298 const uintptr_t offset = (uintptr_t)obj - hb->base;
299 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
300 } else {
301 return 0;
302 }
303}
304
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800305#undef HB_INLINE_PROTO
306
307#endif // _DALVIK_HEAP_BITMAP