blob: 3ec5ab1825ab6a1242aa0605e64940b7a6e0e0ec [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 Shapirof1461cb2010-07-29 19:51:53 -070020#include <sys/mman.h> /* for PROT_* */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080021
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080022/*
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 */
27bool
28dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
29 const char *name)
30{
31 void *bits;
32 size_t bitsLen;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080033
34 assert(hb != NULL);
Carl Shapiro3db1ad32010-07-12 16:09:54 -070035 assert(name != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080036 bitsLen = HB_OFFSET_TO_INDEX(maxSize) * sizeof(*hb->bits);
Carl Shapiro3db1ad32010-07-12 16:09:54 -070037 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 Projectf6c38712009-03-03 19:28:47 -080040 return false;
41 }
Carl Shapirofc75f3e2010-12-07 11:43:38 -080042 hb->bits = (unsigned long *)bits;
Carl Shapiro8d724802010-02-14 18:40:47 -080043 hb->bitsLen = hb->allocLen = bitsLen;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080044 hb->base = (uintptr_t)base;
45 hb->max = hb->base - 1;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080046 return true;
47}
48
49/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080050 * Clean up any resources associated with the bitmap.
51 */
52void
53dvmHeapBitmapDelete(HeapBitmap *hb)
54{
55 assert(hb != NULL);
56
57 if (hb->bits != NULL) {
Carl Shapiro8d724802010-02-14 18:40:47 -080058 munmap((char *)hb->bits, hb->allocLen);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080059 }
60 memset(hb, 0, sizeof(*hb));
61}
62
63/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080064 * Fill the bitmap with zeroes. Returns the bitmap's memory to
65 * the system as a side-effect.
66 */
67void
68dvmHeapBitmapZero(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 Hayes1c206f62010-07-21 12:03:02 -070083 * object pointers that correspond to garbage objects. Call
84 * <callback> zero or more times with lists of these object pointers.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080085 *
Carl Shapiro879011d2010-12-02 16:41:28 -080086 * The callback is not permitted to increase the max of either bitmap.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080087 */
Barry Hayes1c206f62010-07-21 12:03:02 -070088void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb,
Carl Shapiro57ee2702010-08-27 13:06:48 -070089 BitmapSweepCallback *callback, void *callbackArg)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080090{
Carl Shapiroc6eb9672010-12-08 15:40:24 -080091 void *pointerBuf[4 * HB_BITS_PER_WORD];
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080092 void **pb = pointerBuf;
93 size_t index;
94 size_t i;
Carl Shapiro879011d2010-12-02 16:41:28 -080095 unsigned long *live, *mark;
96 uintptr_t offset;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080097
Barry Hayes1c206f62010-07-21 12:03:02 -070098 assert(liveHb != NULL);
99 assert(liveHb->bits != NULL);
100 assert(markHb != NULL);
101 assert(markHb->bits != NULL);
Carl Shapirof6426e32010-11-30 20:55:32 -0800102 assert(liveHb->base == markHb->base);
103 assert(liveHb->bitsLen == markHb->bitsLen);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800104 assert(callback != NULL);
Carl Shapirof6426e32010-11-30 20:55:32 -0800105 if (liveHb->max < liveHb->base) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800106 /* Easy case; both are obviously empty.
107 */
Carl Shapiro006346e2010-07-29 20:39:50 -0700108 return;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800109 }
Carl Shapirof6426e32010-11-30 20:55:32 -0800110 offset = liveHb->max - liveHb->base;
111 index = HB_OFFSET_TO_INDEX(offset);
Carl Shapirof6426e32010-11-30 20:55:32 -0800112 live = liveHb->bits;
113 mark = markHb->bits;
114 for (i = 0; i <= index; i++) {
115 unsigned long garbage = live[i] & ~mark[i];
Carl Shapiro879011d2010-12-02 16:41:28 -0800116 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 Shapiroc6eb9672010-12-08 15:40:24 -0800126 if (NELEM(pointerBuf) - (pb - pointerBuf) < HB_BITS_PER_WORD) {
Carl Shapiro879011d2010-12-02 16:41:28 -0800127 (*callback)(pb - pointerBuf, pointerBuf, callbackArg);
128 pb = pointerBuf;
129 }
130 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800131 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800132 if (pb > pointerBuf) {
Carl Shapiro879011d2010-12-02 16:41:28 -0800133 (*callback)(pb - pointerBuf, pointerBuf, callbackArg);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800134 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800135}