32 Foreign.Marshal.Array
Date: 2010-07-20 13:11:00

[next] [prev] [prev-tail] [tail] [up]

## Chapter 32
Foreign.Marshal.Array

module Foreign.Marshal.Array (
mallocArray, mallocArray0, allocaArray, allocaArray0, reallocArray,
reallocArray0, peekArray, peekArray0, pokeArray, pokeArray0, newArray,
newArray0, withArray, withArray0, withArrayLen, withArrayLen0,
copyArray, moveArray, lengthArray0, advancePtr
) where

The module Foreign.Marshal.Array provides operations for marshalling Haskell lists into monolithic arrays and vice versa. Most functions come in two flavours: one for arrays terminated by a special termination element and one where an explicit length parameter is used to determine the extent of an array. The typical example for the former case are C’s NUL terminated strings. However, please note that C strings should usually be marshalled using the functions provided by Foreign.C.String as the Unicode encoding has to be taken into account. All functions specifically operating on arrays that are terminated by a special termination element have a name ending on 0—e.g., mallocArray allocates space for an array of the given size, whereas mallocArray0 allocates space for one more element to ensure that there is room for the terminator.

### 32.1 Marshalling arrays

#### 32.1.1 Allocation

mallocArray :: Storable a => Int -> IO (Ptr a)

  Allocate storage for the given number of elements of a storable type (like Foreign.Marshal.Alloc.malloc, but for multiple elements).

mallocArray0 :: Storable a => Int -> IO (Ptr a)

  Like mallocArray, but add an extra position to hold a special termination element.

allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b

  Temporarily allocate space for the given number of elements (like Foreign.Marshal.Alloc.alloca, but for multiple elements).

allocaArray0 :: Storable a => Int -> (Ptr a -> IO b) -> IO b

  Like allocaArray, but add an extra position to hold a special termination element.

reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a)

  Adjust the size of an array

reallocArray0 :: Storable a => Ptr a -> Int -> IO (Ptr a)

  Adjust the size of an array including an extra position for the end marker.

#### 32.1.2 Marshalling

peekArray :: Storable a => Int -> Ptr a -> IO [a]

  Convert an array of given length into a Haskell list.

peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]

  Convert an array terminated by the given end marker into a Haskell list

pokeArray :: Storable a => Ptr a -> [a] -> IO ()

  Write the list elements consecutive into memory

pokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()

  Write the list elements consecutive into memory and terminate them with the given marker element

#### 32.1.3 Combined allocation and marshalling

newArray :: Storable a => [a] -> IO (Ptr a)

  Write a list of storable elements into a newly allocated, consecutive sequence of storable values (like Foreign.Marshal.Utils.new, but for multiple elements).

newArray0 :: Storable a => a -> [a] -> IO (Ptr a)

  Write a list of storable elements into a newly allocated, consecutive sequence of storable values, where the end is fixed by the given end marker

withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b

  Temporarily store a list of storable values in memory (like Foreign.Marshal.Utils.with, but for multiple elements).

withArray0 :: Storable a => a -> [a] -> (Ptr a -> IO b) -> IO b

  Like withArray, but a terminator indicates where the array ends

withArrayLen :: Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b

  Like withArray, but the action gets the number of values as an additional parameter

withArrayLen0 :: Storable a => a
-> [a] -> (Int -> Ptr a -> IO b) -> IO b

  Like withArrayLen, but a terminator indicates where the array ends

#### 32.1.4 Copying

(argument order: destination, source)

copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()

  Copy the given number of elements from the second array (source) into the first array (destination); the copied areas may not overlap

moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()

  Copy the given number of elements from the second array (source) into the first array (destination); the copied areas may overlap

#### 32.1.5 Finding the length

lengthArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO Int

  Return the number of elements in an array, excluding the terminator

#### 32.1.6 Indexing

advancePtr :: Storable a => Ptr a -> Int -> Ptr a

  Advance a pointer into an array by the given number of elements

[next] [prev] [prev-tail] [front] [up]