public AllocationHandle Take(int size) { Assert.GreatherThan(size, 0); var id = (uint)_dynamicMemoryTracker.Take(); if (id > _handles.Length) { var newHandles = new DynamicMemoryHandle[_handles.Length * 3 / 2]; Array.Copy(_handles, newHandles, _handles.Length); _handles = newHandles; } if (!Unsafe.IsAligned16(size)) { size = Unsafe.Align16(size + 15); // some room for alignment } _size += (uint)size; var intPtr = Marshal.AllocHGlobal(size); Unsafe.ClearAlign16((void *)intPtr, size); _handles[id] = new DynamicMemoryHandle(intPtr, id, 0, (uint)size); ++_blocks; //_logger.LogDebug($"DynamicAlloc allocated { _handles[id]}"); return(new AllocationHandle(intPtr, id, 0)); }
protected override void OnUnmanagedDispose() { for (var i = 0; i < _handles.Length; i++) { Assert.EqualTo(_handles[i].IsValid, false); if (_handles[i].IsValid) { _logger.LogWarning($"{_handles[i]}\nAllocation was not released, consider enabling stack tracing."); Marshal.FreeHGlobal(_handles[i].Address); _handles[i] = new DynamicMemoryHandle(IntPtr.Zero, 0, 0, 0); //_dynamicMemoryTracker.Return(i); } } }