     self.init(1)
   }
 
+  deinit
+  {
+    for i in head..<tail
+    {
+      if (i&mask == 0) { headptr = buffer }
+      headptr.destroy()
+      headptr.successor()
+    }
+
+    buffer.dealloc(mask+1)
+  }
+
   // Computed property accessors
 
   final override var isEmpty: Bool
   {
-    return q.count <= 0
+      return head >= tail
   }
 
   final override var isFull: Bool
   {
-    return q.count >= capacity
+      return head+capacity <= tail
   }
-
+  
   /**
     Determine whether the channel has been closed
   */

//
//  chan-bufferedN.swift
//  concurrency
//
//  Created by Guillaume Lessard on 2014-11-19.


final class SBufferedNChan<T>: Chan<T>
{
  private var buffer: UnsafeMutablePointer<T>

  // housekeeping variables

  private final let capacity: Int
  private final let mask: Int

  // housekeeping variables

  private final var head = 0
  private final var tail = 0

  private final var headptr: UnsafeMutablePointer<T>
  private final var tailptr: UnsafeMutablePointer<T>

  private let filled: dispatch_semaphore_t
  private let empty:  dispatch_semaphore_t
