public AllocatedMemoryData Allocate(int size) { #if MEM_GUARD return(new AllocatedMemoryData { SizeInBytes = size, Address = ElectricFencedMemory.Allocate(size), }); #else var actualSize = Bits.PowerOf2(size); var index = GetIndexFromSize(actualSize); NativeMemory.ThreadStats stats; if (index == -1) { return(new AllocatedMemoryData(NativeMemory.AllocateMemory(size, out stats), size) { AllocatingThread = stats }); } actualSize = GetIndexSize(ref index, actualSize); // when we request 7 bytes, we want to get 16 bytes if (_freeSegments[index].TryPop(out AllocatedMemoryData list)) { return(list); } return(new AllocatedMemoryData(NativeMemory.AllocateMemory(actualSize, out stats), actualSize) { AllocatingThread = stats }); #endif }
public AllocatedMemoryData Allocate(int size) { if (_isDisposed) { ThrowAlreadyDisposedException(); } #if MEM_GUARD return(new AllocatedMemoryData { Address = ElectricFencedMemory.Allocate(size), SizeInBytes = size }); #else if (_used + size > _allocated) { GrowArena(size); } var allocation = new AllocatedMemoryData() { SizeInBytes = size, Address = _ptrCurrent }; _ptrCurrent += size; _used += size; return(allocation); #endif }
public AllocatedMemoryData Allocate(int size) { if (_isDisposed) { goto ErrorDisposed; } if (_ptrStart == null) { goto ErrorResetted; } #if MEM_GUARD return(new AllocatedMemoryData { Address = ElectricFencedMemory.Allocate(size), SizeInBytes = size }); #else size = Bits.NextPowerOf2(Math.Max(sizeof(FreeSection), size)); AllocatedMemoryData allocation; var index = Bits.MostSignificantBit(size) - 1; if (_freed[index] != null) { var section = _freed[index]; _freed[index] = section->Previous; allocation = new AllocatedMemoryData { Address = (byte *)section, SizeInBytes = section->SizeInBytes }; goto Return; } if (_used + size > _allocated) { GrowArena(size); } allocation = new AllocatedMemoryData { SizeInBytes = size, Address = _ptrCurrent }; _ptrCurrent += size; _used += size; TotalUsed += size; Return : return(allocation); #endif ErrorDisposed : ThrowAlreadyDisposedException(); ErrorResetted : ThrowInvalidAllocateFromResetWithoutRenew(); return(null); // Will never happen. }
public AllocatedMemoryData Allocate(int size) { if (_isDisposed ?? true) { goto ErrorDisposed; } if (_ptrStart == null) { goto ErrorResetted; } #if MEM_GUARD return(new AllocatedMemoryData { Address = ElectricFencedMemory.Allocate(size), SizeInBytes = size }); #else if (size < 0) { throw new ArgumentOutOfRangeException(nameof(size), size, $"Size cannot be negative"); } if (size > MaxArenaSize) { throw new ArgumentOutOfRangeException(nameof(size), size, $"Requested size {size} while maximum size is {MaxArenaSize}"); } size = Bits.PowerOf2(Math.Max(sizeof(FreeSection), size)); AllocatedMemoryData allocation; var index = Bits.MostSignificantBit(size) - 1; if (_freed[index] != null) { var section = _freed[index]; _freed[index] = section->Previous; allocation = new AllocatedMemoryData((byte *)section, section->SizeInBytes); goto Return; } if (_used + size > _allocated) { GrowArena(size); } allocation = new AllocatedMemoryData(_ptrCurrent, size); _ptrCurrent += size; _used += size; TotalUsed += size; Return: return(allocation); #endif ErrorDisposed: ThrowAlreadyDisposedException(); ErrorResetted: ThrowInvalidAllocateFromResetWithoutRenew(); return(null); // Will never happen. }