From: simonmar Date: Tue, 21 Oct 2003 13:27:13 +0000 (+0000) Subject: [project @ 2003-10-21 13:27:13 by simonmar] X-Git-Tag: nhc98-1-18-release~465 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=a0527fc18dd66c1b6bef7d7e9f5bd7d1af51394a;p=haskell-directory.git [project @ 2003-10-21 13:27:13 by simonmar] 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). --- diff --git a/Foreign/Marshal/Array.hs b/Foreign/Marshal/Array.hs index 002afed..45ea878 100644 --- a/Foreign/Marshal/Array.hs +++ b/Foreign/Marshal/Array.hs @@ -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 --