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