{-# OPTIONS_HADDOCK hide #-}------------------------------------------------------------------------------- |-- Module : Data.Array.Storable.Internals-- Copyright : (c) The University of Glasgow 2011-- License : BSD-style (see the file libraries/base/LICENSE)---- Maintainer : [email protected]-- Stability : experimental-- Portability : non-portable (uses Data.Array.MArray)---- Actual implementation of "Data.Array.Storable".--------------------------------------------------------------------------------- #hidemoduleData.Array.Storable.Internals(StorableArray(..),withStorableArray,touchStorableArray,unsafeForeignPtrToStorableArray,)whereimportData.Array.BaseimportData.Array.MArrayimportForeignhiding(newArray)-- |The array typedataStorableArrayie=StorableArray!i!iInt!(ForeignPtre)instanceStorablee=>MArrayStorableArrayeIOwheregetBounds(StorableArraylu__)=return(l,u)getNumElements(StorableArray_l_un_)=returnnnewArray(l,u)initialValue=dofp<-mallocForeignPtrArraysizewithForeignPtrfp$\a->sequence_[pokeElemOffaiinitialValue|i<-[0..size-1]]return(StorableArraylusizefp)wheresize=rangeSize(l,u)unsafeNewArray_(l,u)=doletn=rangeSize(l,u)fp<-mallocForeignPtrArraynreturn(StorableArraylunfp)newArray_=unsafeNewArray_unsafeRead(StorableArray___fp)i=withForeignPtrfp$\a->peekElemOffaiunsafeWrite(StorableArray___fp)ie=withForeignPtrfp$\a->pokeElemOffaie-- |The pointer to the array contents is obtained by 'withStorableArray'.-- The idea is similar to 'ForeignPtr' (used internally here).-- The pointer should be used only during execution of the 'IO' action-- retured by the function passed as argument to 'withStorableArray'.withStorableArray::StorableArrayie->(Ptre->IOa)->IOawithStorableArray(StorableArray___fp)f=withForeignPtrfpf-- |If you want to use it afterwards, ensure that you-- 'touchStorableArray' after the last use of the pointer,-- so the array is not freed too early.touchStorableArray::StorableArrayie->IO()touchStorableArray(StorableArray___fp)=touchForeignPtrfp-- |Construct a 'StorableArray' from an arbitrary 'ForeignPtr'. It is-- the caller's responsibility to ensure that the 'ForeignPtr' points to-- an area of memory sufficient for the specified bounds.unsafeForeignPtrToStorableArray::Ixi=>ForeignPtre->(i,i)->IO(StorableArrayie)unsafeForeignPtrToStorableArrayp(l,u)=return(StorableArraylu(rangeSize(l,u))p)