数据结构
typedef struct ReferenceTable {
Object** table; /* top of the list */
Object** nextEntry; /* bottom of the list */
int allocEntries; /* #of entries we have space for */
int maxEntries; /* max #of entries allowed */
} ReferenceTable;
引用表的存储结构
由于使用的单向链表,因此当添加和删除中间元素的是受,需要移动一整块的数据。不知道为何采用这种算法?
bool dvmRemoveFromReferenceTable(ReferenceTable* pRef, Object** top,
Object* obj)
{
Object** ptr;
assert(pRef->table != NULL);
/*
* Scan from the most-recently-added entry up to the top entry for
* this frame.
*/
ptr = dvmFindInReferenceTable(pRef, top, obj);
if (ptr == NULL)
return false;
/*
* Delete the entry.
*/
pRef->nextEntry--;
int moveCount = pRef->nextEntry - ptr;
if (moveCount != 0) {
/* remove from middle, slide the rest down */
memmove(ptr, ptr+1, moveCount * sizeof(Object*));
//LOGV("LREF delete %p, shift %d down\n", obj, moveCount);
} else {
/* last entry, falls off the end */
//LOGV("LREF delete %p from end\n", obj);
}
return true;
}