[project @ 2003-10-21 13:27:13 by simonmar]
authorsimonmar <unknown>
Tue, 21 Oct 2003 13:27:13 +0000 (13:27 +0000)
committersimonmar <unknown>
Tue, 21 Oct 2003 13:27:13 +0000 (13:27 +0000)
Make peekArray0 run in constant stack-space by testing the length of
the array first, then calling peekArray (which works backwards from
the end so it can be tail-recursive).

Foreign/Marshal/Array.hs

index 002afed..45ea878 100644 (file)
@@ -136,13 +136,9 @@ peekArray size ptr | size <= 0 = return []
 -- |Convert an array terminated by the given end marker into a Haskell list
 --
 peekArray0            :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
-peekArray0 marker ptr  = loop 0
-  where
-    loop i = do
-        val <- peekElemOff ptr i
-        if val == marker then return [] else do
-            rest <- loop (i+1)
-            return (val:rest)
+peekArray0 marker ptr  = do
+  size <- lengthArray0 marker ptr
+  peekArray size ptr
 
 -- |Write the list elements consecutive into memory
 --