Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IBufferPool {
- IBufferContext TakeBuffer(int size);
- void ReturnBuffer(IBufferContext buffer);
- }
- internal interface IBufferPoolManagement {
- void ReturnBuffer(byte[] buffer);
- }
- public interface IBufferContext : IDisposable {
- byte[] Buffer { get; }
- }
- public class BufferContext : IBufferContext {
- private readonly byte[] _buffer;
- private readonly IBufferPoolManagement pool;
- #IF DEBUG
- private readonly StackTrace ctor_stack;
- #ENDIF
- public byte[] Buffer {
- get { return _buffer; }
- }
- public BufferContext(IBufferPoolManagement pool, byte[] buffer) {
- _pool = pool;
- _buffer = buffer;
- #IF DEBUG
- ctor_stack = new StackTrace();
- #ENDIF
- }
- ~BufferContext() {
- Dispose(false);
- }
- public void Dispose() {
- Dispose(true);
- }
- protected virtual void Dispose(bool disposing) {
- #IF DEBUG
- if (!disposing) {
- Environment.FailFast("Somebody hasn't deterministically disposed of me!" + Environment.NewLine + ctor_stack
- }
- #ENDIF
- pool.ReturnBuffer(_buffer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement