-block allocator now track allocated blocks and is able to free memory
-jobs data is now allocated in System struct -written memory cleanup (AddressSanitizer don't show any leak)
This commit is contained in:
parent
9824b587fb
commit
235bbb49f2
7 changed files with 214 additions and 41 deletions
|
|
@ -36,7 +36,17 @@ export class EntityManager
|
|||
export static void initialize(uint threads_count)
|
||||
{
|
||||
if (instance is null)
|
||||
{
|
||||
instance = Mallocator.instance.make!EntityManager(threads_count);
|
||||
|
||||
with(instance)
|
||||
{
|
||||
UpdatePass* pass = Mallocator.instance.make!UpdatePass;
|
||||
pass.name = Mallocator.instance.makeArray("update");
|
||||
passes.add(pass);
|
||||
passes_map.add(cast(string) pass.name, cast(ushort)(passes.length - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export static void destroy()
|
||||
|
|
@ -44,19 +54,58 @@ export class EntityManager
|
|||
if (instance is null)
|
||||
return;
|
||||
|
||||
foreach (ref system; instance.systems)
|
||||
with(instance)
|
||||
{
|
||||
system.disable();
|
||||
}
|
||||
foreach (ref system; systems)
|
||||
{
|
||||
system.disable();
|
||||
if (system.m_destroy)
|
||||
(cast(void function(void*)) system.m_destroy)(system.m_system_pointer);
|
||||
|
||||
foreach (ref system; instance.systems)
|
||||
{
|
||||
if (system.m_destroy)
|
||||
(cast(void function(void*)) system.m_destroy)(system.m_system_pointer);
|
||||
if(system.jobs)Mallocator.instance.dispose(system.jobs);
|
||||
if(system.m_read_only_components)Mallocator.instance.dispose(system.m_read_only_components);
|
||||
if(system.m_modified_components)Mallocator.instance.dispose(system.m_modified_components);
|
||||
if(system.m_components)Mallocator.instance.dispose(system.m_components);
|
||||
if(system.m_excluded_components)Mallocator.instance.dispose(system.m_excluded_components);
|
||||
if(system.m_optional_components)Mallocator.instance.dispose(system.m_optional_components);
|
||||
if(system.name)Mallocator.instance.dispose(system.name);
|
||||
if(system.m_event_callers)Mallocator.instance.dispose(system.m_event_callers);
|
||||
|
||||
if(system.m_system_pointer)Mallocator.instance.dispose(system.m_system_pointer);
|
||||
}
|
||||
|
||||
foreach(EntityInfo* info;&entities_infos.byValue)
|
||||
{
|
||||
//if(info.components)Mallocator.instance.dispose(info.components);
|
||||
|
||||
Mallocator.instance.dispose(info);
|
||||
}
|
||||
|
||||
foreach(UpdatePass* pass; passes)
|
||||
{
|
||||
Mallocator.instance.dispose(pass);
|
||||
}
|
||||
passes.clear();
|
||||
|
||||
foreach(ComponentInfo info; components)
|
||||
{
|
||||
if(info.init_data)Mallocator.instance.dispose(info.init_data);
|
||||
}
|
||||
|
||||
foreach(EventInfo info; events)
|
||||
{
|
||||
if(info.callers)Mallocator.instance.dispose(info.callers);
|
||||
}
|
||||
|
||||
foreach(name; &components_map.byKey)
|
||||
{
|
||||
if(name)Mallocator.instance.dispose(name);
|
||||
}
|
||||
}
|
||||
|
||||
Mallocator.instance.dispose(instance);
|
||||
instance = null;
|
||||
|
||||
}
|
||||
|
||||
/************************************************************************************************************************
|
||||
|
|
@ -198,12 +247,16 @@ export class EntityManager
|
|||
entity_block_alloc_mutex = Mallocator.instance.make!Mutex;
|
||||
//event_manager = EventManager(this);
|
||||
//event_manager.manager = this;
|
||||
}
|
||||
|
||||
UpdatePass* pass = Mallocator.instance.make!UpdatePass;
|
||||
pass.name = Mallocator.instance.makeArray("update");
|
||||
passes.add(pass);
|
||||
~this()
|
||||
{
|
||||
id_manager.deinitialize();
|
||||
|
||||
passes_map.add(cast(string) pass.name, cast(ushort)(passes.length - 1));
|
||||
if(threads)Mallocator.instance.dispose(threads);
|
||||
if(entity_block_alloc_mutex)Mallocator.instance.dispose(entity_block_alloc_mutex);
|
||||
|
||||
allocator.freeMemory();
|
||||
}
|
||||
|
||||
/************************************************************************************************************************
|
||||
|
|
@ -451,7 +504,7 @@ export class EntityManager
|
|||
|
||||
enum ComponentsIndices components_info = getComponentsInfo();
|
||||
|
||||
static void genCompList()(ref System system, ref HashMap!(const(char)[], ushort) components_map)
|
||||
static void genCompList()(ref System system, ref HashMap!(char[], ushort) components_map)
|
||||
{
|
||||
|
||||
foreach (member; __traits(allMembers, Sys.EntitiesData))
|
||||
|
|
@ -661,12 +714,13 @@ export class EntityManager
|
|||
|
||||
system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys;
|
||||
system.m_priority = priority;
|
||||
(cast(Sys*) system.m_system_pointer).__ecsInitialize();
|
||||
system.jobs = (cast(Sys*) system.m_system_pointer)._ecs_jobs;
|
||||
//(cast(Sys*) system.m_system_pointer).__ecsInitialize();
|
||||
//system.jobs = (cast(Sys*) system.m_system_pointer)._ecs_jobs;
|
||||
system.jobs = Mallocator.instance.makeArray!(Job)((cast(Sys*) system.m_system_pointer).__ecs_jobs_count);
|
||||
|
||||
genCompList(system, components_map);
|
||||
|
||||
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
|
||||
ushort sys_id = systems_map.get(cast(char[])Sys.stringof, ushort.max);
|
||||
if (sys_id < systems.length)
|
||||
{
|
||||
system.enable();
|
||||
|
|
@ -761,7 +815,7 @@ export class EntityManager
|
|||
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
|
||||
*cast(Comp*) info.init_data.ptr = Comp.init; // = Comp();
|
||||
|
||||
ushort comp_id = components_map.get(Comp.stringof, ushort.max);
|
||||
ushort comp_id = components_map.get(cast(char[])Comp.stringof, ushort.max);
|
||||
if (comp_id < components.length)
|
||||
{
|
||||
Comp.component_id = comp_id;
|
||||
|
|
@ -771,7 +825,7 @@ export class EntityManager
|
|||
{
|
||||
components.add(info);
|
||||
Comp.component_id = cast(ushort)(components.length - 1);
|
||||
const(char)[] name = Mallocator.instance.makeArray(Comp.stringof);
|
||||
char[] name = Mallocator.instance.makeArray(Comp.stringof);
|
||||
components_map.add(name, cast(ushort)(components.length - 1));
|
||||
}
|
||||
}
|
||||
|
|
@ -2362,6 +2416,17 @@ export class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
if(components)Mallocator.instance.dispose(components);
|
||||
if(deltas)Mallocator.instance.dispose(deltas);
|
||||
if(tmpl_deltas)Mallocator.instance.dispose(tmpl_deltas);
|
||||
if(systems)Mallocator.instance.dispose(systems);
|
||||
if(add_listeners)Mallocator.instance.dispose(add_listeners);
|
||||
if(remove_listeners)Mallocator.instance.dispose(remove_listeners);
|
||||
if(change_listeners)Mallocator.instance.dispose(change_listeners);
|
||||
}
|
||||
|
||||
///entity components
|
||||
ushort[] components;
|
||||
|
||||
|
|
@ -2498,6 +2563,9 @@ export class EntityManager
|
|||
if (dependencies)
|
||||
{
|
||||
Mallocator.instance.dispose(dependencies);
|
||||
}
|
||||
if(exclusion)
|
||||
{
|
||||
Mallocator.instance.dispose(exclusion);
|
||||
}
|
||||
if (job_group.dependencies)
|
||||
|
|
@ -2526,10 +2594,13 @@ export class EntityManager
|
|||
assert(name);
|
||||
if (name)
|
||||
Mallocator.instance.dispose(name);
|
||||
foreach(caller; system_callers)
|
||||
{
|
||||
Mallocator.instance.dispose(caller);
|
||||
}
|
||||
system_callers.clear();
|
||||
}
|
||||
|
||||
export size_t __xtoHash();
|
||||
|
||||
char[] name;
|
||||
Vector!(SystemCaller*) system_callers;
|
||||
}
|
||||
|
|
@ -2558,8 +2629,8 @@ export class EntityManager
|
|||
uint delegate() m_thread_id_func;
|
||||
|
||||
HashMap!(ushort[], EntityInfo*) entities_infos;
|
||||
HashMap!(const(char)[], ushort) systems_map;
|
||||
HashMap!(const(char)[], ushort) components_map;
|
||||
HashMap!(char[], ushort) systems_map;
|
||||
HashMap!(char[], ushort) components_map;
|
||||
HashMap!(const(char)[], ushort) events_map;
|
||||
HashMap!(const(char)[], ushort) passes_map;
|
||||
Vector!System systems;
|
||||
|
|
@ -2578,6 +2649,15 @@ export class EntityManager
|
|||
uint allocated = 0;
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
foreach(block;blocks)
|
||||
{
|
||||
Mallocator.instance.dispose(block);
|
||||
}
|
||||
blocks.clear();
|
||||
}
|
||||
|
||||
Vector!(Block*) blocks;
|
||||
uint id;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue