Support more than one bucket for given entity type

This commit is contained in:
mmcomando 2018-09-10 18:34:40 +02:00
parent a61a54b43f
commit 4d35bc2a1b
2 changed files with 44 additions and 11 deletions

View file

@ -182,15 +182,42 @@ class EntityManager
void addEntity(EntityTemplate* tmpl)
{
if(tmpl.info.first_block is null)
{
tmpl.info.first_block = cast(EntitiesBlock*) AlignedMallocator.instance.alignedAllocate(4096,4096);
*tmpl.info.first_block = EntitiesBlock(tmpl.info);
}
EntitiesBlock* previous_block;
EntitiesBlock* block=tmpl.info.first_with_free_space;//tmpl.info.first_block;
void* start = tmpl.info.first_block.dataBegin() + tmpl.info.first_block.entities_count * tmpl.info.size;
// find block with enought space
while(1){
if(block is null)
{
block = cast(EntitiesBlock*) AlignedMallocator.instance.alignedAllocate(4096,4096);
*block = EntitiesBlock(tmpl.info);
if(previous_block is null)
{
tmpl.info.first_block=block;
}
else
{
previous_block.next_block=block;
}
tmpl.info.first_with_free_space=block;
break; // new block certainly has free space
}
// check if there is enought space
if(block.dataDelta() + (block.entities_count + 1) * tmpl.info.size > 4096 )
{
previous_block=block;
block=block.next_block;
continue;
}
tmpl.info.first_with_free_space=block;
break; // block exists and bounds check passed
}
void* start = block.dataBegin() + block.entities_count * tmpl.info.size;
memcpy(start, tmpl.entity_data.ptr, tmpl.info.size);
tmpl.info.first_block.entities_count++;
block.entities_count++;
}
struct CallData
@ -213,12 +240,18 @@ class EntityManager
ushort alignment;
ushort size;
EntitiesBlock* first_block;
EntitiesBlock* first_with_free_space; // a hint for allocations, should have empty space in it but doesn't have to
Vector!(CallData) callers;
}
struct EntitiesBlock
{
uint dataDelta()
{
ushort dif = EntitiesBlock.sizeof;
alignNum(dif,type_data.alignment);
return dif;
}
void* dataBegin()
{
ushort dif = EntitiesBlock.sizeof;