public NativeMemAllocationMediator(int nativeBlockAllocSize) { _NativeBlockAllocSize = nativeBlockAllocSize; _FullCompleteBlocks = new List <native_mem_block_t>(); _ActiveBlock = AllocBlock(_NativeBlockAllocSize); }
[M(O.AggressiveInlining)] public IntPtr Alloc(int size) { Debug.Assert(size <= _NativeBlockAllocSize); if (_ActiveBlock.FreeSize() < size) { _FullCompleteBlocks.Add(_ActiveBlock); _ActiveBlock = AllocBlock(_NativeBlockAllocSize); } var ptr = _ActiveBlock.Alloc(size); return(ptr); }
private void DisposeNativeResources() { if (_FullCompleteBlocks != null) { foreach (var t in _FullCompleteBlocks) { Marshal.FreeHGlobal(t.GetBaseAddr()); } _FullCompleteBlocks = null; } if (_ActiveBlock.GetBaseAddr() != IntPtr.Zero) { Marshal.FreeHGlobal(_ActiveBlock.GetBaseAddr()); _ActiveBlock = default; } }
[M(O.AggressiveInlining)] public IntPtr AllocAndCopy(char *source, int sourceLength) { var copyLenInBytes = sourceLength * sizeof(char); var allocSizeInBytes = copyLenInBytes + sizeof(char); Debug.Assert(allocSizeInBytes <= _NativeBlockAllocSize); if (_ActiveBlock.FreeSize() < allocSizeInBytes) { _FullCompleteBlocks.Add(_ActiveBlock); _ActiveBlock = AllocBlock(_NativeBlockAllocSize); } var dest = _ActiveBlock.Alloc(allocSizeInBytes); Buffer.MemoryCopy(source, (void *)dest, copyLenInBytes, copyLenInBytes); ((char *)dest)[sourceLength] = '\0'; return(dest); }