Example #1
0
        private Memory <T> GetMemory(int sizeHint)
        {
            ThrowIfDisposed();
            if (sizeHint < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sizeHint));
            }

            if (sizeHint == 0)
            {
                return(GetMemory());
            }

            if (last is null)
            {
                first = last = new PooledMemoryChunk(allocator, sizeHint);
            }
            else if (last.FreeCapacity == 0)
            {
                last = new PooledMemoryChunk(allocator, sizeHint, last);
            }
            else if (last.FreeCapacity < sizeHint)
            {
                // there are two possible cases:
                // the last chunk has occupied elements - attach a new chunk (causes a hole in the memory)
                // the last chunk has no occupied elements - realloc the memory
                if (last is PooledMemoryChunk pooledChunk && pooledChunk.IsUnused)
                {
                    pooledChunk.Realloc(allocator, sizeHint);
                }
                else
                {
                    last = new PooledMemoryChunk(allocator, sizeHint, last);
                }
            }
Example #2
0
        private unsafe Memory <T> GetMemory()
        {
            if (last is null)
            {
                first = last = new PooledMemoryChunk(allocator, chunkSize);
            }
            else if (last.FreeCapacity == 0)
            {
                last = new PooledMemoryChunk(allocator, growth(chunkSize, ref chunkIndex), last);
            }

            return(last.FreeMemory);
        }
Example #3
0
        private Memory <T> GetMemory()
        {
            if (last is null)
            {
                first = last = new PooledMemoryChunk(allocator, chunkSize);
            }
            else if (last.FreeCapacity == 0)
            {
                last = new PooledMemoryChunk(allocator, chunkSize, last);
            }

            return(last.FreeMemory);
        }