blob: 5571b02c4827f9911db00a9f2ef08e4161532387 [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
107 * object pointers that correspond to places where the bitmaps differ.
108 * Call <callback> zero or more times with lists of these object pointers.
109 *
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
113 * the current XorWalk. <finger> will be set to ULONG_MAX when the
114 * end of the bitmap is reached.
115 */
Carl Shapiro006346e2010-07-29 20:39:50 -0700116void dvmHeapBitmapXorWalk(const HeapBitmap *hb1, const HeapBitmap *hb2,
117 BitmapCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800118
119/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800120 * Similar to dvmHeapBitmapXorWalk(), but visit the set bits
121 * in a single bitmap.
122 */
Carl Shapiro006346e2010-07-29 20:39:50 -0700123void dvmHeapBitmapWalk(const HeapBitmap *hb,
124 BitmapCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800125
126/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800127 * Return true iff <obj> is within the range of pointers that this
128 * bitmap could potentially cover, even if a bit has not been set
129 * for it.
130 */
131HB_INLINE_PROTO(
132 bool
133 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
134)
135{
136 assert(hb != NULL);
137
138 if (obj != NULL) {
139 const uintptr_t offset = (uintptr_t)obj - hb->base;
140 const size_t index = HB_OFFSET_TO_INDEX(offset);
141 return index < hb->bitsLen / sizeof(*hb->bits);
142 }
143 return false;
144}
145
146/*
147 * Internal function; do not call directly.
148 */
149HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700150 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800151 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
152 bool setBit, bool returnOld)
153)
154{
155 const uintptr_t offset = (uintptr_t)obj - hb->base;
156 const size_t index = HB_OFFSET_TO_INDEX(offset);
Carl Shapiro9cbece22010-07-12 17:45:51 -0700157 const unsigned long mask = HB_OFFSET_TO_MASK(offset);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800158
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800159 assert(hb->bits != NULL);
160 assert((uintptr_t)obj >= hb->base);
161 assert(index < hb->bitsLen / sizeof(*hb->bits));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800162
163 if (setBit) {
164 if ((uintptr_t)obj > hb->max) {
165 hb->max = (uintptr_t)obj;
166 }
167 if (returnOld) {
Carl Shapiro9cbece22010-07-12 17:45:51 -0700168 unsigned long *p = hb->bits + index;
169 const unsigned long word = *p;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800170 *p |= mask;
171 return word & mask;
172 } else {
173 hb->bits[index] |= mask;
174 }
175 } else {
176 hb->bits[index] &= ~mask;
177 }
178 return false;
179}
180
181/*
182 * Sets the bit corresponding to <obj>, and returns the previous value
183 * of that bit (as zero or non-zero). Does no range checking to see if
184 * <obj> is outside of the coverage of the bitmap.
185 *
186 * NOTE: casting this value to a bool is dangerous, because higher
187 * set bits will be lost.
188 */
189HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700190 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800191 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
192)
193{
194 return _heapBitmapModifyObjectBit(hb, obj, true, true);
195}
196
197/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800198 * Sets the bit corresponding to <obj>, and widens the range of seen
199 * pointers if necessary. Does no range checking.
200 */
201HB_INLINE_PROTO(
202 void
203 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
204)
205{
206 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
207}
208
209/*
210 * Clears the bit corresponding to <obj>. Does no range checking.
211 */
212HB_INLINE_PROTO(
213 void
214 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
215)
216{
217 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
218}
219
220/*
221 * Returns the current value of the bit corresponding to <obj>,
222 * as zero or non-zero. Does no range checking.
223 *
224 * NOTE: casting this value to a bool is dangerous, because higher
225 * set bits will be lost.
226 */
227HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700228 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800229 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
230)
231{
232 assert(dvmHeapBitmapCoversAddress(hb, obj));
233 assert(hb->bits != NULL);
234 assert((uintptr_t)obj >= hb->base);
235
236 if ((uintptr_t)obj <= hb->max) {
237 const uintptr_t offset = (uintptr_t)obj - hb->base;
238 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
239 } else {
240 return 0;
241 }
242}
243
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800244#undef HB_INLINE_PROTO
245
246#endif // _DALVIK_HEAP_BITMAP