blob: 1f05c2bfade0448956d722b096201c7fc18fe1a1 [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
Carl Shapiro9cbece22010-07-12 17:45:51 -070022#define HB_BITS_PER_WORD (sizeof (unsigned long) * 8)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080023
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
Carl Shapirod25566d2010-03-11 20:39:47 -080033#define HB_OFFSET_TO_BYTE_INDEX(offset_) \
34 (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits))
35
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080036/* Pack the bits in backwards so they come out in address order
37 * when using CLZ.
38 */
39#define HB_OFFSET_TO_MASK(offset_) \
40 (1 << \
41 (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD)))
42
43/* Return the maximum offset (exclusive) that <hb> can represent.
44 */
45#define HB_MAX_OFFSET(hb_) \
46 HB_INDEX_TO_OFFSET((hb_)->bitsLen / sizeof(*(hb_)->bits))
47
48#define HB_INLINE_PROTO(p) \
49 static inline p __attribute__((always_inline)); \
50 static inline p
51
52
Barry Hayes6e5cf602010-06-22 12:32:59 -070053struct HeapBitmap {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080054 /* The bitmap data, which points to an mmap()ed area of zeroed
55 * anonymous memory.
56 */
Carl Shapiro9cbece22010-07-12 17:45:51 -070057 unsigned long *bits;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080058
Carl Shapiro8d724802010-02-14 18:40:47 -080059 /* The size of the used memory pointed to by bits, in bytes. This
60 * value changes when the bitmap is shrunk.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080061 */
62 size_t bitsLen;
63
Carl Shapiro8d724802010-02-14 18:40:47 -080064 /* The real size of the memory pointed to by bits. This is the
65 * number of bytes we requested from the allocator and does not
66 * change.
67 */
68 size_t allocLen;
69
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080070 /* The base address, which corresponds to the first bit in
71 * the bitmap.
72 */
73 uintptr_t base;
74
75 /* The highest pointer value ever returned by an allocation
76 * from this heap. I.e., the highest address that may correspond
77 * to a set bit. If there are no bits set, (max < base).
78 */
79 uintptr_t max;
Barry Hayes6e5cf602010-06-22 12:32:59 -070080};
81typedef struct HeapBitmap HeapBitmap;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080082
Carl Shapiro006346e2010-07-29 20:39:50 -070083typedef void BitmapCallback(size_t numPtrs, void **ptrs,
84 const void *finger, 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,
122 BitmapCallback *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 Shapiro006346e2010-07-29 20:39:50 -0700128void dvmHeapBitmapWalk(const HeapBitmap *hb,
129 BitmapCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800130
131/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800132 * Return true iff <obj> is within the range of pointers that this
133 * bitmap could potentially cover, even if a bit has not been set
134 * for it.
135 */
136HB_INLINE_PROTO(
137 bool
138 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
139)
140{
141 assert(hb != NULL);
142
143 if (obj != NULL) {
144 const uintptr_t offset = (uintptr_t)obj - hb->base;
145 const size_t index = HB_OFFSET_TO_INDEX(offset);
146 return index < hb->bitsLen / sizeof(*hb->bits);
147 }
148 return false;
149}
150
151/*
152 * Internal function; do not call directly.
153 */
154HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700155 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800156 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
157 bool setBit, bool returnOld)
158)
159{
160 const uintptr_t offset = (uintptr_t)obj - hb->base;
161 const size_t index = HB_OFFSET_TO_INDEX(offset);
Carl Shapiro9cbece22010-07-12 17:45:51 -0700162 const unsigned long mask = HB_OFFSET_TO_MASK(offset);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800163
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800164 assert(hb->bits != NULL);
165 assert((uintptr_t)obj >= hb->base);
166 assert(index < hb->bitsLen / sizeof(*hb->bits));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800167
168 if (setBit) {
169 if ((uintptr_t)obj > hb->max) {
170 hb->max = (uintptr_t)obj;
171 }
172 if (returnOld) {
Carl Shapiro9cbece22010-07-12 17:45:51 -0700173 unsigned long *p = hb->bits + index;
174 const unsigned long word = *p;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800175 *p |= mask;
176 return word & mask;
177 } else {
178 hb->bits[index] |= mask;
179 }
180 } else {
181 hb->bits[index] &= ~mask;
182 }
183 return false;
184}
185
186/*
187 * Sets the bit corresponding to <obj>, and returns the previous value
188 * of that bit (as zero or non-zero). Does no range checking to see if
189 * <obj> is outside of the coverage of the bitmap.
190 *
191 * NOTE: casting this value to a bool is dangerous, because higher
192 * set bits will be lost.
193 */
194HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700195 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800196 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
197)
198{
199 return _heapBitmapModifyObjectBit(hb, obj, true, true);
200}
201
202/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800203 * Sets the bit corresponding to <obj>, and widens the range of seen
204 * pointers if necessary. Does no range checking.
205 */
206HB_INLINE_PROTO(
207 void
208 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
209)
210{
211 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
212}
213
214/*
215 * Clears the bit corresponding to <obj>. Does no range checking.
216 */
217HB_INLINE_PROTO(
218 void
219 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
220)
221{
222 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
223}
224
225/*
226 * Returns the current value of the bit corresponding to <obj>,
227 * as zero or non-zero. Does no range checking.
228 *
229 * NOTE: casting this value to a bool is dangerous, because higher
230 * set bits will be lost.
231 */
232HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700233 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800234 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
235)
236{
237 assert(dvmHeapBitmapCoversAddress(hb, obj));
238 assert(hb->bits != NULL);
239 assert((uintptr_t)obj >= hb->base);
240
241 if ((uintptr_t)obj <= hb->max) {
242 const uintptr_t offset = (uintptr_t)obj - hb->base;
243 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
244 } else {
245 return 0;
246 }
247}
248
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800249#undef HB_INLINE_PROTO
250
251#endif // _DALVIK_HEAP_BITMAP