 -- it. The memory is sufficient to hold the given number of elements of storable
 -- type. It is suitably aligned for any type, and is not cleared.
 --
-malloc :: Storable a => Int -> IO (DevicePtr a)
-malloc = doMalloc undefined
+mallocArray :: Storable a => Int -> IO (DevicePtr a)
+mallocArray = doMalloc undefined
   where
     doMalloc :: Storable a' => a' -> Int -> IO (DevicePtr a')
     doMalloc x n = resultIfOk =<< cuMemAlloc (n * sizeOf x)
 
 {# fun unsafe cuMemAlloc
-  { alloca-  `DevicePtr a' dptr*
+  { alloca'- `DevicePtr a' peekDP*
   ,          `Int'               } -> `Status' cToEnum #}
-  where dptr = liftM DevicePtr . peek
+  where
+    alloca' = F.alloca
+    peekDP  = liftM DevicePtr . peek
+
+
+-- |
+-- Execute a computation on the device, passing a pointer to a temporarily
+-- allocated block of memory sufficient to hold the given number of elements of
+-- storable type. The memory is freed when the computation terminates (normally
+-- or via an exception), so the pointer must not be used after this.
+--
+-- Note that kernel launches can be asynchronous, so you may want to add a
+-- synchronisation point using 'sync' as part of the computation.
+--
+allocaArray :: Storable a => Int -> (DevicePtr a -> IO b) -> IO b
+allocaArray n = bracket (mallocArray n) free
+
 
 -- |
 -- Release a section of device memory


{# fun unsafe cuMemcpyHtoDAsync
  { useDevicePtr `DevicePtr a'
  , castPtr      `Ptr a'
  ,              `Int'
  , useStream    `Stream'  } -> `Status' cToEnum #}


--------------------------------------------------------------------------------


module Foreign.CUDA.Driver.Marshal
  (
    -- * Host Allocation
    HostPtr, AllocFlag(..),
    withHostPtr, mallocHostArray, freeHost,

    -- * Device Allocation
    DevicePtr,
    mallocArray, allocaArray, free,

    -- * Marshalling
    peekArray, peekArrayAsync, peekListArray,
    pokeArray, pokeArrayAsync, pokeListArray,

    -- * Combined Allocation and Marshalling
    newListArray, withListArray,

    -- * Utility
    memset, getDevicePtr
  )
  where

