blob: e60d06f3f9104523a447ed4d1377462ab899b533 [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
53
Barry Hayes6e5cf602010-06-22 12:32:59 -070054struct HeapBitmap {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080055 /* The bitmap data, which points to an mmap()ed area of zeroed
56 * anonymous memory.
57 */
Carl Shapiro9cbece22010-07-12 17:45:51 -070058 unsigned long *bits;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080059
Carl Shapiro8d724802010-02-14 18:40:47 -080060 /* The size of the used memory pointed to by bits, in bytes. This
61 * value changes when the bitmap is shrunk.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080062 */
63 size_t bitsLen;
64
Carl Shapiro8d724802010-02-14 18:40:47 -080065 /* The real size of the memory pointed to by bits. This is the
66 * number of bytes we requested from the allocator and does not
67 * change.
68 */
69 size_t allocLen;
70
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080071 /* The base address, which corresponds to the first bit in
72 * the bitmap.
73 */
74 uintptr_t base;
75
76 /* The highest pointer value ever returned by an allocation
77 * from this heap. I.e., the highest address that may correspond
78 * to a set bit. If there are no bits set, (max < base).
79 */
80 uintptr_t max;
Barry Hayes6e5cf602010-06-22 12:32:59 -070081};
82typedef struct HeapBitmap HeapBitmap;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080083
Carl Shapiro006346e2010-07-29 20:39:50 -070084typedef void BitmapCallback(size_t numPtrs, void **ptrs,
85 const void *finger, void *arg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080086
87/*
88 * Initialize a HeapBitmap so that it points to a bitmap large
89 * enough to cover a heap at <base> of <maxSize> bytes, where
90 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
91 */
92bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
93 const char *name);
94
95/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080096 * Clean up any resources associated with the bitmap.
97 */
98void dvmHeapBitmapDelete(HeapBitmap *hb);
99
100/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800101 * Fill the bitmap with zeroes. Returns the bitmap's memory to
102 * the system as a side-effect.
103 */
104void dvmHeapBitmapZero(HeapBitmap *hb);
105
106/*
107 * Walk through the bitmaps in increasing address order, and find the
Barry Hayesacdea992010-07-21 12:03:02 -0700108 * object pointers that correspond to garbage objects. Call
109 * <callback> zero or more times with lists of these object pointers.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800110 *
111 * The <finger> argument to the callback indicates the next-highest
112 * address that hasn't been visited yet; setting bits for objects whose
113 * addresses are less than <finger> are not guaranteed to be seen by
Barry Hayesacdea992010-07-21 12:03:02 -0700114 * the current walk.
115 *
116 * The callback is permitted to increase the bitmap's max; the walk
117 * will use the updated max as a terminating condition,
118 *
119 * <finger> will be set to some value beyond the bitmap max when the
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800120 * end of the bitmap is reached.
121 */
Barry Hayesacdea992010-07-21 12:03:02 -0700122void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb,
123 BitmapCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800124
125/*
Barry Hayesacdea992010-07-21 12:03:02 -0700126 * Similar to dvmHeapBitmapSweepWalk(), but visit the set bits
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800127 * in a single bitmap.
128 */
Carl Shapiro006346e2010-07-29 20:39:50 -0700129void dvmHeapBitmapWalk(const HeapBitmap *hb,
130 BitmapCallback *callback, void *callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800131
132/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800133 * Return true iff <obj> is within the range of pointers that this
134 * bitmap could potentially cover, even if a bit has not been set
135 * for it.
136 */
137HB_INLINE_PROTO(
138 bool
139 dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj)
140)
141{
142 assert(hb != NULL);
143
144 if (obj != NULL) {
145 const uintptr_t offset = (uintptr_t)obj - hb->base;
146 const size_t index = HB_OFFSET_TO_INDEX(offset);
147 return index < hb->bitsLen / sizeof(*hb->bits);
148 }
149 return false;
150}
151
152/*
153 * Internal function; do not call directly.
154 */
155HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700156 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800157 _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj,
158 bool setBit, bool returnOld)
159)
160{
161 const uintptr_t offset = (uintptr_t)obj - hb->base;
162 const size_t index = HB_OFFSET_TO_INDEX(offset);
Carl Shapiro9cbece22010-07-12 17:45:51 -0700163 const unsigned long mask = HB_OFFSET_TO_MASK(offset);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800164
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800165 assert(hb->bits != NULL);
166 assert((uintptr_t)obj >= hb->base);
167 assert(index < hb->bitsLen / sizeof(*hb->bits));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800168
169 if (setBit) {
170 if ((uintptr_t)obj > hb->max) {
171 hb->max = (uintptr_t)obj;
172 }
173 if (returnOld) {
Carl Shapiro9cbece22010-07-12 17:45:51 -0700174 unsigned long *p = hb->bits + index;
175 const unsigned long word = *p;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800176 *p |= mask;
177 return word & mask;
178 } else {
179 hb->bits[index] |= mask;
180 }
181 } else {
182 hb->bits[index] &= ~mask;
183 }
184 return false;
185}
186
187/*
188 * Sets the bit corresponding to <obj>, and returns the previous value
189 * of that bit (as zero or non-zero). Does no range checking to see if
190 * <obj> is outside of the coverage of the bitmap.
191 *
192 * NOTE: casting this value to a bool is dangerous, because higher
193 * set bits will be lost.
194 */
195HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700196 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800197 dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj)
198)
199{
200 return _heapBitmapModifyObjectBit(hb, obj, true, true);
201}
202
203/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800204 * Sets the bit corresponding to <obj>, and widens the range of seen
205 * pointers if necessary. Does no range checking.
206 */
207HB_INLINE_PROTO(
208 void
209 dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj)
210)
211{
212 (void)_heapBitmapModifyObjectBit(hb, obj, true, false);
213}
214
215/*
216 * Clears the bit corresponding to <obj>. Does no range checking.
217 */
218HB_INLINE_PROTO(
219 void
220 dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj)
221)
222{
223 (void)_heapBitmapModifyObjectBit(hb, obj, false, false);
224}
225
226/*
227 * Returns the current value of the bit corresponding to <obj>,
228 * as zero or non-zero. Does no range checking.
229 *
230 * NOTE: casting this value to a bool is dangerous, because higher
231 * set bits will be lost.
232 */
233HB_INLINE_PROTO(
Carl Shapiro9cbece22010-07-12 17:45:51 -0700234 unsigned long
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800235 dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj)
236)
237{
238 assert(dvmHeapBitmapCoversAddress(hb, obj));
239 assert(hb->bits != NULL);
240 assert((uintptr_t)obj >= hb->base);
241
242 if ((uintptr_t)obj <= hb->max) {
243 const uintptr_t offset = (uintptr_t)obj - hb->base;
244 return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset);
245 } else {
246 return 0;
247 }
248}
249
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800250#undef HB_INLINE_PROTO
251
252#endif // _DALVIK_HEAP_BITMAP