Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dispose() for EntityStore #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/ECS/Archetype/Archetype.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
Expand Down Expand Up @@ -30,7 +30,7 @@ namespace Friflo.Engine.ECS;
/// <br/>
/// Queries can be created via generic <see cref="EntityStoreBase"/>.<c>Query()</c> methods.<br/>
/// </remarks>
public sealed class Archetype
public sealed class Archetype : IDisposable
{
#region public properties
/// <summary>Number of entities / components stored in the <see cref="Archetype"/></summary>
Expand Down Expand Up @@ -223,7 +223,17 @@ private Archetype(in ArchetypeConfig config, StructHeap[] heaps, in Tags tags)
SetStandardComponentHeaps(heap, ref std);
}
}


public void Dispose()
{
entityIds = null;
foreach (var heap in structHeaps)
{
if (heap != null)
heap.Dispose();
}
}

private static void SetStandardComponentHeaps(StructHeap heap, ref StandardComponents std)
{
var type = heap.StructType;
Expand Down
17 changes: 14 additions & 3 deletions src/ECS/Archetype/EntityStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
Expand All @@ -21,7 +21,7 @@ namespace Friflo.Engine.ECS;
/// different entity store implementations like the <see cref="RawEntityStore"/>.
/// </remarks>
[CLSCompliant(true)]
public abstract partial class EntityStoreBase
public abstract partial class EntityStoreBase : IDisposable
{
#region public properties
/// <summary>Number of all entities stored in the entity store</summary>
Expand Down Expand Up @@ -151,8 +151,19 @@ protected EntityStoreBase()
internBase.entityLists = new StackArray<EntityList> (Array.Empty<EntityList>());
singleIds = new int[Static.SingleMax];
}

public virtual void Dispose()
{
if (archs == null) return;
for (int i = 0; i < archsCount; i++)
{
var arch = archs[i];
if (arch != null)
arch.Dispose();
}
}
#endregion

protected internal abstract void UpdateEntityCompIndex(int id, int compIndex);

#region exceptions
Expand Down
4 changes: 3 additions & 1 deletion src/ECS/Archetype/StructHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Friflo.Engine.ECS;
/// - to enable maximum efficiency when GC iterate <see cref="Archetype.structHeaps"/> <see cref="Archetype.heapMap"/>
/// for collection.
/// </remarks>
internal abstract class StructHeap : IComponentStash
internal abstract class StructHeap : IComponentStash, IDisposable
{
// Note: Should not contain any other field. See class <remarks>
// --- internal fields
Expand Down Expand Up @@ -50,6 +50,8 @@ internal StructHeap(int structIndex) {
this.structIndex = structIndex;
}

public abstract void Dispose();

internal void SetArchetypeDebug(Archetype archetype) {
#if DEBUG
this.archetype = archetype;
Expand Down
9 changes: 7 additions & 2 deletions src/ECS/Archetype/StructHeap.generic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
Expand Down Expand Up @@ -38,7 +38,12 @@ internal StructHeap(int structIndex)
{
components = new T[ArchetypeUtils.MinCapacity];
}


public override void Dispose()
{
components = null;
}

internal override void StashComponent(int compIndex) {
componentStash = components[compIndex];
}
Expand Down
14 changes: 12 additions & 2 deletions src/ECS/Collections/IdArray/IdArrayHeap.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.


using System;
using System.Diagnostics.CodeAnalysis;

// ReSharper disable ConvertToPrimaryConstructor
// ReSharper disable ConvertIfStatementToSwitchStatement
// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS.Collections;

internal readonly struct IdArrayHeap
internal readonly struct IdArrayHeap : IDisposable
{
#region properties
public int Count => GetCount();
Expand All @@ -24,6 +25,15 @@ public IdArrayHeap() {
pools = new IdArrayPool[32];
}

public void Dispose()
{
foreach (var pool in pools)
{
if (pool != null)
pool.Dispose();
}
}

internal IdArrayPool GetPool (int index) => pools[index];
internal IdArrayPool GetOrCreatePool(int index) => pools[index] ??= new IdArrayPool(index);

Expand Down
11 changes: 8 additions & 3 deletions src/ECS/Collections/IdArray/IdArrayPool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
Expand All @@ -7,7 +7,7 @@
// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS.Collections;

internal sealed class IdArrayPool
internal sealed class IdArrayPool : IDisposable
{
public int Count => count;
internal int FreeCount => freeStarts.Count;
Expand All @@ -27,7 +27,12 @@ internal IdArrayPool(int poolIndex)
ids = Array.Empty<int>();
freeStarts = new StackArray<int>(Array.Empty<int>());
}


public void Dispose()
{
ids = null;
}

internal static int[] GetIds(int count, IdArrayHeap heap)
{
return heap.pools[IdArrayHeap.PoolIndex(count)].ids;
Expand Down
27 changes: 24 additions & 3 deletions src/ECS/Entity/Store/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
Expand All @@ -21,7 +21,7 @@ namespace Friflo.Engine.ECS;
/// - Permanent ids (pid's) of type long used as an alternative identifier for id of type int.<br/>
/// - Entity <see cref="Script"/>'s to support entity components via OOP.<br/>
/// </summary>
internal partial struct StoreExtension
internal partial struct StoreExtension : IDisposable
{
#region pid storage
internal Random randPid; // 8 - generate random pid's - null if UsePidAsId
Expand Down Expand Up @@ -73,7 +73,28 @@ internal StoreExtension(PidType pidType)
entityScripts = new EntityScripts[1]; // invariant: entityScripts[0] = 0
entityScriptCount = 1;
}


public void Dispose()
{
childHeap.Dispose();
if (indexMap != null)
{
foreach (var index in indexMap)
{
if (index != null)
index.Dispose();
}
}
if (relationsMap != null)
{
foreach (var relation in relationsMap)
{
if (relation != null)
relation.Dispose();
}
}
}

internal void RemoveEntity(int id) {
if (id2Pid != null) {
var pid = id2Pid[id];
Expand Down
21 changes: 17 additions & 4 deletions src/ECS/EntityStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
Expand Down Expand Up @@ -168,10 +168,23 @@ public EntityStore(PidType pidType)
dataBuffer = new DataEntity();
Info = new EntityStoreInfo(this);
}

public override void Dispose()
{
var cmd = GetCommandBuffer();
foreach (var entity in Entities)
{
cmd.DeleteEntity(entity.Id);
}
cmd.Playback();
ReturnCommandBuffer(cmd);
extension.Dispose();
base.Dispose();
}
#endregion


#region id / pid conversion

#region id / pid conversion
/// <summary>
/// Return the <see cref="Entity.Id"/> for the passed entity <paramref name="pid"/>.
/// </summary>
Expand All @@ -180,7 +193,7 @@ public EntityStore(PidType pidType)
/// Instead use <see cref="Entity.Id"/> instead of <see cref="Entity.Pid"/> if possible
/// as this method performs a <see cref="Dictionary{TKey,TValue}"/> lookup.
/// </remarks>
public int PidToId(long pid) => extension.pid2Id != null ? extension.pid2Id[pid] : (int)pid;
public int PidToId(long pid) => extension.pid2Id != null ? extension.pid2Id[pid] : (int)pid;

/// <summary>
/// Return the <see cref="Entity.Pid"/> for the passed entity <paramref name="id"/>.
Expand Down
9 changes: 7 additions & 2 deletions src/ECS/Index/Indexes/AbstractComponentIndex.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
Expand All @@ -12,7 +12,7 @@ namespace Friflo.Engine.ECS.Index;
/// Base class to enable implementing a custom component index.<br/>
/// A custom component index can be implemented to optimize indexing or component queries for a specific component type.
/// </summary>
public abstract class AbstractComponentIndex
public abstract class AbstractComponentIndex : IDisposable
{
#region properties
internal abstract int Count { get; }
Expand Down Expand Up @@ -47,6 +47,11 @@ internal AbstractComponentIndex(EntityStore store, ComponentType componentType)
var types = new ComponentTypes(componentType);
indexBit = (int)types.bitSet.l0;
}

public void Dispose()
{
idHeap.Dispose();
}
}

/// <summary>
Expand Down
14 changes: 10 additions & 4 deletions src/ECS/Relations/Internal/AbstractEntityRelations.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.


Expand All @@ -12,7 +12,7 @@
// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS.Relations;

internal abstract class AbstractEntityRelations
internal abstract class AbstractEntityRelations : IDisposable
{
internal int Count => archetype.Count;
public override string ToString() => $"relation count: {archetype.Count}";
Expand Down Expand Up @@ -46,8 +46,14 @@ internal AbstractEntityRelations(ComponentType componentType, Archetype archetyp
var types = new ComponentTypes(componentType);
relationBit = (int)types.bitSet.l0;
}

internal abstract bool AddComponent<TRelation> (int id, in TRelation component) where TRelation : struct, IRelation;

public void Dispose()
{
idHeap.Dispose();
linkIdsHeap.Dispose();
}

internal abstract bool AddComponent<TRelation> (int id, in TRelation component) where TRelation : struct, IRelation;
internal abstract IRelation GetRelationAt (int id, int index);
internal virtual ref TRelation GetEntityRelation<TRelation >(int id, int target) where TRelation : struct => throw new InvalidOperationException($"type: {GetType().Name}");
internal virtual void AddIncomingRelations (int target, List<EntityLink> result) => throw new InvalidOperationException($"type: {GetType().Name}");
Expand Down