2 * Copyright (C) 2008 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 * Reference table management.
23 * Initialize a ReferenceTable structure.
25 bool dvmInitReferenceTable(ReferenceTable* pRef, int initialCount,
28 assert(initialCount > 0);
29 assert(initialCount <= maxCount);
31 pRef->table = (Object**) malloc(initialCount * sizeof(Object*));
32 if (pRef->table == NULL)
35 memset(pRef->table, 0xdd, initialCount * sizeof(Object*));
37 pRef->nextEntry = pRef->table;
38 pRef->allocEntries = initialCount;
39 pRef->maxEntries = maxCount;
45 * Clears out the contents of a ReferenceTable, freeing allocated storage.
47 void dvmClearReferenceTable(ReferenceTable* pRef)
50 pRef->table = pRef->nextEntry = NULL;
51 pRef->allocEntries = pRef->maxEntries = -1;
55 * Add "obj" to "pRef".
57 bool dvmAddToReferenceTable(ReferenceTable* pRef, Object* obj)
60 assert(dvmIsHeapAddress(obj));
61 assert(pRef->table != NULL);
62 assert(pRef->allocEntries <= pRef->maxEntries);
64 if (pRef->nextEntry == pRef->table + pRef->allocEntries) {
65 /* reached end of allocated space; did we hit buffer max? */
66 if (pRef->nextEntry == pRef->table + pRef->maxEntries) {
67 LOGW("ReferenceTable overflow (max=%d)", pRef->maxEntries);
74 newSize = pRef->allocEntries * 2;
75 if (newSize > pRef->maxEntries)
76 newSize = pRef->maxEntries;
77 assert(newSize > pRef->allocEntries);
79 newTable = (Object**) realloc(pRef->table, newSize * sizeof(Object*));
80 if (newTable == NULL) {
81 LOGE("Unable to expand ref table (from %d to %d %d-byte entries)",
82 pRef->allocEntries, newSize, sizeof(Object*));
85 LOGVV("Growing %p from %d to %d", pRef, pRef->allocEntries, newSize);
87 /* update entries; adjust "nextEntry" in case memory moved */
88 pRef->nextEntry = newTable + (pRef->nextEntry - pRef->table);
89 pRef->table = newTable;
90 pRef->allocEntries = newSize;
93 *pRef->nextEntry++ = obj;
98 * Returns NULL if not found.
100 Object** dvmFindInReferenceTable(const ReferenceTable* pRef, Object** bottom,
105 ptr = pRef->nextEntry;
106 while (--ptr >= bottom) {
114 * Remove "obj" from "pRef". We start at the end of the list (where the
115 * most-recently-added element is), and stop searching for a match after
116 * examining the element at "bottom".
118 * Most of the time "obj" is at or near the end of the list. If not, we
121 bool dvmRemoveFromReferenceTable(ReferenceTable* pRef, Object** bottom,
126 assert(pRef->table != NULL);
129 * Scan from the most-recently-added entry up to the bottom entry for
132 ptr = dvmFindInReferenceTable(pRef, bottom, obj);
140 int moveCount = pRef->nextEntry - ptr;
141 if (moveCount != 0) {
142 /* remove from middle, slide the rest down */
143 memmove(ptr, ptr+1, moveCount * sizeof(Object*));
144 //ALOGV("LREF delete %p, shift %d down", obj, moveCount);
146 /* last entry, falls off the end */
147 //ALOGV("LREF delete %p from end", obj);
154 * If "obj" is an array, return the number of elements in the array.
155 * Otherwise, return zero.
157 static size_t getElementCount(const Object* obj)
159 const ArrayObject* arrayObj = (ArrayObject*) obj;
160 if (arrayObj == NULL || arrayObj == kClearedJniWeakGlobal ||
161 arrayObj->clazz == NULL || !dvmIsArray(arrayObj)) {
164 return arrayObj->length;
168 * This is a qsort() callback. We sort Object* by class, allocation size,
169 * and then by the Object* itself.
171 static int compareObject(const void* vobj1, const void* vobj2)
173 const Object* obj1 = *((Object* const*) vobj1);
174 const Object* obj2 = *((Object* const*) vobj2);
176 // Ensure null references and cleared jweaks appear at the end.
183 } else if (obj2 == NULL) {
186 if (obj1 == kClearedJniWeakGlobal) {
187 if (obj2 == kClearedJniWeakGlobal) {
192 } else if (obj2 == kClearedJniWeakGlobal) {
196 if (obj1->clazz != obj2->clazz) {
197 return (u1*)obj1->clazz - (u1*)obj2->clazz;
199 size_t count1 = getElementCount(obj1);
200 size_t count2 = getElementCount(obj2);
201 if (count1 != count2) {
202 return count1 - count2;
204 return (u1*)obj1 - (u1*)obj2;
210 * Log an object with some additional info.
212 * Pass in the number of elements in the array (or 0 if this is not an
213 * array object), and the number of additional objects that are identical
214 * or equivalent to the original.
216 static void logSummaryLine(const Object* obj, size_t elems, int identical, int equiv)
219 LOGW(" NULL reference (count=%d)", equiv);
222 if (obj == kClearedJniWeakGlobal) {
223 LOGW(" cleared jweak (count=%d)", equiv);
227 std::string className(dvmHumanReadableType(obj));
228 if (obj->clazz == gDvm.classJavaLangClass) {
229 // We're summarizing multiple instances, so using the exemplar
230 // Class' type parameter here would be misleading.
231 className = "java.lang.Class";
234 StringAppendF(&className, " (%zd elements)", elems);
237 size_t total = identical + equiv + 1;
238 std::string msg(StringPrintf("%5d of %s", total, className.c_str()));
239 if (identical + equiv != 0) {
240 StringAppendF(&msg, " (%d unique instances)", equiv + 1);
242 LOGW(" %s", msg.c_str());
246 * Dump a summary of an array of references to the log file.
248 * This is used to dump the contents of ReferenceTable and IndirectRefTable
251 void dvmDumpReferenceTableContents(Object* const* refs, size_t count,
254 LOGW("%s reference table (%p) dump:", descr, refs);
261 // Dump the most recent N entries.
262 const size_t kLast = 10;
263 int first = count - kLast;
267 LOGW(" Last %d entries (of %d):", (count - first), count);
268 for (int idx = count - 1; idx >= first; --idx) {
269 const Object* ref = refs[idx];
273 if (ref == kClearedJniWeakGlobal) {
274 LOGW(" %5d: cleared jweak", idx);
277 if (ref->clazz == NULL) {
278 // should only be possible right after a plain dvmMalloc().
279 size_t size = dvmObjectSizeInHeap(ref);
280 LOGW(" %5d: %p (raw) (%zd bytes)", idx, ref, size);
284 std::string className(dvmHumanReadableType(ref));
287 size_t elems = getElementCount(ref);
289 StringAppendF(&extras, " (%zd elements)", elems);
290 } else if (ref->clazz == gDvm.classJavaLangString) {
291 const StringObject* str =
292 reinterpret_cast<const StringObject*>(ref);
295 char* s = dvmCreateCstrFromString(str);
297 for (; *p && count < 16; ++p, ++count) {
303 StringAppendF(&extras, "... (%d chars)", str->length());
307 LOGW(" %5d: %p %s%s", idx, ref, className.c_str(), extras.c_str());
310 // Make a copy of the table, and sort it.
311 Object** tableCopy = (Object**)malloc(sizeof(Object*) * count);
312 if (tableCopy == NULL) {
313 LOGE("Unable to copy table with %d elements", count);
316 memcpy(tableCopy, refs, sizeof(Object*) * count);
317 qsort(tableCopy, count, sizeof(Object*), compareObject);
318 refs = tableCopy; // use sorted list
320 // Remove any uninteresting stuff from the list. The sort moved them all to the end.
321 while (count > 0 && refs[count-1] == NULL) {
324 while (count > 0 && refs[count-1] == kClearedJniWeakGlobal) {
331 // Dump a summary of the whole table.
333 size_t equiv, identical;
334 equiv = identical = 0;
337 for (idx = 1; idx < count; idx++) {
338 elems = getElementCount(refs[idx-1]);
340 if (refs[idx] == refs[idx-1]) {
341 // same reference, added more than once.
343 } else if (refs[idx]->clazz == refs[idx-1]->clazz &&
344 getElementCount(refs[idx]) == elems)
346 // same class / element count, different object.
350 logSummaryLine(refs[idx-1], elems, identical, equiv);
351 equiv = identical = 0;
355 // Handle the last entry (everything above outputs refs[i-1]).
356 elems = getElementCount(refs[idx-1]);
357 logSummaryLine(refs[count-1], elems, identical, equiv);
363 * Dump the contents of a ReferenceTable to the log.
365 void dvmDumpReferenceTable(const ReferenceTable* pRef, const char* descr)
367 dvmDumpReferenceTableContents(pRef->table, dvmReferenceTableEntries(pRef),