shithub: MicroHs

Download patch

ref: 4df44dba70a276f1df43882423a05fb734999f09
parent: 14c55115ad7bee634e890d0c930942d0a121a787
author: Lennart Augustsson <lennart@augustsson.net>
date: Thu Dec 21 10:48:24 EST 2023

More functions

--- a/lib/Foreign/Marshal/Array.hs
+++ b/lib/Foreign/Marshal/Array.hs
@@ -38,6 +38,12 @@
   where go []         n = pokeElemOff ptr n marker
         go (val:vals) n = do { pokeElemOff ptr n val; go vals (n + 1) }
 
+newArray :: forall a . Storable a => [a] -> IO (Ptr a)
+newArray vals  = do
+  ptr <- mallocArray (length vals)
+  pokeArray ptr vals
+  return ptr
+
 newArray0 :: forall a . Storable a => a -> [a] -> IO (Ptr a)
 newArray0 marker vals  = do
   ptr <- mallocArray0 (length vals)
@@ -50,3 +56,10 @@
     loop i = do
         val <- peekElemOff ptr i
         if val == marker then return i else loop (i+1)
+
+withArrray :: forall a b . Storable a => [a] -> (Ptr a -> IO b) -> IO b
+withArrray vals iob = do
+  ptr <- newArray vals
+  b <- iob ptr
+  free ptr
+  return b
--