 moduleErr :: String -> String -> a
 moduleErr fun msg =  error ("Foreign.CUDA." ++ fun ++ ':':' ':msg)
 
+moduleForceEither :: Either String a -> a
+moduleForceEither (Left  s) = moduleErr "Marshal" s
+moduleForceEither (Right r) = r
+

new :: Storable a => a -> IO (DevicePtr a)
new v =
    let bytes = fromIntegral (F.sizeOf v) in
    forceEither `fmap` malloc bytes >>= \dptr ->
    poke dptr v >>= \rv ->
    case rv of
        Nothing -> return dptr

newArray   :: Storable a => [a] -> IO (DevicePtr a)
newArray v =
    let bytes = fromIntegral (length v) * fromIntegral (F.sizeOf (head v)) in
    moduleForceEither `fmap` malloc bytes >>= \dptr ->
    pokeArray dptr v >>= \rv ->
    case rv of
        Nothing -> return dptr
