| 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 | |
| 53 | |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 54 | struct HeapBitmap { |
| 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; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 81 | }; |
| 82 | typedef struct HeapBitmap HeapBitmap; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 83 | |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame] | 84 | typedef void BitmapCallback(size_t numPtrs, void **ptrs, |
| 85 | const void *finger, void *arg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 86 | |
| 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 | */ |
| 92 | bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize, |
| 93 | const char *name); |
| 94 | |
| 95 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 96 | * Clean up any resources associated with the bitmap. |
| 97 | */ |
| 98 | void dvmHeapBitmapDelete(HeapBitmap *hb); |
| 99 | |
| 100 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 101 | * Fill the bitmap with zeroes. Returns the bitmap's memory to |
| 102 | * the system as a side-effect. |
| 103 | */ |
| 104 | void dvmHeapBitmapZero(HeapBitmap *hb); |
| 105 | |
| 106 | /* |
| 107 | * Walk through the bitmaps in increasing address order, and find the |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 108 | * object pointers that correspond to garbage objects. Call |
| 109 | * <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] | 110 | * |
| 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 Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 114 | * 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 120 | * end of the bitmap is reached. |
| 121 | */ |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 122 | void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb, |
| 123 | BitmapCallback *callback, void *callbackArg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 124 | |
| 125 | /* |
| Barry Hayes | acdea99 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 126 | * Similar to dvmHeapBitmapSweepWalk(), but visit the set bits |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | * in a single bitmap. |
| 128 | */ |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame] | 129 | void dvmHeapBitmapWalk(const HeapBitmap *hb, |
| 130 | BitmapCallback *callback, void *callbackArg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 131 | |
| 132 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 133 | * 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 | */ |
| 137 | HB_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 | */ |
| 155 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 156 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 157 | _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 Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 163 | const unsigned long mask = HB_OFFSET_TO_MASK(offset); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 165 | assert(hb->bits != NULL); |
| 166 | assert((uintptr_t)obj >= hb->base); |
| 167 | assert(index < hb->bitsLen / sizeof(*hb->bits)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 168 | |
| 169 | if (setBit) { |
| 170 | if ((uintptr_t)obj > hb->max) { |
| 171 | hb->max = (uintptr_t)obj; |
| 172 | } |
| 173 | if (returnOld) { |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 174 | unsigned long *p = hb->bits + index; |
| 175 | const unsigned long word = *p; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 176 | *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 | */ |
| 195 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 196 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 197 | dvmHeapBitmapSetAndReturnObjectBit(HeapBitmap *hb, const void *obj) |
| 198 | ) |
| 199 | { |
| 200 | return _heapBitmapModifyObjectBit(hb, obj, true, true); |
| 201 | } |
| 202 | |
| 203 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 204 | * Sets the bit corresponding to <obj>, and widens the range of seen |
| 205 | * pointers if necessary. Does no range checking. |
| 206 | */ |
| 207 | HB_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 | */ |
| 218 | HB_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 | */ |
| 233 | HB_INLINE_PROTO( |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 234 | unsigned long |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 235 | 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 250 | #undef HB_INLINE_PROTO |
| 251 | |
| 252 | #endif // _DALVIK_HEAP_BITMAP |