Merge branch 'benaadams/memorypool2-allocate' into dev
This commit is contained in:
commit
9d19eed88f
|
|
@ -74,24 +74,21 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
|
||||||
slab: null);
|
slab: null);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
MemoryPoolBlock2 block;
|
||||||
|
if (_blocks.TryPop(out block))
|
||||||
{
|
{
|
||||||
MemoryPoolBlock2 block;
|
// block successfully taken from the stack - return it
|
||||||
if (_blocks.TryPop(out block))
|
return block;
|
||||||
{
|
|
||||||
// block successfully taken from the stack - return it
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
// no blocks available - grow the pool and try again
|
|
||||||
AllocateSlab();
|
|
||||||
}
|
}
|
||||||
|
// no blocks available - grow the pool
|
||||||
|
return AllocateSlab();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Internal method called when a block is requested and the pool is empty. It allocates one additional slab, creates all of the
|
/// Internal method called when a block is requested and the pool is empty. It allocates one additional slab, creates all of the
|
||||||
/// block tracking objects, and adds them all to the pool.
|
/// block tracking objects, and adds them all to the pool.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void AllocateSlab()
|
private MemoryPoolBlock2 AllocateSlab()
|
||||||
{
|
{
|
||||||
var slab = MemoryPoolSlab2.Create(_slabLength);
|
var slab = MemoryPoolSlab2.Create(_slabLength);
|
||||||
_slabs.Push(slab);
|
_slabs.Push(slab);
|
||||||
|
|
@ -99,8 +96,11 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
|
||||||
var basePtr = slab.ArrayPtr;
|
var basePtr = slab.ArrayPtr;
|
||||||
var firstOffset = (int)((_blockStride - 1) - ((ulong)(basePtr + _blockStride - 1) % _blockStride));
|
var firstOffset = (int)((_blockStride - 1) - ((ulong)(basePtr + _blockStride - 1) % _blockStride));
|
||||||
|
|
||||||
for (var offset = firstOffset;
|
var poolAllocationLength = _slabLength - _blockStride;
|
||||||
offset + _blockLength <= _slabLength;
|
|
||||||
|
var offset = firstOffset;
|
||||||
|
for (;
|
||||||
|
offset + _blockLength < poolAllocationLength;
|
||||||
offset += _blockStride)
|
offset += _blockStride)
|
||||||
{
|
{
|
||||||
var block = MemoryPoolBlock2.Create(
|
var block = MemoryPoolBlock2.Create(
|
||||||
|
|
@ -110,6 +110,15 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
|
||||||
slab);
|
slab);
|
||||||
Return(block);
|
Return(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return last block rather than adding to pool
|
||||||
|
var newBlock = MemoryPoolBlock2.Create(
|
||||||
|
new ArraySegment<byte>(slab.Array, offset, _blockLength),
|
||||||
|
basePtr,
|
||||||
|
this,
|
||||||
|
slab);
|
||||||
|
|
||||||
|
return newBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue