| 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> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 21 | |
| 22 | #define HB_OBJECT_ALIGNMENT 8 |
| Carl Shapiro | eff16fb | 2010-08-19 14:29:41 -0700 | [diff] [blame] | 23 | #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] | 24 | |
| 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 Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 34 | #define HB_OFFSET_TO_BYTE_INDEX(offset_) \ |
| 35 | (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits)) |
| 36 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | /* 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 Shapiro | 7511ae1 | 2010-08-19 16:54:01 -0700 | [diff] [blame^] | 53 | typedef struct { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 54 | /* The bitmap data, which points to an mmap()ed area of zeroed |
| 55 | * anonymous memory. |
| 56 | */ |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 57 | unsigned long *bits; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 58 | |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 59 | /* 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 61 | */ |
| 62 | size_t bitsLen; |
| 63 | |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 64 | /* 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 70 | /* 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 Shapiro | 7511ae1 | 2010-08-19 16:54:01 -0700 | [diff] [blame^] | 80 | } HeapBitmap; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 81 | |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame] | 82 | typedef void BitmapCallback(size_t numPtrs, void **ptrs, |
| 83 | const void *finger, void *arg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 84 | |
| 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 | */ |
| 90 | bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize, |
| 91 | const char *name); |
| 92 | |
| 93 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 94 | * Clean up any resources associated with the bitmap. |
| 95 | */ |
| 96 | void dvmHeapBitmapDelete(HeapBitmap *hb); |
| 97 | |
| 98 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 99 | * Fill the bitmap with zeroes. Returns the bitmap's memory to |
| 100 | * the system as a side-effect. |
| 101 | */ |
| 102 | void dvmHeapBitmapZero(HeapBitmap *hb); |
| 103 | |
| 104 | /* |
| 105 | * Walk through the bitmaps in increasing address order, and find the |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 106 | * object pointers that correspond to garbage objects. Call |
| 107 | * <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] | 108 | * |
| 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 Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 112 | * 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 118 | * end of the bitmap is reached. |
| 119 | */ |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 120 | void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb, |
| 121 | BitmapCallback *callback, void *callbackArg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | |
| 123 | /* |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 124 | * Similar to dvmHeapBitmapSweepWalk(), but visit the set bits |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | * in a single bitmap. |
| 126 | */ |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame] | 127 | void dvmHeapBitmapWalk(const HeapBitmap *hb, |
| 128 | BitmapCallback *callback, void *callbackArg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 129 | |
| 130 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 131 | * 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 | */ |
| 135 | HB_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 | */ |
| 153 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 154 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 155 | _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 Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 161 | const unsigned long mask = HB_OFFSET_TO_MASK(offset); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 162 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 163 | assert(hb->bits != NULL); |
| 164 | assert((uintptr_t)obj >= hb->base); |
| 165 | assert(index < hb->bitsLen / sizeof(*hb->bits)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 166 | |
| 167 | if (setBit) { |
| 168 | if ((uintptr_t)obj > hb->max) { |
| 169 | hb->max = (uintptr_t)obj; |
| 170 | } |
| 171 | if (returnOld) { |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 172 | unsigned long *p = hb->bits + index; |
| 173 | const unsigned long word = *p; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 174 | *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 | */ |
| 193 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 194 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 195 | dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj) |
| 196 | ) |
| 197 | { |
| 198 | return _heapBitmapModifyObjectBit(hb, obj, true, true); |
| 199 | } |
| 200 | |
| 201 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 202 | * Sets the bit corresponding to <obj>, and widens the range of seen |
| 203 | * pointers if necessary. Does no range checking. |
| 204 | */ |
| 205 | HB_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 | */ |
| 216 | HB_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 | */ |
| 231 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 232 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 233 | 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 248 | #undef HB_INLINE_PROTO |
| 249 | |
| 250 | #endif // _DALVIK_HEAP_BITMAP |