示例#1
0
        public PoolPointer Allocate(long size)
        {
            if (size > PoolSize)
            {
                return(null);
            }

            long currentScanOffset = 0;

            lock (PoolPointers)
            {
                foreach (var pointer in PoolPointers.OrderBy(x => x.Offset).ToArray())
                {
                    if (pointer.Offset - currentScanOffset >= size)
                    {
                        var newPointer = new PoolPointer(this, currentScanOffset, size, DefaultFixed);
                        PoolPointers.Add(newPointer);
                        return(newPointer);
                    }

                    currentScanOffset = pointer.Offset + pointer.Length;
                }

                if (PoolSize - currentScanOffset >= size)
                {
                    var newPointer = new PoolPointer(this, currentScanOffset, size, DefaultFixed);
                    PoolPointers.Add(newPointer);
                    return(newPointer);
                }
            }

            return(null);
        }
示例#2
0
 protected void UnpinPointer(PoolPointer pointer)
 {
     lock (PoolPointers)
     {
         PinnedPointers.Remove(pointer);
     }
 }
示例#3
0
 public IMemoryOwner <byte> GetMemory(PoolPointer poolPointer)
 {
     lock (PoolPointers)
     {
         PinnedPointers.Add(poolPointer);
         return(new AllocatorMemoryOwner(this, poolPointer, UnderlyingMemory.Slice((int)poolPointer.Offset, (int)poolPointer.Length)));
     }
 }
示例#4
0
        public void Release(PoolPointer poolPointer)
        {
            lock (PoolPointers)
            {
                if (PinnedPointers.Contains(poolPointer))
                {
                    throw new InvalidOperationException("Cannot release a pool pointer that is currently pinned");
                }

                PoolPointers.Remove(poolPointer);
            }
        }
示例#5
0
 public AllocatorMemoryOwner(Allocator allocator, PoolPointer pointer, Memory <byte> memory)
 {
     Allocator   = allocator;
     Memory      = memory;
     PoolPointer = pointer;
 }