blob: fd3585829251e9515d6a0b9da3360b1fb37e2bd2 [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
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
53typedef struct {
54 /* The bitmap data, which points to an mmap()ed area of zeroed
55 * anonymous memory.
56 */
57 unsigned long int *bits;
58
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;
80} HeapBitmap;
81
82
83/*
84 * Initialize a HeapBitmap so that it points to a bitmap large
85 * enough to cover a heap at <base> of <maxSize> bytes, where
86 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
87 */
88bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
89 const char *name);
90
91/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080092 * Clean up any resources associated with the bitmap.
93 */
94void dvmHeapBitmapDelete(HeapBitmap *hb);
95
96/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080097 * Fill the bitmap with zeroes. Returns the bitmap's memory to
98 * the system as a side-effect.
99 */
100void dvmHeapBitmapZero(HeapBitmap *hb);
101
102/*
103 * Walk through the bitmaps in increasing address order, and find the
104 * object pointers that correspond to places where the bitmaps differ.
105 * Call <callback> zero or more times with lists of these object pointers.
106 *
107 * The <finger> argument to the callback indicates the next-highest
108 * address that hasn't been visited yet; setting bits for objects whose
109 * addresses are less than <finger> are not guaranteed to be seen by
110 * the current XorWalk. <finger> will be set to ULONG_MAX when the
111 * end of the bitmap is reached.
112 */
113bool dvmHeapBitmapXorWalk(const HeapBitmap *hb1, const HeapBitmap *hb2,
114 bool (*callback)(size_t numPtrs, void **ptrs,
115 const void *finger, void *arg),
116 void *callbackArg);
117
118/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800119 * Similar to dvmHeapBitmapXorWalk(), but visit the set bits
120 * in a single bitmap.
121 */
122bool dvmHeapBitmapWalk(const HeapBitmap *hb,
123 bool (*callback)(size_t numPtrs, void **ptrs,
124 const void *finger, void *arg),
125 void *callbackArg);
126
127/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800128 * Return true iff <obj> is within the range of pointers that this
129 * bitmap could potentially cover, even if a bit has not been set
130 * for it.
131 */
132HB_INLINE_PROTO(
133 bool
134 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
135)
136{
137 assert(hb != NULL);
138
139 if (obj != NULL) {
140 const uintptr_t offset = (uintptr_t)obj - hb->base;
141 const size_t index = HB_OFFSET_TO_INDEX(offset);
142 return index < hb->bitsLen / sizeof(*hb->bits);
143 }
144 return false;
145}
146
147/*
148 * Internal function; do not call directly.
149 */
150HB_INLINE_PROTO(
151 unsigned long int
152 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
153 bool setBit, bool returnOld)
154)
155{
156 const uintptr_t offset = (uintptr_t)obj - hb->base;
157 const size_t index = HB_OFFSET_TO_INDEX(offset);
158 const unsigned long int mask = HB_OFFSET_TO_MASK(offset);
159
160#ifndef NDEBUG
161 assert(hb->bits != NULL);
162 assert((uintptr_t)obj >= hb->base);
163 assert(index < hb->bitsLen / sizeof(*hb->bits));
164#endif
165
166 if (setBit) {
167 if ((uintptr_t)obj > hb->max) {
168 hb->max = (uintptr_t)obj;
169 }
170 if (returnOld) {
171 unsigned long int *p = hb->bits + index;
172 const unsigned long int word = *p;
173 *p |= mask;
174 return word & mask;
175 } else {
176 hb->bits[index] |= mask;
177 }
178 } else {
179 hb->bits[index] &= ~mask;
180 }
181 return false;
182}
183
184/*
185 * Sets the bit corresponding to <obj>, and returns the previous value
186 * of that bit (as zero or non-zero). Does no range checking to see if
187 * <obj> is outside of the coverage of the bitmap.
188 *
189 * NOTE: casting this value to a bool is dangerous, because higher
190 * set bits will be lost.
191 */
192HB_INLINE_PROTO(
193 unsigned long int
194 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
195)
196{
197 return _heapBitmapModifyObjectBit(hb, obj, true, true);
198}
199
200/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800201 * Sets the bit corresponding to <obj>, and widens the range of seen
202 * pointers if necessary. Does no range checking.
203 */
204HB_INLINE_PROTO(
205 void
206 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
207)
208{
209 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
210}
211
212/*
213 * Clears the bit corresponding to <obj>. Does no range checking.
214 */
215HB_INLINE_PROTO(
216 void
217 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
218)
219{
220 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
221}
222
223/*
224 * Returns the current value of the bit corresponding to <obj>,
225 * as zero or non-zero. Does no range checking.
226 *
227 * NOTE: casting this value to a bool is dangerous, because higher
228 * set bits will be lost.
229 */
230HB_INLINE_PROTO(
231 unsigned long int
232 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
233)
234{
235 assert(dvmHeapBitmapCoversAddress(hb, obj));
236 assert(hb->bits != NULL);
237 assert((uintptr_t)obj >= hb->base);
238
239 if ((uintptr_t)obj <= hb->max) {
240 const uintptr_t offset = (uintptr_t)obj - hb->base;
241 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
242 } else {
243 return 0;
244 }
245}
246
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800247#undef HB_INLINE_PROTO
248
249#endif // _DALVIK_HEAP_BITMAP