-fully working betterC for windows
*replaced array[..] = array[ .. ] sclice copy with mempcy *added own std library (allocator, alloca, Mutex) *changed tamplates for collecting components for systems -fixed issue with multiple optional components registering for system
This commit is contained in:
parent
ed99807871
commit
41f1c6474b
14 changed files with 722 additions and 868 deletions
|
|
@ -1,14 +1,15 @@
|
|||
module ecs.id_manager;
|
||||
|
||||
/*
|
||||
import std.experimental.allocator;
|
||||
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
|
||||
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;*/
|
||||
|
||||
import ecs.entity;
|
||||
import ecs.vector;
|
||||
import ecs.std;
|
||||
|
||||
import core.atomic;
|
||||
import core.stdc.string : memcpy;
|
||||
import core.sync.mutex;
|
||||
//import core.sync.mutex;
|
||||
|
||||
/************************************************************************************************************************
|
||||
*IDManager is responsible for assignment and removing IDs. IDs are unique throughtout a whole duration of the program.
|
||||
|
|
@ -35,13 +36,13 @@ begin:
|
|||
uint block_id = local_id >> 16;
|
||||
if (block_id >= m_blocks_count)
|
||||
{
|
||||
add_mutex.lock_nothrow();
|
||||
add_mutex.lock();
|
||||
if(block_id >= m_blocks_count)
|
||||
{
|
||||
m_blocks[m_blocks_count].alloc();
|
||||
m_blocks_count++;
|
||||
}
|
||||
add_mutex.unlock_nothrow();
|
||||
add_mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -145,30 +146,34 @@ begin:
|
|||
|
||||
void initialize() nothrow @nogc
|
||||
{
|
||||
m_ids_array = Mallocator.instance.makeArray!Data(65536);
|
||||
m_free_stack = Mallocator.instance.makeArray!uint(65536);
|
||||
m_blocks = Mallocator.instance.makeArray!Block(64);
|
||||
m_ids_array = Mallocator.makeArray!Data(65536);
|
||||
m_free_stack = Mallocator.makeArray!uint(65536);
|
||||
m_blocks = Mallocator.makeArray!Block(64);
|
||||
foreach(ref block;m_blocks)block = Block();
|
||||
m_blocks_count = 1;
|
||||
m_blocks[0].alloc();
|
||||
|
||||
add_mutex = Mallocator.instance.make!Mutex();
|
||||
add_mutex = Mallocator.make!Mutex();
|
||||
add_mutex.initialize();
|
||||
}
|
||||
|
||||
void deinitialize() @trusted @nogc
|
||||
void deinitialize() @trusted @nogc nothrow
|
||||
{
|
||||
if(m_ids_array)Mallocator.instance.dispose(m_ids_array);
|
||||
if(m_free_stack)Mallocator.instance.dispose(m_free_stack);
|
||||
if(m_ids_array)Mallocator.dispose(m_ids_array);
|
||||
if(m_free_stack)Mallocator.dispose(m_free_stack);
|
||||
if(m_blocks)
|
||||
{
|
||||
foreach(ref block;m_blocks)
|
||||
{
|
||||
if(block.data)block.free();
|
||||
}
|
||||
Mallocator.instance.dispose(m_blocks);
|
||||
Mallocator.dispose(m_blocks);
|
||||
}
|
||||
if(add_mutex)
|
||||
{
|
||||
Mallocator.instance.dispose(cast(void*)add_mutex); //workaround for compiler bug
|
||||
add_mutex.destroy();
|
||||
Mallocator.dispose(add_mutex);//cast(void*)add_mutex); //workaround for compiler bug
|
||||
add_mutex = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -178,14 +183,14 @@ begin:
|
|||
if(m_last_id > m_ids_array.length)
|
||||
{
|
||||
uint begin = cast(uint)m_ids_array.length;
|
||||
Data[] new_array = Mallocator.instance.makeArray!Data(begin + (m_blocks_count << 16));
|
||||
Data[] new_array = Mallocator.makeArray!Data(begin + (m_blocks_count << 16));
|
||||
memcpy(new_array.ptr, m_ids_array.ptr, m_ids_array.length * Data.sizeof);
|
||||
Mallocator.instance.dispose(m_ids_array);
|
||||
Mallocator.dispose(m_ids_array);
|
||||
m_ids_array = new_array;
|
||||
|
||||
uint[] new_stack = Mallocator.instance.makeArray!uint(m_ids_array.length);
|
||||
uint[] new_stack = Mallocator.makeArray!uint(m_ids_array.length);
|
||||
memcpy(new_stack.ptr,m_free_stack.ptr,m_free_stack.length * uint.sizeof);
|
||||
Mallocator.instance.dispose(m_free_stack);
|
||||
Mallocator.dispose(m_free_stack);
|
||||
m_free_stack = new_stack;
|
||||
|
||||
foreach(block;m_blocks[0..m_blocks_count-1])
|
||||
|
|
@ -204,13 +209,13 @@ begin:
|
|||
void alloc() nothrow @nogc
|
||||
{
|
||||
assert(data is null);
|
||||
data = Mallocator.instance.makeArray!Data(65536);
|
||||
data = Mallocator.makeArray!Data(65536);
|
||||
}
|
||||
|
||||
void free() nothrow @nogc
|
||||
{
|
||||
assert(data !is null);
|
||||
Mallocator.instance.dispose(data);
|
||||
Mallocator.dispose(data);
|
||||
data = null;
|
||||
}
|
||||
|
||||
|
|
@ -225,7 +230,7 @@ begin:
|
|||
}
|
||||
|
||||
private:
|
||||
Mutex add_mutex;
|
||||
Mutex* add_mutex;
|
||||
//shared uint m_next_id = 0;
|
||||
|
||||
//shared uint m_last_id = 0;
|
||||
|
|
@ -261,5 +266,4 @@ unittest
|
|||
assert(id3 == EntityID(2, 1));
|
||||
assert(manager.isExist(id3));
|
||||
assert(!manager.isExist(EntityID(0, 1)));
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue