From 5b2a6e989081832d3a4785dc8853200abc80691b Mon Sep 17 00:00:00 2001 From: Mergul Date: Thu, 13 Sep 2018 22:37:37 +0200 Subject: [PATCH] -added EntityBlocks allocating in blocks (no memory freeing, currently block contain 128 EntityBlocks) --- source/ecs/entity_allocator.d | 57 +++++++++-------------------------- source/ecs/manager.d | 5 +-- tests/tests.d | 6 ++-- 3 files changed, 20 insertions(+), 48 deletions(-) diff --git a/source/ecs/entity_allocator.d b/source/ecs/entity_allocator.d index 50192c2..ae6cc61 100644 --- a/source/ecs/entity_allocator.d +++ b/source/ecs/entity_allocator.d @@ -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() - { - - } - - ubyte[] getMemory() - { - if (lastEmptyBucket is null) + 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) { - allocateBucket(); + void** pointer = cast(void**)(next_block + i * EntityManager.page_size); + *pointer = next_block + (i+1) * EntityManager.page_size; } - auto bucketTmp = lastEmptyBucket; - lastEmptyBucket = bucketTmp.next; - - return bucketTmp.memory[]; } - } diff --git a/source/ecs/manager.d b/source/ecs/manager.d index d4bf7b3..9901332 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -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; diff --git a/tests/tests.d b/tests/tests.d index 5f5f259..b71b9a9 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -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; }