singrdk/base/Services/Smb/Shared/BufferPool.sg

67 lines
1.5 KiB
Plaintext
Raw Normal View History

2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
using System;
using Microsoft.SingSharp;
using Microsoft.Singularity;
using Microsoft.Singularity.Channels;
namespace Smb.Shared
{
class BufferPool : IDisposable
{
public BufferPool(int maxcount, int size)
{
_maxcount = maxcount;
_buffersize = size;
}
readonly int _maxcount;
readonly int _buffersize;
readonly VContainer<byte>[]! _pool = new VContainer<byte>[0x10];
int _readycount = 0;
public void Recycle([Claims]byte[]! in ExHeap buffer)
{
if (_readycount < _pool.Length) {
VContainer<byte> container = _pool[_readycount];
if (container != null) {
container.Release(buffer);
2008-11-17 18:29:00 -05:00
}
else {
2008-03-05 09:52:00 -05:00
_pool[_readycount] = new VContainer<byte>(buffer);
}
_readycount++;
2008-11-17 18:29:00 -05:00
}
else {
2008-03-05 09:52:00 -05:00
delete buffer;
}
}
public byte[]! in ExHeap Allocate()
{
2008-11-17 18:29:00 -05:00
if (_readycount > 0) {
2008-03-05 09:52:00 -05:00
byte[] in ExHeap buf = ((!)_pool[--_readycount]).Acquire();
return buf;
2008-11-17 18:29:00 -05:00
}
else {
2008-03-05 09:52:00 -05:00
byte[]! in ExHeap buf = new[ExHeap] byte[_buffersize];
return buf;
}
}
public void Dispose()
{
for (int i = 0; i < _readycount; i++) {
byte[] in ExHeap! buf = ((!)_pool[i]).Acquire();
delete buf;
}
_readycount = 0;
}
}
}