-added EntityBlocks allocating in blocks (no memory freeing, currently block contain 128 EntityBlocks)

This commit is contained in:
Mergul 2018-09-13 22:37:37 +02:00
parent 1aa1fbf36b
commit 5b2a6e9890
3 changed files with 20 additions and 48 deletions

View file

@ -1,58 +1,29 @@
module ecs.entity_allocator;
enum bucketSize = 4096;
enum bucketsInAllocation = 128;
import ecs.manager;
struct Bucket
{
union
{
ubyte[bucketSize] memory;
Bucket* next;
}
}
struct BucketArray
{
Bucket[bucketsInAllocation] buckets;
}
import std.experimental.allocator.mallocator : Mallocator, AlignedMallocator;
import std.experimental.allocator;
struct EntityAllocator
{
BucketArray[] arrays;
Bucket* lastEmptyBucket;
void* next_block;
Bucket* initBucketArray(ref BucketArray bArr)
void* getBlock()
{
return null;
if(next_block is null)allocBlock();
void* ret = next_block;
next_block = *cast(void**)next_block;
return ret;
}
void allocateBucketArray()
private void allocBlock()
{
//auto bucketArray = new BucketArray();
BucketArray bucketArray;
assert(cast(uint)bucketArray.buckets[0].memory.ptr % bucketSize == 0);
arrays ~= bucketArray;
Bucket* lasBucket = initBucketArray(bucketArray);
lastEmptyBucket = lasBucket;
}
void allocateBucket()
next_block = cast(void*)AlignedMallocator.instance.alignedAllocate(EntityManager.page_size * EntityManager.pages_in_block, EntityManager.page_size);
foreach(i;0..EntityManager.pages_in_block-1)
{
void** pointer = cast(void**)(next_block + i * EntityManager.page_size);
*pointer = next_block + (i+1) * EntityManager.page_size;
}
ubyte[] getMemory()
{
if (lastEmptyBucket is null)
{
allocateBucket();
}
auto bucketTmp = lastEmptyBucket;
lastEmptyBucket = bucketTmp.next;
return bucketTmp.memory[];
}
}

View file

@ -14,6 +14,7 @@ import ecs.entity;
import ecs.vector;
import ecs.hash_map;
import ecs.id_manager;
import ecs.entity_allocator;
alias gEM = EntityManager.instance;
@ -441,8 +442,7 @@ class EntityManager
{
if (block is null)
{
block = cast(EntitiesBlock*) AlignedMallocator.instance.alignedAllocate(page_size,
page_size);
block = cast(EntitiesBlock*) allocator.getBlock();//AlignedMallocator.instance.alignedAllocate(page_size, page_size);
*block = EntitiesBlock(info);
if (previous_block is null)
{
@ -619,6 +619,7 @@ class EntityManager
enum pages_in_block = 128;
IDManager id_manager;
EntityAllocator allocator;
HashMap!(ushort[], EntityInfo*) entities_infos;
HashMap!(string, uint) components_map;

View file

@ -101,7 +101,7 @@ int main()
test2.b += 2;
test2.a = 8;
//writeln("Jakis tekst! ",test2.b);
writeln("Low priority tekst! ");
//writeln("Low priority tekst! ");
}
void handleEvent(Event event, ref TestComp comp)
@ -122,7 +122,7 @@ int main()
{
assert(cast(size_t)&test % TestComp.alignof == 0);
writeln("High priority tekst! ");
//writeln("High priority tekst! ");
}
void handleEvent(Event event, ref TestComp comp)
@ -155,7 +155,7 @@ int main()
void update(ref Entity entity, ref TestComp3 test) //ref TestComp comp)
{
writeln("TestSystem2 update");
//writeln("TestSystem2 update");
test.gg += 14;
}