blob: 84749639c294fbf407f0da021e622aeca241f341 [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);
84typedef void BitmapSweepCallback(size_t numPtrs, void **ptrs, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080085
86/*
87 * Initialize a HeapBitmap so that it points to a bitmap large
88 * enough to cover a heap at <base> of <maxSize> bytes, where
89 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
90 */
91bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
92 const char *name);
93
94/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080095 * Clean up any resources associated with the bitmap.
96 */
97void dvmHeapBitmapDelete(HeapBitmap *hb);
98
99/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800100 * Fill the bitmap with zeroes. Returns the bitmap's memory to
101 * the system as a side-effect.
102 */
103void dvmHeapBitmapZero(HeapBitmap *hb);
104
105/*
106 * Walk through the bitmaps in increasing address order, and find the
Barry Hayesacdea992010-07-21 12:03:02 -0700107 * object pointers that correspond to garbage objects. Call
108 * <callback> zero or more times with lists of these object pointers.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800109 *
110 * The <finger> argument to the callback indicates the next-highest
111 * address that hasn't been visited yet; setting bits for objects whose
112 * addresses are less than <finger> are not guaranteed to be seen by
Barry Hayesacdea992010-07-21 12:03:02 -0700113 * the current walk.
114 *
115 * The callback is permitted to increase the bitmap's max; the walk
116 * will use the updated max as a terminating condition,
117 *
118 * <finger> will be set to some value beyond the bitmap max when the
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800119 * end of the bitmap is reached.
120 */
Barry Hayesacdea992010-07-21 12:03:02 -0700121void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb,
Carl Shapiro57ee2702010-08-27 13:06:48 -0700122 BitmapSweepCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800123
124/*
Barry Hayesacdea992010-07-21 12:03:02 -0700125 * Similar to dvmHeapBitmapSweepWalk(), but visit the set bits
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800126 * in a single bitmap.
127 */
Carl Shapiro57ee2702010-08-27 13:06:48 -0700128HB_INLINE_PROTO(
129 void
130 dvmHeapBitmapWalk(const HeapBitmap *bitmap,
131 BitmapCallback *callback, void *arg)
132)
133{
134 assert(bitmap != NULL);
135 assert(bitmap->bits != NULL);
136 assert(callback != NULL);
137 uintptr_t end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base);
138 uintptr_t i;
139 for (i = 0; i <= end; ++i) {
140 unsigned long word = bitmap->bits[i];
141 if (UNLIKELY(word != 0)) {
142 unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1);
143 uintptr_t ptrBase = HB_INDEX_TO_OFFSET(i) + bitmap->base;
144 while (word != 0) {
145 const int shift = CLZ(word);
146 word &= ~(highBit >> shift);
147 void *addr = (void *)(ptrBase + shift * HB_OBJECT_ALIGNMENT);
148 (*callback)(addr, arg);
Carl Shapiro57ee2702010-08-27 13:06:48 -0700149 }
Carl Shapiroa634c852010-08-30 11:10:45 -0700150 end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base);
Carl Shapiro57ee2702010-08-27 13:06:48 -0700151 }
152 }
153}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800154
155/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800156 * Return true iff <obj> is within the range of pointers that this
157 * bitmap could potentially cover, even if a bit has not been set
158 * for it.
159 */
160HB_INLINE_PROTO(
161 bool
162 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
163)
164{
165 assert(hb != NULL);
166
167 if (obj != NULL) {
168 const uintptr_t offset = (uintptr_t)obj - hb->base;
169 const size_t index = HB_OFFSET_TO_INDEX(offset);
170 return index < hb->bitsLen / sizeof(*hb->bits);
171 }
172 return false;
173}
174
175/*
176 * Internal function; do not call directly.
177 */
178HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700179 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800180 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
181 bool setBit, bool returnOld)
182)
183{
184 const uintptr_t offset = (uintptr_t)obj - hb->base;
185 const size_t index = HB_OFFSET_TO_INDEX(offset);
Carl Shapiro9cbece22010-07-12 17:45:51 -0700186 const unsigned long mask = HB_OFFSET_TO_MASK(offset);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800187
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800188 assert(hb->bits != NULL);
189 assert((uintptr_t)obj >= hb->base);
190 assert(index < hb->bitsLen / sizeof(*hb->bits));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800191
192 if (setBit) {
193 if ((uintptr_t)obj > hb->max) {
194 hb->max = (uintptr_t)obj;
195 }
196 if (returnOld) {
Carl Shapiro9cbece22010-07-12 17:45:51 -0700197 unsigned long *p = hb->bits + index;
198 const unsigned long word = *p;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800199 *p |= mask;
200 return word & mask;
201 } else {
202 hb->bits[index] |= mask;
203 }
204 } else {
205 hb->bits[index] &= ~mask;
206 }
207 return false;
208}
209
210/*
211 * Sets the bit corresponding to <obj>, and returns the previous value
212 * of that bit (as zero or non-zero). Does no range checking to see if
213 * <obj> is outside of the coverage of the bitmap.
214 *
215 * NOTE: casting this value to a bool is dangerous, because higher
216 * set bits will be lost.
217 */
218HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700219 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800220 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
221)
222{
223 return _heapBitmapModifyObjectBit(hb, obj, true, true);
224}
225
226/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800227 * Sets the bit corresponding to <obj>, and widens the range of seen
228 * pointers if necessary. Does no range checking.
229 */
230HB_INLINE_PROTO(
231 void
232 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
233)
234{
235 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
236}
237
238/*
239 * Clears the bit corresponding to <obj>. Does no range checking.
240 */
241HB_INLINE_PROTO(
242 void
243 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
244)
245{
246 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
247}
248
249/*
250 * Returns the current value of the bit corresponding to <obj>,
251 * as zero or non-zero. Does no range checking.
252 *
253 * NOTE: casting this value to a bool is dangerous, because higher
254 * set bits will be lost.
255 */
256HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700257 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800258 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
259)
260{
261 assert(dvmHeapBitmapCoversAddress(hb, obj));
262 assert(hb->bits != NULL);
263 assert((uintptr_t)obj >= hb->base);
264
265 if ((uintptr_t)obj <= hb->max) {
266 const uintptr_t offset = (uintptr_t)obj - hb->base;
267 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
268 } else {
269 return 0;
270 }
271}
272
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800273#undef HB_INLINE_PROTO
274
275#endif // _DALVIK_HEAP_BITMAP