internal UnmanagedWriteBuffer(JsonOperationContext context, UnmanagedBuffersPool.AllocatedMemoryData allocatedMemoryData) { _context = context; _current = new Segment { Address = (byte *)allocatedMemoryData.Address, Allocation = allocatedMemoryData, Used = 0, Previous = null }; }
public void Dispose() { if (_buffer != null) { _context.ReturnMemory(_buffer); _buffer = null; } if (_compressionBuffer != null) { _context.ReturnMemory(_compressionBuffer); _compressionBuffer = null; } _stream.Dispose(); }
/// <summary> /// Returns memory buffer to work with, be aware, this buffer is not thread safe /// </summary> /// <param name="requestedSize"></param> /// <param name="actualSize"></param> /// <returns></returns> public unsafe byte *GetNativeTempBuffer(int requestedSize, out int actualSize) { if (requestedSize == 0) { throw new ArgumentException(nameof(requestedSize)); } if (_tempBuffer == null) { _tempBuffer = GetMemory(requestedSize); } else if (requestedSize > _tempBuffer.SizeInBytes) { ReturnMemory(_tempBuffer); _tempBuffer = GetMemory(requestedSize); } actualSize = _tempBuffer.SizeInBytes; return((byte *)_tempBuffer.Address); }
public void ReturnMemory(UnmanagedBuffersPool.AllocatedMemoryData buffer) { if (buffer.SizeInBytes == 0) { return; } if (_allocatedMemory == null) { _allocatedMemory = new Stack <UnmanagedBuffersPool.AllocatedMemoryData> [32]; } var index = UnmanagedBuffersPool.GetIndexFromSize(buffer.SizeInBytes); if (index == -1) { Pool.Return(buffer); return; } if (_allocatedMemory[index] == null) { _allocatedMemory[index] = new Stack <UnmanagedBuffersPool.AllocatedMemoryData>(); } _allocatedMemory[index].Push(buffer); }