-added EntityBlocks allocating in blocks (no memory freeing, currently block contain 128 EntityBlocks)
This commit is contained in:
parent
1aa1fbf36b
commit
5b2a6e9890
3 changed files with 20 additions and 48 deletions
|
|
@ -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[];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue