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)
59 assert(dvmIsValidObject(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)\n", 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)\n",
82 pRef->allocEntries, newSize, sizeof(Object*));
85 LOGVV("Growing %p from %d to %d\n", 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 //LOGV("LREF delete %p, shift %d down\n", obj, moveCount);
146 /* last entry, falls off the end */
147 //LOGV("LREF delete %p from end\n", obj);
154 * This is a qsort() callback. We sort Object* by class, allocation size,
155 * and then by the Object* itself.
157 static int compareObject(const void* vobj1, const void* vobj2)
159 Object* obj1 = *((Object**) vobj1);
160 Object* obj2 = *((Object**) vobj2);
162 if (obj1 == NULL || obj2 == NULL)
163 return (u1*)obj1 - (u1*)obj2;
165 if (obj1->clazz != obj2->clazz) {
166 return (u1*)obj1->clazz - (u1*)obj2->clazz;
168 int size1 = dvmObjectSizeInHeap(obj1);
169 int size2 = dvmObjectSizeInHeap(obj2);
170 if (size1 != size2) {
171 return size1 - size2;
173 return (u1*)obj1 - (u1*)obj2;
179 * Log an object with some additional info.
181 * Pass in the number of additional elements that are identical to or
182 * equivalent to the original.
184 static void logObject(Object* obj, int size, int identical, int equiv)
187 LOGW(" NULL reference (count=%d)\n", equiv);
191 if (identical + equiv != 0) {
192 LOGW("%5d of %s %dB (%d unique)\n", identical + equiv +1,
193 obj->clazz->descriptor, size, equiv +1);
195 LOGW("%5d of %s %dB\n", identical + equiv +1,
196 obj->clazz->descriptor, size);
201 * Dump the contents of a ReferenceTable to the log.
203 * The caller should lock any external sync before calling.
205 * (This was originally written to be tolerant of null entries in the table.
206 * I don't think that can happen anymore.)
208 void dvmDumpReferenceTable(const ReferenceTable* pRef, const char* descr)
210 const int kLast = 10;
211 int count = dvmReferenceTableEntries(pRef);
216 LOGW("Reference table has no entries\n");
222 * Dump the most recent N entries.
224 LOGW("Last %d entries in %s reference table:\n", kLast, descr);
225 refs = pRef->table; // use unsorted list
227 int start = count - kLast;
231 for (i = start; i < count; i++) {
232 size = (refs[i] == NULL) ? 0 : dvmObjectSizeInHeap(refs[i]);
233 Object* ref = refs[i];
234 if (ref->clazz == gDvm.classJavaLangClass) {
235 ClassObject* clazz = (ClassObject*) ref;
236 LOGW("%5d: %p cls=%s '%s' (%d bytes)\n", i, ref,
237 (refs[i] == NULL) ? "-" : ref->clazz->descriptor,
238 clazz->descriptor, size);
240 LOGW("%5d: %p cls=%s (%d bytes)\n", i, ref,
241 (refs[i] == NULL) ? "-" : ref->clazz->descriptor, size);
246 * Make a copy of the table, and sort it.
248 Object** tableCopy = (Object**)malloc(sizeof(Object*) * count);
249 memcpy(tableCopy, pRef->table, sizeof(Object*) * count);
250 qsort(tableCopy, count, sizeof(Object*), compareObject);
251 refs = tableCopy; // use sorted list
254 * Dump uniquified table summary. While we're at it, generate a
255 * cumulative total amount of pinned memory based on the unique entries.
257 LOGW("%s reference table summary (%d entries):\n", descr, count);
258 int equiv, identical, total;
259 total = equiv = identical = 0;
260 for (i = 1; i < count; i++) {
261 size = (refs[i-1] == NULL) ? 0 : dvmObjectSizeInHeap(refs[i-1]);
263 if (refs[i] == refs[i-1]) {
264 /* same reference, added more than once */
266 } else if (refs[i]->clazz == refs[i-1]->clazz &&
267 (int) dvmObjectSizeInHeap(refs[i]) == size)
269 /* same class / size, different object */
273 /* different class */
275 logObject(refs[i-1], size, identical, equiv);
276 equiv = identical = 0;
280 /* handle the last entry (everything above outputs refs[i-1]) */
281 size = (refs[count-1] == NULL) ? 0 : dvmObjectSizeInHeap(refs[count-1]);
283 logObject(refs[count-1], size, identical, equiv);
285 LOGW("Memory held directly by native code is %d bytes\n", total);