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