| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | eff16fb | 2010-08-19 14:29:41 -0700 | [diff] [blame] | 19 | #include <limits.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 21 | #include "clz.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | |
| 23 | #define HB_OBJECT_ALIGNMENT 8 |
| Carl Shapiro | eff16fb | 2010-08-19 14:29:41 -0700 | [diff] [blame] | 24 | #define HB_BITS_PER_WORD (sizeof(unsigned long) * CHAR_BIT) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | |
| 26 | /* <offset> is the difference from .base to a pointer address. |
| 27 | * <index> is the index of .bits that contains the bit representing |
| 28 | * <offset>. |
| 29 | */ |
| 30 | #define HB_OFFSET_TO_INDEX(offset_) \ |
| 31 | ((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT / HB_BITS_PER_WORD) |
| 32 | #define HB_INDEX_TO_OFFSET(index_) \ |
| 33 | ((uintptr_t)(index_) * HB_OBJECT_ALIGNMENT * HB_BITS_PER_WORD) |
| 34 | |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 35 | #define HB_OFFSET_TO_BYTE_INDEX(offset_) \ |
| 36 | (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits)) |
| 37 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 38 | /* Pack the bits in backwards so they come out in address order |
| 39 | * when using CLZ. |
| 40 | */ |
| 41 | #define HB_OFFSET_TO_MASK(offset_) \ |
| 42 | (1 << \ |
| 43 | (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD))) |
| 44 | |
| 45 | /* Return the maximum offset (exclusive) that <hb> can represent. |
| 46 | */ |
| 47 | #define HB_MAX_OFFSET(hb_) \ |
| 48 | HB_INDEX_TO_OFFSET((hb_)->bitsLen / sizeof(*(hb_)->bits)) |
| 49 | |
| 50 | #define HB_INLINE_PROTO(p) \ |
| 51 | static inline p __attribute__((always_inline)); \ |
| 52 | static inline p |
| 53 | |
| Carl Shapiro | 7511ae1 | 2010-08-19 16:54:01 -0700 | [diff] [blame] | 54 | typedef struct { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 55 | /* The bitmap data, which points to an mmap()ed area of zeroed |
| 56 | * anonymous memory. |
| 57 | */ |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 58 | unsigned long *bits; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 60 | /* 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 62 | */ |
| 63 | size_t bitsLen; |
| 64 | |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 65 | /* 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 71 | /* 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; |
| Carl Shapiro | 7511ae1 | 2010-08-19 16:54:01 -0700 | [diff] [blame] | 81 | } HeapBitmap; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 82 | |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 83 | typedef void BitmapCallback(void *addr, void *arg); |
| 84 | typedef void BitmapSweepCallback(size_t numPtrs, void **ptrs, void *arg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 85 | |
| 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 | */ |
| 91 | bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize, |
| 92 | const char *name); |
| 93 | |
| 94 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 95 | * Clean up any resources associated with the bitmap. |
| 96 | */ |
| 97 | void dvmHeapBitmapDelete(HeapBitmap *hb); |
| 98 | |
| 99 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 100 | * Fill the bitmap with zeroes. Returns the bitmap's memory to |
| 101 | * the system as a side-effect. |
| 102 | */ |
| 103 | void dvmHeapBitmapZero(HeapBitmap *hb); |
| 104 | |
| 105 | /* |
| 106 | * Walk through the bitmaps in increasing address order, and find the |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 107 | * object pointers that correspond to garbage objects. Call |
| 108 | * <callback> zero or more times with lists of these object pointers. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 113 | * the current walk. |
| 114 | * |
| 115 | * The callback is permitted to increase the bitmap's max; the walk |
| 116 | * will use the updated max as a terminating condition, |
| 117 | * |
| 118 | * <finger> will be set to some value beyond the bitmap max when the |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 119 | * end of the bitmap is reached. |
| 120 | */ |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 121 | void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb, |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 122 | BitmapSweepCallback *callback, void *callbackArg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 123 | |
| 124 | /* |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 125 | * Similar to dvmHeapBitmapSweepWalk(), but visit the set bits |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 126 | * in a single bitmap. |
| 127 | */ |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 128 | HB_INLINE_PROTO( |
| 129 | void |
| 130 | dvmHeapBitmapWalk(const HeapBitmap *bitmap, |
| 131 | BitmapCallback *callback, void *arg) |
| 132 | ) |
| 133 | { |
| 134 | assert(bitmap != NULL); |
| 135 | assert(bitmap->bits != NULL); |
| 136 | assert(callback != NULL); |
| 137 | uintptr_t end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base); |
| 138 | uintptr_t i; |
| 139 | for (i = 0; i <= end; ++i) { |
| 140 | unsigned long word = bitmap->bits[i]; |
| 141 | if (UNLIKELY(word != 0)) { |
| 142 | unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1); |
| 143 | uintptr_t ptrBase = HB_INDEX_TO_OFFSET(i) + bitmap->base; |
| 144 | while (word != 0) { |
| 145 | const int shift = CLZ(word); |
| 146 | word &= ~(highBit >> shift); |
| 147 | void *addr = (void *)(ptrBase + shift * HB_OBJECT_ALIGNMENT); |
| 148 | (*callback)(addr, arg); |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 149 | } |
| Carl Shapiro | a634c85 | 2010-08-30 11:10:45 -0700 | [diff] [blame^] | 150 | end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base); |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 154 | |
| 155 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 156 | * Return true iff <obj> is within the range of pointers that this |
| 157 | * bitmap could potentially cover, even if a bit has not been set |
| 158 | * for it. |
| 159 | */ |
| 160 | HB_INLINE_PROTO( |
| 161 | bool |
| 162 | dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj) |
| 163 | ) |
| 164 | { |
| 165 | assert(hb != NULL); |
| 166 | |
| 167 | if (obj != NULL) { |
| 168 | const uintptr_t offset = (uintptr_t)obj - hb->base; |
| 169 | const size_t index = HB_OFFSET_TO_INDEX(offset); |
| 170 | return index < hb->bitsLen / sizeof(*hb->bits); |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Internal function; do not call directly. |
| 177 | */ |
| 178 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 179 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 180 | _heapBitmapModifyObjectBit(HeapBitmap *hb, const void *obj, |
| 181 | bool setBit, bool returnOld) |
| 182 | ) |
| 183 | { |
| 184 | const uintptr_t offset = (uintptr_t)obj - hb->base; |
| 185 | const size_t index = HB_OFFSET_TO_INDEX(offset); |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 186 | const unsigned long mask = HB_OFFSET_TO_MASK(offset); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 187 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 188 | assert(hb->bits != NULL); |
| 189 | assert((uintptr_t)obj >= hb->base); |
| 190 | assert(index < hb->bitsLen / sizeof(*hb->bits)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 191 | |
| 192 | if (setBit) { |
| 193 | if ((uintptr_t)obj > hb->max) { |
| 194 | hb->max = (uintptr_t)obj; |
| 195 | } |
| 196 | if (returnOld) { |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 197 | unsigned long *p = hb->bits + index; |
| 198 | const unsigned long word = *p; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 199 | *p |= mask; |
| 200 | return word & mask; |
| 201 | } else { |
| 202 | hb->bits[index] |= mask; |
| 203 | } |
| 204 | } else { |
| 205 | hb->bits[index] &= ~mask; |
| 206 | } |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Sets the bit corresponding to <obj>, and returns the previous value |
| 212 | * of that bit (as zero or non-zero). Does no range checking to see if |
| 213 | * <obj> is outside of the coverage of the bitmap. |
| 214 | * |
| 215 | * NOTE: casting this value to a bool is dangerous, because higher |
| 216 | * set bits will be lost. |
| 217 | */ |
| 218 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 219 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 220 | dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj) |
| 221 | ) |
| 222 | { |
| 223 | return _heapBitmapModifyObjectBit(hb, obj, true, true); |
| 224 | } |
| 225 | |
| 226 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 227 | * Sets the bit corresponding to <obj>, and widens the range of seen |
| 228 | * pointers if necessary. Does no range checking. |
| 229 | */ |
| 230 | HB_INLINE_PROTO( |
| 231 | void |
| 232 | dvmHeapBitmapSetObjectBit(HeapBitmap *hb, const void *obj) |
| 233 | ) |
| 234 | { |
| 235 | (void)_heapBitmapModifyObjectBit(hb, obj, true, false); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Clears the bit corresponding to <obj>. Does no range checking. |
| 240 | */ |
| 241 | HB_INLINE_PROTO( |
| 242 | void |
| 243 | dvmHeapBitmapClearObjectBit(HeapBitmap *hb, const void *obj) |
| 244 | ) |
| 245 | { |
| 246 | (void)_heapBitmapModifyObjectBit(hb, obj, false, false); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Returns the current value of the bit corresponding to <obj>, |
| 251 | * as zero or non-zero. Does no range checking. |
| 252 | * |
| 253 | * NOTE: casting this value to a bool is dangerous, because higher |
| 254 | * set bits will be lost. |
| 255 | */ |
| 256 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 257 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 258 | dvmHeapBitmapIsObjectBitSet(const HeapBitmap *hb, const void *obj) |
| 259 | ) |
| 260 | { |
| 261 | assert(dvmHeapBitmapCoversAddress(hb, obj)); |
| 262 | assert(hb->bits != NULL); |
| 263 | assert((uintptr_t)obj >= hb->base); |
| 264 | |
| 265 | if ((uintptr_t)obj <= hb->max) { |
| 266 | const uintptr_t offset = (uintptr_t)obj - hb->base; |
| 267 | return hb->bits[HB_OFFSET_TO_INDEX(offset)] & HB_OFFSET_TO_MASK(offset); |
| 268 | } else { |
| 269 | return 0; |
| 270 | } |
| 271 | } |
| 272 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 273 | #undef HB_INLINE_PROTO |
| 274 | |
| 275 | #endif // _DALVIK_HEAP_BITMAP |