Exemplo n.º 1
0
 public void Reallocate(PooledByteBuffer buffer, int newCapacity, bool freeOldMemory)
 {
     if (newCapacity < 0 || newCapacity > buffer.MaxCapacity)
     {
         throw new ArgumentException("newCapacity less than 0 or greater than the max capacity.");
     }
     var oldCapacity = buffer.Capacity;
     if (oldCapacity == newCapacity)
     {
         return;
     }
     var oldBuffer = buffer.BaseArray;
     var oldChunk = buffer._chunk;
     var oldHandle = buffer._handle;
     int oldOffset = buffer.BaseOffset;
     int oldMaxLength = buffer.MaxLength;
     int readerIndex = buffer.ReaderIndex;
     int writerIndex = buffer.WriterIndex;
     this.Allocate(buffer, newCapacity);
     Buffer.BlockCopy(oldBuffer, oldOffset, buffer.BaseArray, buffer.BaseOffset, oldCapacity);
     buffer.SetIndex(readerIndex, writerIndex);
     if (freeOldMemory)
     {
         this.Free(oldChunk, oldHandle, oldMaxLength);
     }
 }
Exemplo n.º 2
0
        public void Reallocate(PooledByteBuffer buffer, int newCapacity, bool freeOldMemory)
        {
            if (newCapacity < 0 || newCapacity > buffer.MaxCapacity)
            {
                throw new ArgumentException("newCapacity less than 0 or greater than the max capacity.");
            }
            var oldCapacity = buffer.Capacity;

            if (oldCapacity == newCapacity)
            {
                return;
            }
            var oldBuffer    = buffer.BaseArray;
            var oldChunk     = buffer._chunk;
            var oldHandle    = buffer._handle;
            int oldOffset    = buffer.BaseOffset;
            int oldMaxLength = buffer.MaxLength;
            int readerIndex  = buffer.ReaderIndex;
            int writerIndex  = buffer.WriterIndex;

            this.Allocate(buffer, newCapacity);
            Buffer.BlockCopy(oldBuffer, oldOffset, buffer.BaseArray, buffer.BaseOffset, oldCapacity);
            buffer.SetIndex(readerIndex, writerIndex);
            if (freeOldMemory)
            {
                this.Free(oldChunk, oldHandle, oldMaxLength);
            }
        }
Exemplo n.º 3
0
        private void InitBufferWithSubpage(PooledByteBuffer buf, long handle, int bitmapIdx, int reqCapacity)
        {
            var memoryMapIdx = (int)handle;
            var idx          = this.PageIdx(memoryMapIdx);
            var subpage      = _subpages[idx];

            buf.Init(this, handle, this.RunOffset(memoryMapIdx) + (bitmapIdx & 0x3FFFFFFF) * subpage.ElemSize, reqCapacity, subpage.ElemSize);
        }
Exemplo n.º 4
0
 public ByteBuffer NewBuffer(int length, int maxCapacity = int.MaxValue)
 {
     var p = (int)Math.Abs(Interlocked.Increment(ref _seqNum) % _arenas.Length);
     var arena = _arenas[p];
     var buffer = new PooledByteBuffer(maxCapacity);
     arena.Allocate(buffer, length);
     return buffer;
 }
Exemplo n.º 5
0
        public ByteBuffer NewBuffer(int length, int maxCapacity = int.MaxValue)
        {
            var p      = (int)Math.Abs(Interlocked.Increment(ref _seqNum) % _arenas.Length);
            var arena  = _arenas[p];
            var buffer = new PooledByteBuffer(maxCapacity);

            arena.Allocate(buffer, length);
            return(buffer);
        }
Exemplo n.º 6
0
 private void Allocate(PooledByteBuffer buffer, int reqCapacity, int normCapacity)
 {
     lock (_sync)
     {
         if (_chunkPools.Allocate(buffer, reqCapacity, normCapacity))
         {
             return;
         }
         var chunk  = new PoolChunk(this, _pageSize, _maxOrder, _pageShifts, _chunkSize);
         var handle = chunk.Allocate(normCapacity);
         chunk.InitBuffer(buffer, handle, reqCapacity);
         _chunkPools.Add(chunk);
     }
 }
Exemplo n.º 7
0
        public void InitBuffer(PooledByteBuffer buffer, long handle, int reqCapacity)
        {
            int memoryMapIdx = (int)handle;
            int bitmapIdx    = (int)(handle >> 32);

            if (bitmapIdx == 0)
            {
                //=pagesize
                buffer.Init(this, handle, this.RunOffset(memoryMapIdx), reqCapacity, this.RunLength(memoryMapIdx));
            }
            else
            {
                this.InitBufferWithSubpage(buffer, handle, bitmapIdx, reqCapacity);
            }
        }
