MCPcopy Create free account
hub / github.com/TheForceEngine/TheForceEngine / region_alloc

Function region_alloc

TheForceEngine/TFE_Memory/memoryRegion.cpp:234–284  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

232 }
233
234 void* region_alloc(MemoryRegion* region, u64 size)
235 {
236 assert(region);
237 if (size == 0) { return nullptr; }
238
239 size = alloc_align(size + sizeof(RegionAllocHeader));
240 assert(size >= 24); // at least 24 bytes is required to hold the free header.
241 if (size > region->blockSize) { return nullptr; }
242
243 for (s32 i = 0; i < region->blockCount; i++)
244 {
245 MemoryBlock* block = region->memBlocks[i];
246 if (block->sizeFree < size)
247 {
248 continue;
249 }
250
251 // Try to allocate from the closest matching bin.
252 s32 bin = getBinFromSize((u32)size);
253 for (s32 b = bin; b < ALLOC_BIN_COUNT; b++)
254 {
255 AllocHeaderFree* header = block->freeListBins[b];
256 while (header)
257 {
258 if (header->size >= size)
259 {
260 VERIFY_MEMORY();
261 void* mem = allocFromHeader(block, (RegionAllocHeader*)header, (u32)size);
262 VERIFY_MEMORY();
263 return mem;
264 }
265 header = header->binNext;
266 }
267 }
268 }
269
270 if (!region->maxBlocks || region->blockCount < region->maxBlocks)
271 {
272 if (allocateNewBlock(region))
273 {
274 VERIFY_MEMORY();
275 void* mem = region_alloc(region, size);
276 VERIFY_MEMORY();
277 return mem;
278 }
279 }
280
281 // We are all out of memory...
282 TFE_System::logWrite(LOG_ERROR, "MemoryRegion", "Failed to allocate %u bytes in region '%s'.", size, region->name);
283 return nullptr;
284 }
285
286 void* region_realloc(MemoryRegion* region, void* ptr, u64 size)
287 {

Callers 9

restoreFunction · 0.85
createChunkedArrayFunction · 0.85
allocFromChunkedArrayFunction · 0.85
region_reallocFunction · 0.85
region_testFunction · 0.85
allocator_createFunction · 0.85
allocator_newItemFunction · 0.85
bitmap_loadHDFunction · 0.85
bitmap_loadFunction · 0.85

Calls 5

alloc_alignFunction · 0.85
getBinFromSizeFunction · 0.85
allocFromHeaderFunction · 0.85
allocateNewBlockFunction · 0.85
logWriteFunction · 0.85

Tested by

no test coverage detected