     filled = dispatch_semaphore_create(0)!
     empty =  dispatch_semaphore_create(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()
   }
 

//
//  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
