/* like realloc(r, newSize), resizeRegion will return a new region of size newSize containing the old contents of r by: 1. checking if the present region has sufficient available space to satisfy the request (if so, do nothing) 2. allocating a new region of sufficient size & copying the data TODO: if the successor 's' to r's block is free, and there is sufficient space in r + s, then just adjust sizes of r & s. */ void *resizeRegion(void *r, size_t newSize) { int oldSize; // int nextPSize; // size_t oldAndNew; BlockPrefix_t *p = regionToPrefix(r); BlockPrefix_t *nextp = getNextPrefix(p); int nextpNum = getNextPrefix(p); BlockPrefix_t *nexts; if (r != (void *)0){ /* old region existed */ oldSize = computeUsableSpace(regionToPrefix(r)); } else oldSize = 0; /* non-existant regions have size 0 */ if (oldSize >= newSize) /* old region is big enough */ return r; // else if(nextpNum != 0){ // size_t nextPSize=computeUsableSpace(nextp); // size_t oldAndNext=nextPSize+oldSize; // if(oldAndNext>=align8(newSize)){ // // size_t asize = align8(newSize); // // size_t availSize = computeUsableSpace(p); // if (oldAndNext >= (align8(newSize) + prefixSize + suffixSize + 8)) { /* split block? */ // void *freeSliverStart = (void *)p + prefixSize + suffixSize + align8(newSize); // void *freeSliverEnd = computeNextPrefixAddr(nextp); // makeFreeBlock(freeSliverStart, freeSliverEnd - freeSliverStart); // makeFreeBlock(p, freeSliverStart - (void *)p); /* piece being allocated */ // } // p->allocated = 1; /* mark as allocated */ // return prefixToRegion(p); /* convert to *region */ // } // } else { /* allocate new region & copy old data */ char *o = (char *)r; /* treat both regions as char* */ char *n = (char *)nextFitAllocRegion(newSize); int i; for (i = 0; i < oldSize; i++) /* copy byte-by-byte, should use memcpy */ n[i] = o[i]; freeRegion(o); /* free old region */ return (void *)n; } }
void freeRegions( PStack desc ) { while( !PStackIsEmpty(desc) ) freeRegion(PopPStack(desc)); FreePStack(desc); }