using System; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; namespace GameCore.LowLevel { public unsafe struct NativeDataContainer : IDisposable where T : unmanaged { [NativeDisableUnsafePtrRestriction] internal UnsafeDataContainer* m_UnsafeDataPtr; internal AllocatorManager.AllocatorHandle m_Allocator; public bool IsCreated => m_UnsafeDataPtr != null; public NativeDataContainer(AllocatorManager.AllocatorHandle allocator) { m_UnsafeDataPtr = AllocatorManager.Allocate(allocator); *m_UnsafeDataPtr = new UnsafeDataContainer(UnsafeUtility.SizeOf(), allocator); m_Allocator = allocator; } public void Dispose() { if (!IsCreated) { return; } m_UnsafeDataPtr->Dispose(); AllocatorManager.Free(m_Allocator, m_UnsafeDataPtr); m_UnsafeDataPtr = null; } public bool Free(int index) { return m_UnsafeDataPtr->Free(index); } public int Alloc(UnsafeDataContainer.AllocOptions options = UnsafeDataContainer.AllocOptions.None) { return m_UnsafeDataPtr->Alloc(options); } public ref T ElementAt(int index) { return ref m_UnsafeDataPtr->ElementAt(index); } public void Clear() { m_UnsafeDataPtr->Clear(); } } }