bubel-ecs/source/ecs/block_allocator.d
Mergul d3f7593afc -BlockAllocator is no longer template
-Multithreaded IDManager.getNewID()
 *use implementation with free IDs stack (instead of classic pool)
-support for multiple UpdatePasses. Passes are added by name, and must be called between begin() end() functions.
-removed mutex from addEntity()
-commit() function added. Used to commit all changes made while update() call. Called automatically by begin() end() functions.
2018-10-25 11:46:08 +02:00

43 lines
1.1 KiB
D

module ecs.block_allocator;
import ecs.manager;
import std.experimental.allocator;
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
struct BlockAllocator//(uint block_size, uint blocks_in_allocation)
{
private uint block_size;
private uint blocks_in_allocation;
void* next_block = null;
void* getBlock()
{
if (next_block is null)
allocBlock();
void* ret = next_block;
next_block = *cast(void**) next_block;
return ret;
}
void freeBlock(void* block)
{
*cast(void**)block = next_block;
next_block = block;
}
private void allocBlock()
{
next_block = cast(void*) AlignedMallocator.instance.alignedAllocate(
block_size * blocks_in_allocation, block_size);
foreach (i; 0 .. blocks_in_allocation - 1)
{
void** pointer = cast(void**)(next_block + i * block_size);
*pointer = next_block + (i + 1) * block_size;
}
void** pointer = cast(void**)(
next_block + (blocks_in_allocation - 1) * block_size);
*pointer = null;
}
}