blob: 5810d9a68255adf5a0b11d15ff334437bd1be029 [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>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080021
22#define HB_OBJECT_ALIGNMENT 8
Carl Shapiroeff16fb2010-08-19 14:29:41 -070023#define HB_BITS_PER_WORD (sizeof(unsigned long) * CHAR_BIT)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080024
25/* <offset> is the difference from .base to a pointer address.
26 * <index> is the index of .bits that contains the bit representing
27 * <offset>.
28 */
29#define HB_OFFSET_TO_INDEX(offset_) \
30 ((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT / HB_BITS_PER_WORD)
31#define HB_INDEX_TO_OFFSET(index_) \
32 ((uintptr_t)(index_) * HB_OBJECT_ALIGNMENT * HB_BITS_PER_WORD)
33
Carl Shapirod25566d2010-03-11 20:39:47 -080034#define HB_OFFSET_TO_BYTE_INDEX(offset_) \
35 (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits))
36
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080037/* Pack the bits in backwards so they come out in address order
38 * when using CLZ.
39 */
40#define HB_OFFSET_TO_MASK(offset_) \
41 (1 << \
42 (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD)))
43
44/* Return the maximum offset (exclusive) that <hb> can represent.
45 */
46#define HB_MAX_OFFSET(hb_) \
47 HB_INDEX_TO_OFFSET((hb_)->bitsLen / sizeof(*(hb_)->bits))
48
49#define HB_INLINE_PROTO(p) \
50 static inline p __attribute__((always_inline)); \
51 static inline p
52
Carl Shapiro7511ae12010-08-19 16:54:01 -070053typedef struct {
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;
Carl Shapiro7511ae12010-08-19 16:54:01 -070080} HeapBitmap;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080081
Carl Shapiro006346e2010-07-29 20:39:50 -070082typedef void BitmapCallback(size_t numPtrs, void **ptrs,
83 const void *finger, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080084
85/*
86 * Initialize a HeapBitmap so that it points to a bitmap large
87 * enough to cover a heap at <base> of <maxSize> bytes, where
88 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
89 */
90bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
91 const char *name);
92
93/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080094 * Clean up any resources associated with the bitmap.
95 */
96void dvmHeapBitmapDelete(HeapBitmap *hb);
97
98/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080099 * Fill the bitmap with zeroes. Returns the bitmap's memory to
100 * the system as a side-effect.
101 */
102void dvmHeapBitmapZero(HeapBitmap *hb);
103
104/*
105 * Walk through the bitmaps in increasing address order, and find the
Barry Hayesacdea992010-07-21 12:03:02 -0700106 * object pointers that correspond to garbage objects. Call
107 * <callback> zero or more times with lists of these object pointers.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800108 *
109 * The <finger> argument to the callback indicates the next-highest
110 * address that hasn't been visited yet; setting bits for objects whose
111 * addresses are less than <finger> are not guaranteed to be seen by
Barry Hayesacdea992010-07-21 12:03:02 -0700112 * the current walk.
113 *
114 * The callback is permitted to increase the bitmap's max; the walk
115 * will use the updated max as a terminating condition,
116 *
117 * <finger> will be set to some value beyond the bitmap max when the
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800118 * end of the bitmap is reached.
119 */
Barry Hayesacdea992010-07-21 12:03:02 -0700120void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb,
121 BitmapCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800122
123/*
Barry Hayesacdea992010-07-21 12:03:02 -0700124 * Similar to dvmHeapBitmapSweepWalk(), but visit the set bits
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800125 * in a single bitmap.
126 */
Carl Shapiro006346e2010-07-29 20:39:50 -0700127void dvmHeapBitmapWalk(const HeapBitmap *hb,
128 BitmapCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800129
130/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800131 * Return true iff <obj> is within the range of pointers that this
132 * bitmap could potentially cover, even if a bit has not been set
133 * for it.
134 */
135HB_INLINE_PROTO(
136 bool
137 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
138)
139{
140 assert(hb != NULL);
141
142 if (obj != NULL) {
143 const uintptr_t offset = (uintptr_t)obj - hb->base;
144 const size_t index = HB_OFFSET_TO_INDEX(offset);
145 return index < hb->bitsLen / sizeof(*hb->bits);
146 }
147 return false;
148}
149
150/*
151 * Internal function; do not call directly.
152 */
153HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700154 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800155 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
156 bool setBit, bool returnOld)
157)
158{
159 const uintptr_t offset = (uintptr_t)obj - hb->base;
160 const size_t index = HB_OFFSET_TO_INDEX(offset);
Carl Shapiro9cbece22010-07-12 17:45:51 -0700161 const unsigned long mask = HB_OFFSET_TO_MASK(offset);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800162
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800163 assert(hb->bits != NULL);
164 assert((uintptr_t)obj >= hb->base);
165 assert(index < hb->bitsLen / sizeof(*hb->bits));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800166
167 if (setBit) {
168 if ((uintptr_t)obj > hb->max) {
169 hb->max = (uintptr_t)obj;
170 }
171 if (returnOld) {
Carl Shapiro9cbece22010-07-12 17:45:51 -0700172 unsigned long *p = hb->bits + index;
173 const unsigned long word = *p;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800174 *p |= mask;
175 return word & mask;
176 } else {
177 hb->bits[index] |= mask;
178 }
179 } else {
180 hb->bits[index] &= ~mask;
181 }
182 return false;
183}
184
185/*
186 * Sets the bit corresponding to <obj>, and returns the previous value
187 * of that bit (as zero or non-zero). Does no range checking to see if
188 * <obj> is outside of the coverage of the bitmap.
189 *
190 * NOTE: casting this value to a bool is dangerous, because higher
191 * set bits will be lost.
192 */
193HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700194 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800195 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
196)
197{
198 return _heapBitmapModifyObjectBit(hb, obj, true, true);
199}
200
201/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800202 * Sets the bit corresponding to <obj>, and widens the range of seen
203 * pointers if necessary. Does no range checking.
204 */
205HB_INLINE_PROTO(
206 void
207 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
208)
209{
210 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
211}
212
213/*
214 * Clears the bit corresponding to <obj>. Does no range checking.
215 */
216HB_INLINE_PROTO(
217 void
218 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
219)
220{
221 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
222}
223
224/*
225 * Returns the current value of the bit corresponding to <obj>,
226 * as zero or non-zero. Does no range checking.
227 *
228 * NOTE: casting this value to a bool is dangerous, because higher
229 * set bits will be lost.
230 */
231HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700232 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800233 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
234)
235{
236 assert(dvmHeapBitmapCoversAddress(hb, obj));
237 assert(hb->bits != NULL);
238 assert((uintptr_t)obj >= hb->base);
239
240 if ((uintptr_t)obj <= hb->max) {
241 const uintptr_t offset = (uintptr_t)obj - hb->base;
242 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
243 } else {
244 return 0;
245 }
246}
247
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800248#undef HB_INLINE_PROTO
249
250#endif // _DALVIK_HEAP_BITMAP