public void Dispose() { if (m_BlockList != null) { int occupiedBlocks = (int)(m_BlockSlots - m_UnusedBlockSlots); for (int i = 0; i < occupiedBlocks; ++i) { UnsafeUtility.Free((m_BlockList + i)->mem, Allocator.Persistent); } UnsafeUtility.Free(m_BlockList, Allocator.Persistent); m_BlockList = null; } }
public static void DestroyMemStorage(MemStorage *storage) { int k = 0; MemBlock *block; MemBlock *dst_top = null; if (storage == null) { return; } if (storage->Parent != null) { dst_top = storage->Parent->Top; } for (block = storage->Bottom; block != null; k++) { MemBlock *temp = block; block = block->Next; if (storage->Parent != null) { if (dst_top != null) { temp->Prev = dst_top; temp->Next = dst_top->Next; if (temp->Next != null) { temp->Next->Prev = temp; } dst_top = dst_top->Next = temp; } else { dst_top = storage->Parent->Bottom = storage->Parent->Top = temp; temp->Prev = temp->Next = null; storage->FreeSpace = storage->BlockSize - sizeof(MemBlock); } } else { Marshal.FreeHGlobal((IntPtr)temp); } } storage->Top = storage->Bottom = null; storage->FreeSpace = 0; }
public NativeBlockList(int blockSize, int initialCapacity) { Checks.CheckEquals(true, UnsafeUtility.IsBlittable <T>()); m_BlockSize = blockSize; uint preAllocatedBlockCount = ComputeBlockCount((uint)initialCapacity, m_BlockSize); m_Capacity = preAllocatedBlockCount * (uint)m_BlockSize; Count = 0; m_BlockSlots = preAllocatedBlockCount; m_UnusedBlockSlots = 0; m_BlockList = (MemBlock *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <MemBlock>() * preAllocatedBlockCount, UnsafeUtility.AlignOf <MemBlock>(), Allocator.Persistent); while (preAllocatedBlockCount > 0) { --preAllocatedBlockCount; var block = new MemBlock(); block.mem = UnsafeUtility.Malloc(UnsafeUtility.SizeOf <T>() * m_BlockSize, UnsafeUtility.AlignOf <T>(), Allocator.Persistent); *(m_BlockList + preAllocatedBlockCount) = block; } }
void Grow(int blocks) { if (m_UnusedBlockSlots < blocks) { var blockSlotsCount = m_BlockSlots; m_BlockSlots = m_BlockSlots * 2 > m_BlockSlots + blocks ? m_BlockSlots * 2 : MathFunc.ToNextPow2(m_BlockSlots + Count); var newBlocks = UnsafeUtility.Malloc(UnsafeUtility.SizeOf <MemBlock>() * m_BlockSlots, UnsafeUtility.AlignOf <MemBlock>(), Allocator.Persistent); UnsafeUtility.MemCpy(newBlocks, m_BlockList, UnsafeUtility.SizeOf <MemBlock>() * blockSlotsCount); UnsafeUtility.Free(m_BlockList, Allocator.Persistent); m_BlockList = (MemBlock *)newBlocks; m_UnusedBlockSlots = m_BlockSlots - blockSlotsCount; } for (int i = 0; i < blocks; ++i) { var mem = UnsafeUtility.Malloc(UnsafeUtility.SizeOf <T>() * m_BlockSize, UnsafeUtility.AlignOf <T>(), Allocator.Persistent); m_BlockList[m_BlockSlots - m_UnusedBlockSlots].mem = mem; --m_UnusedBlockSlots; } m_Capacity += (uint)(blocks * m_BlockSize); }