1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | internal void *
AcquireAssetMemory(game_assets *Assets, memory_index Size)
{
void *Result = 0;
for(;;)
{
asset_memory_block *Block = FindBlockForSize(Assets, Size);
if(Block)
{
Block->Flags |= AssetMemory_Used;
Assert(Size <= Block->Size);
Result = (u8 *)(Block + 1);
memory_index RemainingSize = Block->Size - Size;
memory_index BlockSplitThreshold = 4096; // TODO(casey): Set this based on the smallest asset?
if(RemainingSize > BlockSplitThreshold)
{
Block->Size -= RemainingSize;
InsertBlock(Block, RemainingSize, (u8 *)Result + Size);
}
else
{
// TODO(casey): Actually record the unused portion of the memory
// in a block so that we can do the merge on blocks when neighbors
// are freed.
}
break;
|
1 2 3 4 5 6 | +-----------------------+-------------+ | asset_memory_block | asset data | +-----------------------+-------------+ ^ ^ | | +--- Block pointer +-- Result pointer |