-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:
Mergul 2019-10-10 20:56:44 +02:00
parent ed99807871
commit 41f1c6474b
14 changed files with 722 additions and 868 deletions

View file

@ -3,12 +3,13 @@ module ecs.events;
import ecs.manager;
import ecs.block_allocator;
import ecs.entity;
import ecs.std;
/*
import std.experimental.allocator;
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;*/
import std.algorithm.comparison : max;
import core.sync.mutex;
//import core.sync.mutex;
/*struct Event
{
@ -20,67 +21,22 @@ import core.sync.mutex;
struct EventManager
{
//@disable this();
/*this(EntityManager m)
{
manager = m;
}*/
/*void sendSelfEvent(Ev)(EntityID id, Ev event)
{
ushort size = cast(ushort)(Ev.sizeof); // + EntityID.sizeof + ushort.sizeof);
ushort alignment = cast(ushort)(Ev.alignof);
EventList* list = &process_events;
if (list.current_block is null)
{
list.current_block = cast(EventBlock*) allocator.getBlock();
list.first_block = list.current_block;
list.current_block.index = cast(ushort)((void*).sizeof + ushort.sizeof);
}
ushort index = cast(ushort)(
list.current_block.index + ushort.sizeof + EntityID.sizeof + ushort.sizeof);
ushort aligned_index = index; //cast(ushort)(list.current_block.index);
alignNum(aligned_index, alignment);
if (aligned_index + Ev.sizeof > events_block_size)
{
list.current_block.next = cast(EventBlock*) allocator.getBlock();
list.current_block = list.current_block.next;
list.current_block.index = cast(ushort)((void*).sizeof + ushort.sizeof);
index = cast(ushort)((void*)
.sizeof + ushort.sizeof + ushort.sizeof + EntityID.sizeof + ushort.sizeof); // + EntityID.sizeof + ushort.sizeof;
aligned_index = index;
alignNum(aligned_index, alignment);
}
EventBlock* block = list.current_block;
ushort align_ = cast(ushort)(aligned_index - index);
*cast(ushort*)&block.data[block.index] = align_;
index = cast(ushort)(aligned_index - (EntityID.sizeof + ushort.sizeof));
*cast(ushort*)&block.data[index] = Ev.event_id;
*cast(EntityID*)&block.data[index + 2] = id;
*cast(Ev*)&block.data[aligned_index] = event;
block.index = cast(ushort)(aligned_index + Ev.sizeof);
}*/
void initialize(EntityManager m) nothrow @nogc
void initialize(EntityManager* m) nothrow @nogc
{
allocator = BlockAllocator(events_block_size, events_blocks_in_allocation);
event_block_alloc_mutex = Mallocator.instance.make!Mutex;
event_block_alloc_mutex = Mallocator.make!Mutex;
event_block_alloc_mutex.initialize();
manager = m;
}
void destroy()
void destroy() nothrow @nogc
{
Mallocator.instance.dispose(event_block_alloc_mutex);
if(event_block_alloc_mutex)
{
event_block_alloc_mutex.destroy();
Mallocator.dispose(event_block_alloc_mutex);
event_block_alloc_mutex = null;
}
}
export void sendEvent(Ev)(EntityID id, Ev event, uint thread_id = 0) nothrow @nogc
@ -94,9 +50,9 @@ struct EventManager
if(block is null)
{
event_block_alloc_mutex.lock_nothrow();
event_block_alloc_mutex.lock();
scope (exit)
event_block_alloc_mutex.unlock_nothrow();
event_block_alloc_mutex.unlock();
block = cast(EventBlock*) allocator.getBlock();
*block = EventBlock();
@ -106,9 +62,9 @@ struct EventManager
if(block.count >= data.max_events)
{
event_block_alloc_mutex.lock_nothrow();
event_block_alloc_mutex.lock();
scope (exit)
event_block_alloc_mutex.unlock_nothrow();
event_block_alloc_mutex.unlock();
EventBlock* new_block = cast(EventBlock*) allocator.getBlock();
*new_block = EventBlock();
@ -117,9 +73,6 @@ struct EventManager
data.blocks[block_id] = block;
}
/*void* start = cast(void*)block + data.data_offset + block.count * info.size;
Ev* event_ptr = cast(Ev*)start;
*event_ptr = event;*/
Ev* event_array = cast(Ev*)(cast(void*)block + data.data_offset);
event_array[block.count] = event;
block.count++;
@ -172,30 +125,16 @@ struct EventManager
block = null;
}
}
/*EventList tmp = current_events;
current_events = process_events;
process_events = tmp;
EventBlock* block = process_events.first_block;
while (block)
{
EventBlock* free = block;
block = block.next;
allocator.freeBlock(free);
}
process_events.first_block = null;
process_events.current_block = null;*/
}
void allocateData(uint threads_count) nothrow @nogc
{
disposeData();
events = Mallocator.instance.makeArray!EventData(gEM.events.length);
events = Mallocator.makeArray!EventData(gEM.events.length);
foreach(i,ref event;events)
{
event.blocks = Mallocator.instance.makeArray!(EventBlock*)(threads_count*2);
event.first_blocks = Mallocator.instance.makeArray!(EventBlock*)(threads_count*2);
event.blocks = Mallocator.makeArray!(EventBlock*)(threads_count*2);
event.first_blocks = Mallocator.makeArray!(EventBlock*)(threads_count*2);
event.data_offset = EventBlock.sizeof;//gEM.events[i].
gEM.alignNum(event.data_offset, gEM.events[i].alignment);
@ -216,16 +155,16 @@ struct EventManager
if(block)next_block = first_block.next;
while(block)
{
Mallocator.instance.dispose(block);
Mallocator.dispose(block);
block = next_block;
if(block)next_block = block.next;
}
}
Mallocator.instance.dispose(event.blocks);
Mallocator.instance.dispose(event.first_blocks);
Mallocator.dispose(event.blocks);
Mallocator.dispose(event.first_blocks);
}
Mallocator.instance.dispose(events);
Mallocator.dispose(events);
}
allocator.freeMemory();
}
@ -233,10 +172,6 @@ struct EventManager
~this() nothrow @nogc
{
disposeData();
/*foreach(i,ref event;events)
{
EventBlock* block = event.first_blocks;
}*/
}
///Single page size. Must be power of two.
@ -246,26 +181,10 @@ struct EventManager
struct EventBlock
{
/*union
{
struct
{
EventBlock* next;
ushort index = 2;
}
ubyte[events_block_size] data;
}*/
EventBlock* next;
ushort count = 0;
}
/*struct EventList
{
EventBlock* first_block;
EventBlock* current_block;
}*/
struct EventData
{
ushort data_offset;
@ -276,12 +195,10 @@ struct EventManager
//EventBlock*[] current_blocks;
}
/*EventList current_events;
EventList process_events;*/
uint current_index = 0;
EventData[] events;
Mutex event_block_alloc_mutex;
Mutex* event_block_alloc_mutex;
BlockAllocator/*!(events_block_size, events_blocks_in_allocation)*/ allocator;
EntityManager manager;
BlockAllocator allocator;
EntityManager* manager;
}