 -- a given value
 --
 allocaBytesMemset :: Int64 -> Int -> (DevicePtr a -> IO b) -> IO b
-allocaBytesMemset bytes symbol f =
-    allocaBytes bytes $ \dptr ->
-    memset dptr bytes symbol >>= \rv ->
-      case rv of
-        Nothing -> f dptr
-        Just e  -> error e
+allocaBytesMemset bytes symbol action =
+  allocaBytes bytes $ \dptr ->
+  memset dptr bytes symbol  >>
+  action dptr
 
 
 --------------------------------------------------------------------------------

-- meet coalescing requirements. The actual allocation width is returned.
--
malloc2D :: (Int64, Int64)              -- ^ allocation (width,height) in bytes
         -> IO (Either String (DevicePtr a, Int64))
malloc2D (width,height) =  do
    (rv,ptr,pitch) <- cudaMallocPitch width height
    case rv of
        Success -> (\p -> Right (p,pitch)) `fmap` newDevicePtr (castPtr ptr)
        _       -> return . Left . describe $ rv

{# fun unsafe cudaMallocPitch
    { alloca'-  `Ptr ()' peek'*       ,
      alloca'-  `Int64'  peekIntConv* ,
      cIntConv  `Int64'               ,
      cIntConv  `Int64'               } -> `Status' cToEnum #}
    where
        -- C->Haskell doesn't like qualified imports
        --
        alloca' :: Storable a => (Ptr a -> IO b) -> IO b
        alloca' =  F.alloca
        peek'   =  F.peek


-- |

       -> Ptr a                 -- ^ source
       -> Int64                 -- ^ number of bytes
       -> CopyDirection
       -> IO ()
memcpy dst src bytes dir =
    nothingIfOk =<< cudaMemcpy dst src bytes dir

{# fun unsafe cudaMemcpy
  { castPtr   `Ptr a'
  , castPtr   `Ptr a'
  , cIntConv  `Int64'
  , cFromEnum `CopyDirection' } -> `Status' cToEnum #}


{-