Exemplo n.º 8
0
        public void Allocate(PooledByteBuffer buffer, int reqCapacity)
        {
            var normCapacity = this.NormalizeCapacity(reqCapacity);

            if (this.IsTinyOrSmall(normCapacity))
            {
                var        tableIdx = 0;
                PoolPage[] table;
                if ((normCapacity & 0xFFFFFE00) == 0)
                {
                    tableIdx = normCapacity >> 4;
                    table    = _tinySubpagePools;
                }
                else
                {
                    tableIdx = 0;
                    var i = normCapacity >> 10;//1024
                    while (i != 0)
                    {
                        i >>= 1;
                        tableIdx++;
                    }
                    table = _smallSubpagePools;
                }
                lock (_sync)
                {
                    var head = table[tableIdx];
                    var s    = head.Next;
                    if (s != head)
                    {
                        long handle = s.Allocate();
                        s.Chunk.InitBufferWithSubpage(buffer, handle, reqCapacity);
                        return;
                    }
                }
            }
            else if (normCapacity > _chunkSize)
            {
                var unpooledChunk = new PoolChunk(this, normCapacity);
                buffer.Init(unpooledChunk, 0, 0, reqCapacity, normCapacity);
                return;
            }
            this.Allocate(buffer, reqCapacity, normCapacity);
        }
Exemplo n.º 9
0
 public void Allocate(PooledByteBuffer buffer, int reqCapacity)
 {
     var normCapacity = this.NormalizeCapacity(reqCapacity);
     if (this.IsTinyOrSmall(normCapacity))
     {
         var tableIdx = 0;
         PoolPage[] table;
         if ((normCapacity & 0xFFFFFE00) == 0)
         {
             tableIdx = normCapacity >> 4;
             table = _tinySubpagePools;
         }
         else
         {
             tableIdx = 0;
             var i = normCapacity >> 10;//1024
             while (i != 0)
             {
                 i >>= 1;
                 tableIdx++;
             }
             table = _smallSubpagePools;
         }
         lock (_sync)
         {
             var head = table[tableIdx];
             var s = head.Next;
             if (s != head)
             {
                 long handle = s.Allocate();
                 s.Chunk.InitBufferWithSubpage(buffer, handle, reqCapacity);
                 return;
             }
         }
     }
     else if (normCapacity > _chunkSize)
     {
         var unpooledChunk = new PoolChunk(this, normCapacity);
         buffer.Init(unpooledChunk, 0, 0, reqCapacity, normCapacity);
         return;
     }
     this.Allocate(buffer, reqCapacity, normCapacity);
 }
Exemplo n.º 10
0
            public bool Allocate(PooledByteBuffer buffer, int reqCapacity, int normCapacity)
            {
                if (_head == null)
                {
                    return(false);
                }
                var cur = _head;

                do
                {
                    var handle = cur.Allocate(normCapacity);
                    if (handle >= 0)
                    {
                        cur.InitBuffer(buffer, handle, reqCapacity);
                        return(true);
                    }
                } while ((cur = cur.Next) != null);
                return(false);
            }
Exemplo n.º 11
0
 public void InitBufferWithSubpage(PooledByteBuffer buf, long handle, int reqCapacity)
 {
     InitBufferWithSubpage(buf, handle, (int)(handle >> 32), reqCapacity);
 }
Exemplo n.º 12
0
 public bool Allocate(PooledByteBuffer buffer, int reqCapacity, int normCapacity)
 {
     if (_head == null)
     {
         return false;
     }
     var cur = _head;
     do
     {
         var handle = cur.Allocate(normCapacity);
         if (handle >= 0)
         {
             cur.InitBuffer(buffer, handle, reqCapacity);
             return true;
         }
     } while ((cur = cur.Next) != null);
     return false;
 }
Exemplo n.º 13
0
 private void Allocate(PooledByteBuffer buffer, int reqCapacity, int normCapacity)
 {
     lock (_sync)
     {
         if (_chunkPools.Allocate(buffer, reqCapacity, normCapacity))
         {
             return;
         }
         var chunk = new PoolChunk(this, _pageSize, _maxOrder, _pageShifts, _chunkSize);
         var handle = chunk.Allocate(normCapacity);
         chunk.InitBuffer(buffer, handle, reqCapacity);
         _chunkPools.Add(chunk);
     }
 }
Exemplo n.º 14
0
 private void InitBufferWithSubpage(PooledByteBuffer buf, long handle, int bitmapIdx, int reqCapacity)
 {
     var memoryMapIdx = (int)handle;
     var idx = this.PageIdx(memoryMapIdx);
     var subpage = _subpages[idx];
     buf.Init(this, handle, this.RunOffset(memoryMapIdx) + (bitmapIdx & 0x3FFFFFFF) * subpage.ElemSize, reqCapacity, subpage.ElemSize);
 }
Exemplo n.º 15
0
 public void InitBufferWithSubpage(PooledByteBuffer buf, long handle, int reqCapacity)
 {
     InitBufferWithSubpage(buf, handle, (int)(handle >> 32), reqCapacity);
 }
Exemplo n.º 16
0
 public void InitBuffer(PooledByteBuffer buffer, long handle, int reqCapacity)
 {
     int memoryMapIdx = (int)handle;
     int bitmapIdx = (int)(handle >> 32);
     if (bitmapIdx == 0)
     {
         //=pagesize
         buffer.Init(this, handle, this.RunOffset(memoryMapIdx), reqCapacity, this.RunLength(memoryMapIdx));
     }
     else
     {
         this.InitBufferWithSubpage(buffer, handle, bitmapIdx, reqCapacity);
     }
 }