internal UnsafeStreamBlock *Allocate(UnsafeStreamBlock *oldBlock, int threadIndex) { Assert.IsTrue(threadIndex < BlockCount && threadIndex >= 0); UnsafeStreamBlock *block = (UnsafeStreamBlock *)UnsafeUtility.Malloc(AllocationSize, 16, Allocator); block->Next = null; if (oldBlock == null) { if (Blocks[threadIndex] == null) { Blocks[threadIndex] = block; } else { // Walk the linked list and append our new block to the end. // Otherwise, we leak memory. UnsafeStreamBlock *head = Blocks[threadIndex]; while (head->Next != null) { head = head->Next; } head->Next = block; } } else { oldBlock->Next = block; } return(block); }
/// <summary> /// Begin reading data at the iteration index. /// </summary> /// <param name="foreachIndex"></param> /// <remarks>BeginForEachIndex must always be called balanced by a EndForEachIndex.</remarks> /// <returns>The number of elements at this index.</returns> public void BeginForEachIndex(int foreachIndex) { m_ForeachIndex = foreachIndex; m_ElementCount = 0; m_NumberOfBlocks = 0; m_FirstBlock = m_CurrentBlock; m_FirstOffset = (int)(m_CurrentPtr - (byte *)m_CurrentBlock); }
internal Writer(ref UnsafeStream stream) { m_BlockStream = stream.m_Block; m_ForeachIndex = int.MinValue; m_ElementCount = -1; m_CurrentBlock = null; m_CurrentBlockEnd = null; m_CurrentPtr = null; m_FirstBlock = null; m_NumberOfBlocks = 0; m_FirstOffset = 0; m_ThreadIndex = 0; }
internal UnsafeStreamBlock *Allocate(UnsafeStreamBlock *oldBlock, int threadIndex) { Assert.IsTrue(threadIndex < BlockCount && threadIndex >= 0); UnsafeStreamBlock *block = (UnsafeStreamBlock *)Memory.Unmanaged.Allocate(AllocationSize, 16, Allocator); block->Next = null; if (oldBlock == null) { // Append our new block in front of the previous head. block->Next = Blocks[threadIndex]; Blocks[threadIndex] = block; } else { oldBlock->Next = block; } return(block); }
void Deallocate() { if (m_Block == null) { return; } for (int i = 0; i != m_Block->BlockCount; i++) { UnsafeStreamBlock *block = m_Block->Blocks[i]; while (block != null) { UnsafeStreamBlock *next = block->Next; UnsafeUtility.Free(block, m_Allocator); block = next; } } UnsafeUtility.Free(m_Block->Ranges, m_Allocator); UnsafeUtility.Free(m_Block, m_Allocator); m_Block = null; m_Allocator = Allocator.Invalid; }
void Deallocate() { if (m_Block == null) { return; } for (int i = 0; i != m_Block->BlockCount; i++) { UnsafeStreamBlock *block = m_Block->Blocks[i]; while (block != null) { UnsafeStreamBlock *next = block->Next; Memory.Unmanaged.Free(block, m_Allocator); block = next; } } Memory.Unmanaged.Free(m_Block->Ranges, m_Allocator); Memory.Unmanaged.Free(m_Block, m_Allocator); m_Block = null; m_Allocator = Allocator.None; }