2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Indirect reference table management.
23 * Initialize an IndirectRefTable structure.
25 bool dvmInitIndirectRefTable(IndirectRefTable* pRef, int initialCount,
26 int maxCount, IndirectRefKind kind)
28 assert(initialCount > 0);
29 assert(initialCount <= maxCount);
30 assert(kind == kIndirectKindLocal || kind == kIndirectKindGlobal);
32 pRef->table = (Object**) malloc(initialCount * sizeof(Object*));
33 if (pRef->table == NULL)
36 memset(pRef->table, 0xd1, initialCount * sizeof(Object*));
38 pRef->segmentState.all = IRT_SEGMENT_INIT;
39 pRef->allocEntries = initialCount;
40 pRef->maxEntries = maxCount;
47 * Clears out the contents of a IndirectRefTable, freeing allocated storage.
49 void dvmClearIndirectRefTable(IndirectRefTable* pRef)
53 pRef->allocEntries = pRef->maxEntries = -1;
57 * Remove one or more segments from the top. The table entry identified
58 * by "cookie" becomes the new top-most entry.
60 * Returns false if "cookie" is invalid or the table has only one segment.
62 bool dvmPopIndirectRefTableSegmentCheck(IndirectRefTable* pRef, u4 cookie)
67 * The new value for "top" must be <= the current value. Otherwise
68 * this would represent an expansion of the table.
71 if (sst.parts.topIndex > pRef->segmentState.parts.topIndex) {
72 LOGE("Attempt to expand table with segment pop (%d to %d)\n",
73 pRef->segmentState.parts.topIndex, sst.parts.topIndex);
76 if (sst.parts.numHoles >= sst.parts.topIndex) {
77 LOGE("Absurd numHoles in cookie (%d bi=%d)\n",
78 sst.parts.numHoles, sst.parts.topIndex);
82 LOGV("--- after pop, top=%d holes=%d\n",
83 sst.parts.topIndex, sst.parts.numHoles);
89 * Make sure that the entry at "idx" is correctly paired with "iref".
91 static bool checkEntry(IndirectRefTable* pRef, IndirectRef iref, int idx)
93 Object* obj = pRef->table[idx];
94 IndirectRef checkRef = dvmObjectToIndirectRef(obj, idx, pRef->kind);
95 if (checkRef != iref) {
96 LOGW("iref mismatch: %p vs %p\n", iref, checkRef);
103 * Add "obj" to "pRef".
105 IndirectRef dvmAddToIndirectRefTable(IndirectRefTable* pRef, u4 cookie,
108 IRTSegmentState prevState;
109 prevState.all = cookie;
110 int topIndex = pRef->segmentState.parts.topIndex;
111 int bottomIndex = prevState.parts.topIndex;
114 assert(dvmIsValidObject(obj));
115 assert(pRef->table != NULL);
116 assert(pRef->allocEntries <= pRef->maxEntries);
117 assert(pRef->segmentState.parts.numHoles >= prevState.parts.numHoles);
119 if (topIndex == pRef->allocEntries) {
120 /* reached end of allocated space; did we hit buffer max? */
121 if (topIndex == pRef->maxEntries) {
122 LOGW("ReferenceTable overflow (max=%d)\n", pRef->maxEntries);
129 newSize = pRef->allocEntries * 2;
130 if (newSize > pRef->maxEntries)
131 newSize = pRef->maxEntries;
132 assert(newSize > pRef->allocEntries);
134 newTable = (Object**) realloc(pRef->table, newSize * sizeof(Object*));
135 if (newTable == NULL) {
136 LOGE("Unable to expand iref table (from %d to %d entries)\n",
137 pRef->allocEntries, newSize);
140 LOGI("Growing %p from %d to %d\n", pRef, pRef->allocEntries, newSize);
142 /* update entries; adjust "nextEntry" in case memory moved */
143 pRef->table = newTable;
144 pRef->allocEntries = newSize;
150 * We know there's enough room in the table. Now we just need to find
151 * the right spot. If there's a hole, find it and fill it; otherwise,
152 * add to the end of the list.
154 int numHoles = pRef->segmentState.parts.numHoles - prevState.parts.numHoles;
156 assert(topIndex > 1);
157 /* find the first hole; likely to be near the end of the list */
158 Object** pScan = &pRef->table[topIndex - 1];
159 assert(*pScan != NULL);
160 while (*--pScan != NULL) {
161 assert(pScan >= pRef->table + bottomIndex);
163 result = dvmObjectToIndirectRef(obj, pScan - pRef->table, pRef->kind);
165 pRef->segmentState.parts.numHoles--;
168 result = dvmObjectToIndirectRef(obj, topIndex, pRef->kind);
169 pRef->table[topIndex++] = obj;
170 pRef->segmentState.parts.topIndex = topIndex;
173 assert(result != NULL);
178 * Verify that the indirect table lookup is valid.
180 * Returns "false" if something looks bad.
182 bool dvmGetFromIndirectRefTableCheck(IndirectRefTable* pRef, IndirectRef iref)
184 int topIndex = pRef->segmentState.parts.topIndex;
185 int idx = dvmIndirectRefToIndex(iref);
188 LOGI("--- lookup on NULL iref\n");
191 if (idx >= topIndex) {
192 /* bad -- stale reference? */
193 LOGI("Attempt to access invalid index %d (top=%d)\n",
198 Object* obj = pRef->table[idx];
200 LOGI("Attempt to read from hole, iref=%p\n", iref);
203 if (!checkEntry(pRef, iref, idx))
210 * Remove "obj" from "pRef". We extract the table offset bits from "iref"
211 * and zap the corresponding entry, leaving a hole if it's not at the top.
213 * If the entry is not between the current top index and the bottom index
214 * specified by the cookie, we don't remove anything. This is the behavior
215 * required by JNI's DeleteLocalRef function.
217 * Returns "false" if nothing was removed.
219 bool dvmRemoveFromIndirectRefTable(IndirectRefTable* pRef, u4 cookie,
222 IRTSegmentState prevState;
223 prevState.all = cookie;
224 int topIndex = pRef->segmentState.parts.topIndex;
225 int bottomIndex = prevState.parts.topIndex;
227 assert(pRef->table != NULL);
228 assert(pRef->allocEntries <= pRef->maxEntries);
229 assert(pRef->segmentState.parts.numHoles >= prevState.parts.numHoles);
231 int idx = dvmIndirectRefToIndex(iref);
232 if (idx < bottomIndex) {
234 LOGV("Attempt to remove index outside index area (%d vs %d-%d)\n",
235 idx, bottomIndex, topIndex);
238 if (idx >= topIndex) {
239 /* bad -- stale reference? */
240 LOGI("Attempt to remove invalid index %d (bottom=%d top=%d)\n",
241 idx, bottomIndex, topIndex);
245 if (idx == topIndex-1) {
247 * Top-most entry. Scan up and consume holes. No need to NULL
248 * out the entry, since the test vs. topIndex will catch it.
250 if (!checkEntry(pRef, iref, idx))
254 pRef->table[idx] = (IndirectRef) 0xd3d3d3d3;
258 pRef->segmentState.parts.numHoles - prevState.parts.numHoles;
260 while (--topIndex > bottomIndex && numHoles != 0) {
261 LOGV("+++ checking for hole at %d (cookie=0x%08x) val=%p\n",
262 topIndex-1, cookie, pRef->table[topIndex-1]);
263 if (pRef->table[topIndex-1] != NULL)
265 LOGV("+++ ate hole at %d\n", topIndex-1);
268 pRef->segmentState.parts.numHoles =
269 numHoles + prevState.parts.numHoles;
270 pRef->segmentState.parts.topIndex = topIndex;
272 pRef->segmentState.parts.topIndex = topIndex-1;
273 LOGV("+++ ate last entry %d\n", topIndex-1);
277 * Not the top-most entry. This creates a hole. We NULL out the
278 * entry to prevent somebody from deleting it twice and screwing up
281 if (pRef->table[idx] == NULL) {
282 LOGV("--- WEIRD: removing null entry %d\n", idx);
285 if (!checkEntry(pRef, iref, idx))
288 pRef->table[idx] = NULL;
289 pRef->segmentState.parts.numHoles++;
290 LOGV("+++ left hole at %d, holes=%d\n",
291 idx, pRef->segmentState.parts.numHoles);
298 * This is a qsort() callback. We sort Object* by class, allocation size,
299 * and then by the Object* itself.
301 static int compareObject(const void* vobj1, const void* vobj2)
303 Object* obj1 = *((Object**) vobj1);
304 Object* obj2 = *((Object**) vobj2);
306 /* ensure null references appear at the end */
313 } else if (obj2 == NULL) {
317 if (obj1->clazz != obj2->clazz) {
318 return (u1*)obj1->clazz - (u1*)obj2->clazz;
320 int size1 = dvmObjectSizeInHeap(obj1);
321 int size2 = dvmObjectSizeInHeap(obj2);
322 if (size1 != size2) {
323 return size1 - size2;
325 return (u1*)obj1 - (u1*)obj2;
331 * Log an object with some additional info.
333 * Pass in the number of additional elements that are identical to or
334 * equivalent to the original.
336 static void logObject(Object* obj, int size, int identical, int equiv)
339 LOGW(" NULL reference (count=%d)\n", equiv);
343 if (identical + equiv != 0) {
344 LOGW("%5d of %s %dB (%d unique)\n", identical + equiv +1,
345 obj->clazz->descriptor, size, equiv +1);
347 LOGW("%5d of %s %dB\n", identical + equiv +1,
348 obj->clazz->descriptor, size);
353 * Dump the contents of a IndirectRefTable to the log.
355 void dvmDumpIndirectRefTable(const IndirectRefTable* pRef, const char* descr)
357 const int kLast = 10;
358 int count = dvmIndirectRefTableEntries(pRef);
363 LOGW("Reference table has no entries\n");
369 * Dump the most recent N entries. If there are holes, we will show
372 LOGW("Last %d entries in %s reference table:\n", kLast, descr);
373 refs = pRef->table; // use unsorted list
375 int start = count - kLast;
379 for (i = start; i < count; i++) {
382 size = dvmObjectSizeInHeap(refs[i]);
383 Object* ref = refs[i];
384 if (ref->clazz == gDvm.classJavaLangClass) {
385 ClassObject* clazz = (ClassObject*) ref;
386 LOGW("%5d: %p cls=%s '%s' (%d bytes)\n", i, ref,
387 (refs[i] == NULL) ? "-" : ref->clazz->descriptor,
388 clazz->descriptor, size);
390 LOGW("%5d: %p cls=%s (%d bytes)\n", i, ref,
391 (refs[i] == NULL) ? "-" : ref->clazz->descriptor, size);
396 * Make a copy of the table, and sort it.
398 * The NULL "holes" wind up at the end, so we can strip them off easily.
400 Object** tableCopy = (Object**)malloc(sizeof(Object*) * count);
401 memcpy(tableCopy, pRef->table, sizeof(Object*) * count);
402 qsort(tableCopy, count, sizeof(Object*), compareObject);
403 refs = tableCopy; // use sorted list
407 for (q = 0; q < count; q++)
408 LOGI("%d %p\n", q, refs[q]);
412 while (refs[count-1] == NULL) {
418 * Dump uniquified table summary. While we're at it, generate a
419 * cumulative total amount of pinned memory based on the unique entries.
421 LOGW("%s reference table summary (%d entries / %d holes):\n",
422 descr, count, holes);
423 int equiv, identical, total;
424 total = equiv = identical = 0;
425 for (i = 1; i < count; i++) {
426 size = dvmObjectSizeInHeap(refs[i-1]);
428 if (refs[i] == refs[i-1]) {
429 /* same reference, added more than once */
431 } else if (refs[i]->clazz == refs[i-1]->clazz &&
432 (int) dvmObjectSizeInHeap(refs[i]) == size)
434 /* same class / size, different object */
438 /* different class */
440 logObject(refs[i-1], size, identical, equiv);
441 equiv = identical = 0;
445 /* handle the last entry (everything above outputs refs[i-1]) */
446 size = (refs[count-1] == NULL) ? 0 : dvmObjectSizeInHeap(refs[count-1]);
448 logObject(refs[count-1], size, identical, equiv);
450 LOGW("Memory held directly by native code is %d bytes\n", total);