| 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 | |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame^] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 26 | #define HB_OBJECT_ALIGNMENT 8 |
| Carl Shapiro | eff16fb | 2010-08-19 14:29:41 -0700 | [diff] [blame] | 27 | #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] | 28 | |
| 29 | /* <offset> is the difference from .base to a pointer address. |
| 30 | * <index> is the index of .bits that contains the bit representing |
| 31 | * <offset>. |
| 32 | */ |
| 33 | #define HB_OFFSET_TO_INDEX(offset_) \ |
| 34 | ((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT / HB_BITS_PER_WORD) |
| 35 | #define HB_INDEX_TO_OFFSET(index_) \ |
| 36 | ((uintptr_t)(index_) * HB_OBJECT_ALIGNMENT * HB_BITS_PER_WORD) |
| 37 | |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 38 | #define HB_OFFSET_TO_BYTE_INDEX(offset_) \ |
| 39 | (HB_OFFSET_TO_INDEX(offset_) * sizeof(*((HeapBitmap *)0)->bits)) |
| 40 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 41 | /* Pack the bits in backwards so they come out in address order |
| 42 | * when using CLZ. |
| 43 | */ |
| 44 | #define HB_OFFSET_TO_MASK(offset_) \ |
| 45 | (1 << \ |
| 46 | (31-(((uintptr_t)(offset_) / HB_OBJECT_ALIGNMENT) % HB_BITS_PER_WORD))) |
| 47 | |
| Carl Shapiro | 7511ae1 | 2010-08-19 16:54:01 -0700 | [diff] [blame] | 48 | typedef struct { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 49 | /* The bitmap data, which points to an mmap()ed area of zeroed |
| 50 | * anonymous memory. |
| 51 | */ |
| Carl Shapiro | 9cbece2 | 2010-07-12 17:45:51 -0700 | [diff] [blame] | 52 | unsigned long *bits; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 53 | |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 54 | /* The size of the used memory pointed to by bits, in bytes. This |
| 55 | * value changes when the bitmap is shrunk. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 56 | */ |
| 57 | size_t bitsLen; |
| 58 | |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 59 | /* The real size of the memory pointed to by bits. This is the |
| 60 | * number of bytes we requested from the allocator and does not |
| 61 | * change. |
| 62 | */ |
| 63 | size_t allocLen; |
| 64 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 65 | /* The base address, which corresponds to the first bit in |
| 66 | * the bitmap. |
| 67 | */ |
| 68 | uintptr_t base; |
| 69 | |
| 70 | /* The highest pointer value ever returned by an allocation |
| 71 | * from this heap. I.e., the highest address that may correspond |
| 72 | * to a set bit. If there are no bits set, (max < base). |
| 73 | */ |
| 74 | uintptr_t max; |
| Carl Shapiro | 7511ae1 | 2010-08-19 16:54:01 -0700 | [diff] [blame] | 75 | } HeapBitmap; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 76 | |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 77 | /* |
| 78 | * Callback types used by the walking routines. |
| 79 | */ |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 80 | typedef void BitmapCallback(void *addr, void *arg); |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 81 | typedef void BitmapScanCallback(void *addr, void *finger, void *arg); |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 82 | 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] | 83 | |
| 84 | /* |
| 85 | * Initialize a HeapBitmap so that it points to a bitmap large |
| 86 | * enough to cover a heap at <base> of <maxSize> bytes, where |
| 87 | * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned. |
| 88 | */ |
| 89 | bool dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize, |
| 90 | const char *name); |
| 91 | |
| 92 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 93 | * Clean up any resources associated with the bitmap. |
| 94 | */ |
| 95 | void dvmHeapBitmapDelete(HeapBitmap *hb); |
| 96 | |
| 97 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 98 | * Fill the bitmap with zeroes. Returns the bitmap's memory to |
| 99 | * the system as a side-effect. |
| 100 | */ |
| 101 | void dvmHeapBitmapZero(HeapBitmap *hb); |
| 102 | |
| 103 | /* |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 104 | * Returns true if the address range of the bitmap covers the object |
| 105 | * address. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 106 | */ |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 107 | bool dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj); |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 110 | * Applies the callback function to each set address in the bitmap. |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 111 | */ |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 112 | void dvmHeapBitmapWalk(const HeapBitmap *bitmap, |
| 113 | BitmapCallback *callback, void *callbackArg); |
| 114 | |
| 115 | /* |
| 116 | * Like dvmHeapBitmapWalk but takes a callback function with a finger |
| 117 | * address. |
| 118 | */ |
| 119 | void dvmHeapBitmapScanWalk(HeapBitmap *bitmap, |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 120 | uintptr_t base, uintptr_t max, |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 121 | BitmapScanCallback *callback, void *arg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | |
| 123 | /* |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 124 | * Walk through the bitmaps in increasing address order, and find the |
| 125 | * object pointers that correspond to garbage objects. Call |
| 126 | * <callback> zero or more times with lists of these object pointers. |
| 127 | * |
| Carl Shapiro | 879011d | 2010-12-02 16:41:28 -0800 | [diff] [blame] | 128 | * The callback is not permitted to increase the max of either bitmap. |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 129 | */ |
| 130 | void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb, |
| Carl Shapiro | 2126071 | 2010-12-09 15:24:11 -0800 | [diff] [blame] | 131 | uintptr_t base, uintptr_t max, |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 132 | BitmapSweepCallback *callback, void *callbackArg); |
| 133 | |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame^] | 134 | #ifdef __cplusplus |
| 135 | } |
| 136 | #endif |
| 137 | |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 138 | #endif /* _DALVIK_HEAP_BITMAP */ |