void* AJ_PoolRealloc(void* mem, size_t newSz) { Pool* p = heapPools; uint8_t i; if (mem) { assert((ptrdiff_t)mem >= (ptrdiff_t)heapStart); /* * Locate the pool from which the released memory was allocated */ for (i = 0; i < numPools; ++i, ++p) { if ((ptrdiff_t)mem < (ptrdiff_t)p->endOfPool) { size_t oldSz = heapConfig[i].size; /* * Don't need to do anything if the same block would be reused */ if ((newSz <= oldSz) && ((i == 0) || (newSz > heapConfig[i - 1].size))) { AJ_InfoPrintf(("AJ_Realloc pool[%d] %d bytes in place\n", (int)oldSz, (int)newSz)); } else { MemBlock* block = (MemBlock*)mem; AJ_InfoPrintf(("AJ_Realloc pool[%d] by AJ_Alloc(%d)\n", (int)oldSz, (int)newSz)); mem = AJ_PoolAlloc(newSz); if (mem) { memcpy(mem, (void*)block, min(oldSz, newSz)); /* * Put old block on the free list */ block->next = p->freeList; p->freeList = block; #ifndef NDEBUG --p->use; #endif } } return mem; } } } else { return AJ_PoolAlloc(newSz); } AJ_ErrPrintf(("AJ_Realloc of %d bytes failed\n", (int)newSz)); AJ_PoolDump(); return NULL; }
void* AJ_WSL_Malloc(size_t size) { void* mem = NULL; // allocate from the WSL pool first. AJ_EnterCriticalRegion(); if (size <= 100) { mem = AJ_PoolAlloc(size); } // if the pool was full or the size too big, fall back to malloc if (!mem) { mem = AJ_Malloc(size); } if (!mem) { AJ_ErrPrintf(("AJ_WSL_Malloc(): Malloc failed\n")); AJ_Reboot(); } AJ_LeaveCriticalRegion(); return mem; }