{-# LANGUAGE ForeignFunctionInterface #-}

module Ffi where

import Control.Monad
import Data.Vector.Storable as V
import Foreign hiding (unsafePerformIO)
import Foreign.C
import System.IO.Unsafe

foreign import ccall unsafe "ffi.h" nativeC :: Ptr CDouble -> CInt -> IO (Ptr CDouble)

doubleVec :: Vector Double -> Vector Double
doubleVec xs = unsafePerformIO $ do
    let (fpIn, off, len) = unsafeToForeignPtr xs
    pOut <- liftM castPtr $ withForeignPtr fpIn $ \ptr -> 
        nativeC (castPtr ptr) (fromIntegral len)
    fpOut <- newForeignPtr finalizerFree pOut
    return $ unsafeFromForeignPtr0 fpOut len
