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