完善snapshot

This commit is contained in:
孙钦者 committed 2026-01-03 04:46:00 +08:00
1 parent 5cf62f91cd
commit af72792875
6 files changed
+181 -38

No files matched your search

@@ -9,6 +9,8 @@ namespace GameCore.LowLevel
[NativeDisableUnsafePtrRestriction]
internal UnsafeDataContainer* m_UnsafeDataPtr;
internal AllocatorManager.AllocatorHandle m_Allocator;
public int Length => m_UnsafeDataPtr->Length;
public bool IsCreated => m_UnsafeDataPtr != null;
@@ -56,5 +58,10 @@ namespace GameCore.LowLevel
{
m_UnsafeDataPtr->Clear();
}
public void CopyFrom(NativeDataContainer<T> other)
{
m_UnsafeDataPtr->CopyFrom(*other.m_UnsafeDataPtr);
}
}
}
@@ -2,6 +2,7 @@
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.Assertions;
namespace GameCore.LowLevel
{
@@ -19,21 +20,23 @@ namespace GameCore.LowLevel
ClearNew = 1,
ClearAll = 2,
}
public readonly int elementSize;
private UnsafeList<FreeNode> m_Buffer;
private readonly int m_NodeNumPerElement;
public int Length => m_Buffer.Length;
public bool IsCreated => m_Buffer.IsCreated;
public UnsafeDataContainer(int elementSize, AllocatorManager.AllocatorHandle allocator)
{
if (elementSize <= 0)
throw new ArgumentException(nameof(elementSize));
this.elementSize = elementSize;
m_NodeNumPerElement = ComputeNodeNumPerElement(elementSize);
m_Buffer = new UnsafeList<FreeNode>(0, allocator);
m_Buffer.Resize(m_NodeNumPerElement, NativeArrayOptions.ClearMemory);
}
@@ -54,7 +57,7 @@ namespace GameCore.LowLevel
ref var node = ref m_Buffer.ElementAt(index * m_NodeNumPerElement);
if (node.next >= 0)
return false;
ref var head = ref m_Buffer.ElementAt(0);
node.next = head.next;
head.next = index;
@@ -68,11 +71,14 @@ namespace GameCore.LowLevel
if (index == 0)
{
index = m_Buffer.Length / m_NodeNumPerElement;
m_Buffer.Resize(m_Buffer.Length + m_NodeNumPerElement, options == AllocOptions.None ? NativeArrayOptions.UninitializedMemory : NativeArrayOptions.ClearMemory);
m_Buffer.Resize(m_Buffer.Length + m_NodeNumPerElement,
options == AllocOptions.None
? NativeArrayOptions.UninitializedMemory
: NativeArrayOptions.ClearMemory);
m_Buffer.ElementAt(index * m_NodeNumPerElement).next = -1;
return index;
}
ref var node = ref m_Buffer.ElementAt(index * m_NodeNumPerElement);
if (options == AllocOptions.ClearAll)
{
@@ -89,8 +95,8 @@ namespace GameCore.LowLevel
{
ref var node = ref m_Buffer.ElementAt(index * m_NodeNumPerElement);
if (node.next >= 0)
throw new Exception("Index out of range");
throw new IndexOutOfRangeException();
return ref UnsafeUtility.As<FreeNode, T>(ref m_Buffer.ElementAt(index * m_NodeNumPerElement + 1));
}
@@ -100,6 +106,16 @@ namespace GameCore.LowLevel
m_Buffer.ElementAt(0).next = 0;
}
public void CopyFrom(UnsafeDataContainer other)
{
if (elementSize != other.elementSize)
{
throw new InvalidOperationException();
}
m_Buffer.CopyFrom(other.m_Buffer);
}
internal static int ComputeNodeNumPerElement(int elementSize)
{
return (elementSize + UnsafeUtility.SizeOf<FreeNode>() - 1) / UnsafeUtility.SizeOf<FreeNode>() + 1;