| 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 | |
| 17 | #include "Dalvik.h" |
| 18 | #include "HeapBitmap.h" |
| 19 | #include "clz.h" |
| Carl Shapiro | f1461cb | 2010-07-29 19:51:53 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> /* for PROT_* */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 21 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | /* |
| 23 | * Initialize a HeapBitmap so that it points to a bitmap large |
| 24 | * enough to cover a heap at <base> of <maxSize> bytes, where |
| 25 | * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned. |
| 26 | */ |
| 27 | bool |
| 28 | dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize, |
| 29 | const char *name) |
| 30 | { |
| 31 | void *bits; |
| 32 | size_t bitsLen; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | |
| 34 | assert(hb != NULL); |
| Carl Shapiro | 3db1ad3 | 2010-07-12 16:09:54 -0700 | [diff] [blame] | 35 | assert(name != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 36 | bitsLen = HB_OFFSET_TO_INDEX(maxSize) * sizeof(*hb->bits); |
| Carl Shapiro | 3db1ad3 | 2010-07-12 16:09:54 -0700 | [diff] [blame] | 37 | bits = dvmAllocRegion(bitsLen, PROT_READ | PROT_WRITE, name); |
| 38 | if (bits == NULL) { |
| 39 | LOGE("Could not mmap %zd-byte ashmem region '%s'", bitsLen, name); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 40 | return false; |
| 41 | } |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 42 | hb->bits = (unsigned long *)bits; |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 43 | hb->bitsLen = hb->allocLen = bitsLen; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | hb->base = (uintptr_t)base; |
| 45 | hb->max = hb->base - 1; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
| 49 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 50 | * Clean up any resources associated with the bitmap. |
| 51 | */ |
| 52 | void |
| 53 | dvmHeapBitmapDelete(HeapBitmap *hb) |
| 54 | { |
| 55 | assert(hb != NULL); |
| 56 | |
| 57 | if (hb->bits != NULL) { |
| Carl Shapiro | 8d72480 | 2010-02-14 18:40:47 -0800 | [diff] [blame] | 58 | munmap((char *)hb->bits, hb->allocLen); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | } |
| 60 | memset(hb, 0, sizeof(*hb)); |
| 61 | } |
| 62 | |
| 63 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 64 | * Fill the bitmap with zeroes. Returns the bitmap's memory to |
| 65 | * the system as a side-effect. |
| 66 | */ |
| 67 | void |
| 68 | dvmHeapBitmapZero(HeapBitmap *hb) |
| 69 | { |
| 70 | assert(hb != NULL); |
| 71 | |
| 72 | if (hb->bits != NULL) { |
| 73 | /* This returns the memory to the system. |
| 74 | * Successive page faults will return zeroed memory. |
| 75 | */ |
| 76 | madvise(hb->bits, hb->bitsLen, MADV_DONTNEED); |
| 77 | hb->max = hb->base - 1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Walk through the bitmaps in increasing address order, and find the |
| Barry Hayes | 1c206f6 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 83 | * object pointers that correspond to garbage objects. Call |
| 84 | * <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] | 85 | * |
| Carl Shapiro | 879011d | 2010-12-02 16:41:28 -0800 | [diff] [blame] | 86 | * The callback is not permitted to increase the max of either bitmap. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 87 | */ |
| Barry Hayes | 1c206f6 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 88 | void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb, |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 89 | BitmapSweepCallback *callback, void *callbackArg) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 90 | { |
| Carl Shapiro | c6eb967 | 2010-12-08 15:40:24 -0800 | [diff] [blame^] | 91 | void *pointerBuf[4 * HB_BITS_PER_WORD]; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 92 | void **pb = pointerBuf; |
| 93 | size_t index; |
| 94 | size_t i; |
| Carl Shapiro | 879011d | 2010-12-02 16:41:28 -0800 | [diff] [blame] | 95 | unsigned long *live, *mark; |
| 96 | uintptr_t offset; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 97 | |
| Barry Hayes | 1c206f6 | 2010-07-21 12:03:02 -0700 | [diff] [blame] | 98 | assert(liveHb != NULL); |
| 99 | assert(liveHb->bits != NULL); |
| 100 | assert(markHb != NULL); |
| 101 | assert(markHb->bits != NULL); |
| Carl Shapiro | f6426e3 | 2010-11-30 20:55:32 -0800 | [diff] [blame] | 102 | assert(liveHb->base == markHb->base); |
| 103 | assert(liveHb->bitsLen == markHb->bitsLen); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 104 | assert(callback != NULL); |
| Carl Shapiro | f6426e3 | 2010-11-30 20:55:32 -0800 | [diff] [blame] | 105 | if (liveHb->max < liveHb->base) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 106 | /* Easy case; both are obviously empty. |
| 107 | */ |
| Carl Shapiro | 006346e | 2010-07-29 20:39:50 -0700 | [diff] [blame] | 108 | return; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 109 | } |
| Carl Shapiro | f6426e3 | 2010-11-30 20:55:32 -0800 | [diff] [blame] | 110 | offset = liveHb->max - liveHb->base; |
| 111 | index = HB_OFFSET_TO_INDEX(offset); |
| Carl Shapiro | f6426e3 | 2010-11-30 20:55:32 -0800 | [diff] [blame] | 112 | live = liveHb->bits; |
| 113 | mark = markHb->bits; |
| 114 | for (i = 0; i <= index; i++) { |
| 115 | unsigned long garbage = live[i] & ~mark[i]; |
| Carl Shapiro | 879011d | 2010-12-02 16:41:28 -0800 | [diff] [blame] | 116 | if (UNLIKELY(garbage != 0)) { |
| 117 | unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1); |
| 118 | uintptr_t ptrBase = HB_INDEX_TO_OFFSET(i) + liveHb->base; |
| 119 | while (garbage != 0) { |
| 120 | int shift = CLZ(garbage); |
| 121 | garbage &= ~(highBit >> shift); |
| 122 | *pb++ = (void *)(ptrBase + shift * HB_OBJECT_ALIGNMENT); |
| 123 | } |
| 124 | /* Make sure that there are always enough slots available */ |
| 125 | /* for an entire word of 1s. */ |
| Carl Shapiro | c6eb967 | 2010-12-08 15:40:24 -0800 | [diff] [blame^] | 126 | if (NELEM(pointerBuf) - (pb - pointerBuf) < HB_BITS_PER_WORD) { |
| Carl Shapiro | 879011d | 2010-12-02 16:41:28 -0800 | [diff] [blame] | 127 | (*callback)(pb - pointerBuf, pointerBuf, callbackArg); |
| 128 | pb = pointerBuf; |
| 129 | } |
| 130 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 131 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 132 | if (pb > pointerBuf) { |
| Carl Shapiro | 879011d | 2010-12-02 16:41:28 -0800 | [diff] [blame] | 133 | (*callback)(pb - pointerBuf, pointerBuf, callbackArg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 134 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 135 | } |