 
 final class QBufferedNChan<T>: Chan<T>
 {
-  private let q = PointerQueue<T>()
+  private var buffer: UnsafeMutablePointer<T>
 
   // housekeeping variables
 
   private let capacity: Int
-
-  // PointerQueue keeps count of its stored elements. Unfortunately, calling
-  // q.count repeatedly ends up degrading performance noticeably (~50ns/iteration).
-  // Keep track locally instead.
-
-  private var elements = 0
+  private let mask: Int
+
+  private var head = 0
+  private var tail = 0
+
+  private var headptr: UnsafeMutablePointer<T>
+  private var tailptr: UnsafeMutablePointer<T>
 
   private let readerQueue = ObjectQueue<dispatch_semaphore_t>()
   private let writerQueue = ObjectQueue<dispatch_semaphore_t>()


    dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER)

    while !self.closed &&  elements >= capacity
    {
      wait(mutex: mutex, queue: writerQueue)
      dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER)
    }

    if self.closed

  {
    self.capacity = (capacity < 1) ? 1 : capacity

    // find the next higher power of 2
    var v = self.capacity - 1
    v |= v >> 1
    v |= v >> 2
    v |= v >> 4
    v |= v >> 8
    v |= v >> 16
    v |= v >> 32

    mask = v // buffer size -1
    buffer = UnsafeMutablePointer.alloc(mask+1)
    headptr = buffer
    tailptr = buffer

    super.init()
  }

  convenience override init()
  {
    self.init(1)
  }

  deinit
  {
    for i in head..<tail
    {
      if (i&mask == 0) { headptr = buffer }
      headptr.destroy()
      headptr = headptr.successor()
    }

    buffer.dealloc(mask+1)
  }

  // Computed property accessors

  final override var isEmpty: Bool
  {
    return head >= tail
  }

  final override var isFull: Bool
  {
    return head+capacity <= tail
  }

  /**
