Advertisement
Guest User

Untitled

a guest
Apr 29th, 2011
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. public interface IBufferPool {
  2.  
  3.   IBufferContext TakeBuffer(int size);
  4.   void ReturnBuffer(IBufferContext buffer);
  5.  
  6. }
  7.  
  8. internal interface IBufferPoolManagement {
  9.  
  10.   void ReturnBuffer(byte[] buffer);
  11.  
  12. }
  13.  
  14. public interface IBufferContext : IDisposable {
  15.  
  16.   byte[] Buffer { get; }
  17.  
  18. }
  19.  
  20. public class BufferContext : IBufferContext {
  21.  
  22.   private readonly byte[] _buffer;
  23.   private readonly IBufferPoolManagement pool;
  24.  
  25.   #IF DEBUG
  26.   private readonly StackTrace ctor_stack;
  27.   #ENDIF
  28.  
  29.   public byte[] Buffer {
  30.     get { return _buffer; }
  31.   }
  32.  
  33.   public BufferContext(IBufferPoolManagement pool, byte[] buffer) {
  34.     _pool = pool;
  35.     _buffer = buffer;
  36.    
  37.     #IF DEBUG
  38.       ctor_stack = new StackTrace();
  39.     #ENDIF
  40.   }
  41.  
  42.   ~BufferContext() {
  43.     Dispose(false);
  44.   }
  45.  
  46.   public void Dispose() {
  47.     Dispose(true);
  48.   }
  49.  
  50.   protected virtual void Dispose(bool disposing) {
  51.     #IF DEBUG
  52.     if (!disposing) {
  53.       Environment.FailFast("Somebody hasn't deterministically disposed of me!" + Environment.NewLine + ctor_stack
  54.     }
  55.     #ENDIF
  56.    
  57.     pool.ReturnBuffer(_buffer);
  58.   }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement