blob: 5ac28beb36bef5807a9c9df285196f747a5b1045 [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"
20#include <limits.h> // for ULONG_MAX
21#include <sys/mman.h> // for madvise(), mmap()
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080022
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080023#define LIKELY(exp) (__builtin_expect((exp) != 0, true))
24#define UNLIKELY(exp) (__builtin_expect((exp) != 0, false))
25
26/*
27 * Initialize a HeapBitmap so that it points to a bitmap large
28 * enough to cover a heap at <base> of <maxSize> bytes, where
29 * objects are guaranteed to be HB_OBJECT_ALIGNMENT-aligned.
30 */
31bool
32dvmHeapBitmapInit(HeapBitmap *hb, const void *base, size_t maxSize,
33 const char *name)
34{
35 void *bits;
36 size_t bitsLen;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080037
38 assert(hb != NULL);
Carl Shapiro3db1ad32010-07-12 16:09:54 -070039 assert(name != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080040 bitsLen = HB_OFFSET_TO_INDEX(maxSize) * sizeof(*hb->bits);
Carl Shapiro3db1ad32010-07-12 16:09:54 -070041 bits = dvmAllocRegion(bitsLen, PROT_READ | PROT_WRITE, name);
42 if (bits == NULL) {
43 LOGE("Could not mmap %zd-byte ashmem region '%s'", bitsLen, name);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080044 return false;
45 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080046 hb->bits = bits;
Carl Shapiro8d724802010-02-14 18:40:47 -080047 hb->bitsLen = hb->allocLen = bitsLen;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080048 hb->base = (uintptr_t)base;
49 hb->max = hb->base - 1;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080050 return true;
51}
52
53/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080054 * Clean up any resources associated with the bitmap.
55 */
56void
57dvmHeapBitmapDelete(HeapBitmap *hb)
58{
59 assert(hb != NULL);
60
61 if (hb->bits != NULL) {
Carl Shapiro8d724802010-02-14 18:40:47 -080062 munmap((char *)hb->bits, hb->allocLen);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080063 }
64 memset(hb, 0, sizeof(*hb));
65}
66
67/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080068 * Fill the bitmap with zeroes. Returns the bitmap's memory to
69 * the system as a side-effect.
70 */
71void
72dvmHeapBitmapZero(HeapBitmap *hb)
73{
74 assert(hb != NULL);
75
76 if (hb->bits != NULL) {
77 /* This returns the memory to the system.
78 * Successive page faults will return zeroed memory.
79 */
80 madvise(hb->bits, hb->bitsLen, MADV_DONTNEED);
81 hb->max = hb->base - 1;
82 }
83}
84
85/*
86 * Walk through the bitmaps in increasing address order, and find the
87 * object pointers that correspond to places where the bitmaps differ.
88 * Call <callback> zero or more times with lists of these object pointers.
89 *
90 * The <finger> argument to the callback indicates the next-highest
91 * address that hasn't been visited yet; setting bits for objects whose
92 * addresses are less than <finger> are not guaranteed to be seen by
93 * the current XorWalk. <finger> will be set to ULONG_MAX when the
94 * end of the bitmap is reached.
95 */
96bool
97dvmHeapBitmapXorWalk(const HeapBitmap *hb1, const HeapBitmap *hb2,
98 bool (*callback)(size_t numPtrs, void **ptrs,
99 const void *finger, void *arg),
100 void *callbackArg)
101{
102 static const size_t kPointerBufSize = 128;
103 void *pointerBuf[kPointerBufSize];
104 void **pb = pointerBuf;
105 size_t index;
106 size_t i;
107
108#define FLUSH_POINTERBUF(finger_) \
109 do { \
110 if (!callback(pb - pointerBuf, (void **)pointerBuf, \
111 (void *)(finger_), callbackArg)) \
112 { \
113 LOGW("dvmHeapBitmapXorWalk: callback failed\n"); \
114 return false; \
115 } \
116 pb = pointerBuf; \
117 } while (false)
118
119#define DECODE_BITS(hb_, bits_, update_index_) \
120 do { \
121 if (UNLIKELY(bits_ != 0)) { \
122 static const unsigned long kHighBit = \
123 (unsigned long)1 << (HB_BITS_PER_WORD - 1); \
124 const uintptr_t ptrBase = HB_INDEX_TO_OFFSET(i) + hb_->base; \
125/*TODO: hold onto ptrBase so we can shrink max later if possible */ \
126/*TODO: see if this is likely or unlikely */ \
127 while (bits_ != 0) { \
128 const int rshift = CLZ(bits_); \
129 bits_ &= ~(kHighBit >> rshift); \
130 *pb++ = (void *)(ptrBase + rshift * HB_OBJECT_ALIGNMENT); \
131 } \
132 /* Make sure that there are always enough slots available */ \
133 /* for an entire word of 1s. */ \
134 if (kPointerBufSize - (pb - pointerBuf) < HB_BITS_PER_WORD) { \
135 FLUSH_POINTERBUF(ptrBase + \
136 HB_BITS_PER_WORD * HB_OBJECT_ALIGNMENT); \
137 if (update_index_) { \
138 /* The callback may have caused hb_->max to grow. */ \
139 index = HB_OFFSET_TO_INDEX(hb_->max - hb_->base); \
140 } \
141 } \
142 } \
143 } while (false)
144
145 assert(hb1 != NULL);
146 assert(hb1->bits != NULL);
147 assert(hb2 != NULL);
148 assert(hb2->bits != NULL);
149 assert(callback != NULL);
150
151 if (hb1->base != hb2->base) {
152 LOGW("dvmHeapBitmapXorWalk: bitmaps cover different heaps "
153 "(0x%08x != 0x%08x)\n",
154 (uintptr_t)hb1->base, (uintptr_t)hb2->base);
155 return false;
156 }
157 if (hb1->bitsLen != hb2->bitsLen) {
158 LOGW("dvmHeapBitmapXorWalk: size of bitmaps differ (%zd != %zd)\n",
159 hb1->bitsLen, hb2->bitsLen);
160 return false;
161 }
162 if (hb1->max < hb1->base && hb2->max < hb2->base) {
163 /* Easy case; both are obviously empty.
164 */
165 return true;
166 }
167
168 /* First, walk along the section of the bitmaps that may be the same.
169 */
170 if (hb1->max >= hb1->base && hb2->max >= hb2->base) {
Carl Shapiro9cbece22010-07-12 17:45:51 -0700171 unsigned long *p1, *p2;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800172 uintptr_t offset;
173
174 offset = ((hb1->max < hb2->max) ? hb1->max : hb2->max) - hb1->base;
175//TODO: keep track of which (and whether) one is longer for later
176 index = HB_OFFSET_TO_INDEX(offset);
177
178 p1 = hb1->bits;
179 p2 = hb2->bits;
180 for (i = 0; i <= index; i++) {
181//TODO: unroll this. pile up a few in locals?
Carl Shapiro9cbece22010-07-12 17:45:51 -0700182 unsigned long diff = *p1++ ^ *p2++;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800183 DECODE_BITS(hb1, diff, false);
184//BUG: if the callback was called, either max could have changed.
185 }
186 /* The next index to look at.
187 */
188 index++;
189 } else {
190 /* One of the bitmaps is empty.
191 */
192 index = 0;
193 }
194
195 /* If one bitmap's max is larger, walk through the rest of the
196 * set bits.
197 */
198const HeapBitmap *longHb;
Carl Shapiro9cbece22010-07-12 17:45:51 -0700199unsigned long *p;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800200//TODO: may be the same size, in which case this is wasted work
201 longHb = (hb1->max > hb2->max) ? hb1 : hb2;
202 i = index;
203 index = HB_OFFSET_TO_INDEX(longHb->max - longHb->base);
204 p = longHb->bits + i;
205 for (/* i = i */; i <= index; i++) {
206//TODO: unroll this
207 unsigned long bits = *p++;
208 DECODE_BITS(longHb, bits, true);
209 }
210
211 if (pb > pointerBuf) {
212 /* Set the finger to the end of the heap (rather than longHb->max)
213 * so that the callback doesn't expect to be called again
214 * if it happens to change the current max.
215 */
216 FLUSH_POINTERBUF(longHb->base + HB_MAX_OFFSET(longHb));
217 }
218
219 return true;
220
221#undef FLUSH_POINTERBUF
222#undef DECODE_BITS
223}
224
225/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800226 * Similar to dvmHeapBitmapXorWalk(), but visit the set bits
227 * in a single bitmap.
228 */
229bool
230dvmHeapBitmapWalk(const HeapBitmap *hb,
231 bool (*callback)(size_t numPtrs, void **ptrs,
232 const void *finger, void *arg),
233 void *callbackArg)
234{
235 /* Create an empty bitmap with the same extent as <hb>.
236 * Don't actually allocate any memory.
237 */
238 HeapBitmap emptyHb = *hb;
239 emptyHb.max = emptyHb.base - 1; // empty
240 emptyHb.bits = (void *)1; // non-NULL but intentionally bad
241
242 return dvmHeapBitmapXorWalk(hb, &emptyHb, callback, callbackArg);
243}