/* * "Reallocate" a piece of memory. * * If the new size is <= the old size, we return the original pointer * without doing anything. * * If the new size is > the old size, we allocate new storage, copy the * old stuff over, and mark the new stuff as free. */ void* dvmLinearRealloc(Object* classLoader, void* mem, size_t newSize) { #ifdef DISABLE_LINEAR_ALLOC return realloc(mem, newSize); #endif LinearAllocHdr* pHdr = getHeader(classLoader); /* make sure we have the right region (and mem != NULL) */ assert(mem != NULL); assert(mem >= (void*) pHdr->mapAddr && mem < (void*) (pHdr->mapAddr + pHdr->curOffset)); const u4* pLen = getBlockHeader(mem); LOGV("--- LinearRealloc(%d) old=%d\n", newSize, *pLen); /* handle size reduction case */ if (*pLen >= newSize) { if (ENFORCE_READ_ONLY) dvmLinearSetReadWrite(classLoader, mem); return mem; } void* newMem; newMem = dvmLinearAlloc(classLoader, newSize); assert(newMem != NULL); memcpy(newMem, mem, *pLen); dvmLinearFree(classLoader, mem); return newMem; }
/* * Mark an allocation as free. */ void dvmLinearFree(Object *classLoader, void *mem) { #ifdef DISABLE_LINEAR_ALLOC free(mem); return; #endif if (mem == NULL) return; /* make sure we have the right region */ assert(mem >= (void *) getHeader(classLoader)->mapAddr && mem < (void *) (getHeader(classLoader)->mapAddr + getHeader(classLoader)->curOffset)); if (ENFORCE_READ_ONLY) dvmLinearSetReadWrite(classLoader, mem); u4 *pLen = getBlockHeader(mem); *pLen |= LENGTHFLAG_FREE; if (ENFORCE_READ_ONLY) dvmLinearSetReadOnly(classLoader, mem); }