/// <summary> /// Initializes a new instance of the <see cref="MemoryPoolAllocator"/> class. /// </summary> /// <param name="chunkCount">The number of chunks in the pool.</param> /// <param name="bytesPerChunk">The bytes per chunk which also is the threshold for small allocations.</param> /// <param name="allocator">The allocator.</param> public MemoryPoolAllocator(int chunkCount, int bytesPerChunk, Allocator allocator) : base(true) { if (chunkCount <= 0) { throw new ArgumentException("chunkCount cannot be negative or 0", nameof(chunkCount)); } if (bytesPerChunk <= 0) { throw new ArgumentException("bytesPerChunk cannot be negative or 0", nameof(bytesPerChunk)); } int totalBytes = chunkCount * Math.Max(IntPtr.Size, bytesPerChunk); _defaultAllocator = allocator; _bufferStart = (byte *)Default.Allocate(totalBytes, initMemory: false); _bufferEnd = _bufferStart + (totalBytes); _chunkCount = chunkCount; _chunkSize = bytesPerChunk; for (int i = 0; i < chunkCount; ++i) { Chunk *cur = (Chunk *)(_bufferStart + (bytesPerChunk * i)); cur->next = _head; _head = cur; } }
public StackAllocator(int totalBytes) : base(true) { if (totalBytes <= 0) { throw new ArgumentException(totalBytes.ToString(), nameof(totalBytes)); } _buffer = (byte *)Default.Allocate(totalBytes); _length = totalBytes; _offset = _buffer; }