<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -2,13 +2,15 @@
 
 module System.Event.Array
     ( Array,
-      new,
       empty,
+      new,
+      length,
+      capacity,
       unsafeRead,
       unsafeWrite,
+      unsafeLoad,
       ensureCapacity,
       useAsPtr,
-      length,
       snoc,
       mapM_
     ) where
@@ -37,62 +39,76 @@ newtype Array a = Array (IORef (AC a))
 -- The actual array content.
 data AC a = AC
     !(Ptr a)  -- Elements
-    !Int      -- Size
-    !Int      -- Capacity
+    !Int      -- Number of elements (length)
+    !Int      -- Maximum number of elements (capacity)
+
+empty :: IO (Array a)
+empty = fmap Array (newIORef (AC nullPtr 0 0))
 
 new :: Storable a =&gt; Int -&gt; IO (Array a)
 new cap = do
-    ptr &lt;- mallocArray cap
-    ref &lt;- newIORef (AC ptr 0 cap)
-    return $ Array ref
+    es &lt;- mallocArray cap
+    fmap Array (newIORef (AC es 0 cap))
 
-empty :: IO (Array a)
-empty = fmap Array (newIORef (AC nullPtr 0 0))
+length :: Array a -&gt; IO Int
+length (Array ref) = do
+    AC _ len _ &lt;- readIORef ref
+    return len
+
+capacity :: Array a -&gt; IO Int
+capacity (Array ref) = do
+    AC _ _ cap &lt;- readIORef ref
+    return cap
 
 unsafeRead :: Storable a =&gt; Array a -&gt; Int -&gt; IO a
 unsafeRead (Array ref) ix = do
-    AC ptr _ cap &lt;- readIORef ref
+    AC es _ cap &lt;- readIORef ref
     CHECK_BOUNDS(&quot;unsafeRead&quot;,cap,ix)
-      peekElemOff ptr ix
+      peekElemOff es ix
 
 unsafeWrite :: Storable a =&gt; Array a -&gt; Int -&gt; a -&gt; IO ()
 unsafeWrite (Array ref) ix a = do
-    AC ptr _ cap &lt;- readIORef ref
+    AC es _ cap &lt;- readIORef ref
     CHECK_BOUNDS(&quot;unsafeWrite&quot;,cap,ix)
-      pokeElemOff ptr ix a
+      pokeElemOff es ix a
+
+unsafeLoad :: Storable a =&gt; Array a -&gt; (Ptr a -&gt; Int -&gt; IO Int) -&gt; IO Int
+unsafeLoad (Array ref) load = do
+    AC es _ cap &lt;- readIORef ref
+    len' &lt;- load es cap
+    writeIORef ref (AC es len' cap)
+    return len'
 
 ensureCapacity :: Storable a =&gt; Array a -&gt; Int -&gt; IO ()
-ensureCapacity (Array ref) newSize = do
-    AC ptr sz cap &lt;- readIORef ref
-    when (newSize &gt; cap) $ do
-        let cap' = newCap cap
-        ptr' &lt;- reallocArray ptr cap'
-        writeIORef ref (AC ptr' sz cap')
+ensureCapacity (Array ref) c = do
+    AC es len cap &lt;- readIORef ref
+    when (c &gt; cap) $ do
+        es' &lt;- reallocArray es cap'
+        writeIORef ref (AC es' len cap')
   where
-    newCap 0      = 64
-    newCap oldCap = 2 * oldCap
+    cap' | c == 0    = 64
+         | otherwise = (ceiling . logBase (2 :: Double) . realToFrac) c
 
 useAsPtr :: Array a -&gt; (Ptr a -&gt; Int -&gt; IO b) -&gt; IO b
 useAsPtr (Array ref) f = do
-    AC ptr _ cap &lt;- readIORef ref
-    f ptr cap
-
-length :: Array a -&gt; IO Int
-length (Array ref) = do
-    AC _ sz _ &lt;- readIORef ref
-    return sz
+    AC es _ cap &lt;- readIORef ref
+    f es cap
 
 snoc :: Storable a =&gt; Array a -&gt; a -&gt; IO ()
-snoc arr element = do
-    sz &lt;- length arr
-    ensureCapacity arr (sz + 1)
-    unsafeWrite arr sz element
+snoc arr@(Array ref) e = do
+    len &lt;- length arr
+    let len' = succ len
+    ensureCapacity arr len'
+    unsafeWrite arr len e
+    AC es _ cap &lt;- readIORef ref
+    writeIORef ref (AC es len' cap)
 
 mapM_ :: Storable a =&gt; Array a -&gt; (a -&gt; IO ()) -&gt; IO ()
 mapM_ (Array ref) f = do
-    AC ptr sz _ &lt;- readIORef ref
-    let loop n | n == sz = return ()
-               | otherwise = do
-                     peek (ptr `plusPtr` n) &gt;&gt;= f
-                     loop (n + 1)
+    AC es len _ &lt;- readIORef ref
+    let loop n
+            | n == len = return ()
+            | otherwise = do
+                  peek (es `plusPtr` n) &gt;&gt;= f
+                  loop (succ n)
     loop 0</diff>
      <filename>src/System/Event/Array.hs</filename>
    </modified>
    <modified>
      <diff>@@ -142,11 +142,13 @@ poll ep f = do
     let epfd   = epollEpfd   ep
     let events = epollEvents ep
 
-    A.useAsPtr events $ \eventsPtr eventsLen -&gt; do
-        n &lt;- epollWait epfd eventsPtr eventsLen maxNumMilliseconds
-        when (n &gt; 0) $ putStrLn &quot;events!&quot;
-        when (n == eventsLen) $ A.ensureCapacity events (2 * eventsLen)
-        A.mapM_ events $ \e -&gt; f (eventFd e) []
+    n &lt;- A.unsafeLoad events $ \es cap -&gt;
+         epollWait epfd es cap maxNumMilliseconds
+
+    cap &lt;- A.capacity events
+    when (n == cap) $ A.ensureCapacity events (2 * cap)
+
+    A.mapM_ events $ \e -&gt; f (eventFd e) []
   where
     maxNumMilliseconds = 1000
 </diff>
      <filename>src/System/Event/EPoll.hsc</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>35d1e2d28229456ac7d0b221b057b16fc8c54730</id>
    </parent>
  </parents>
  <author>
    <name>Brian Lewis</name>
    <email>brian@lorf.org</email>
  </author>
  <url>http://github.com/tibbe/event/commit/b2e3682dc69e52e3b8bcec1409199702b33deec4</url>
  <id>b2e3682dc69e52e3b8bcec1409199702b33deec4</id>
  <committed-date>2009-08-09T16:52:36-07:00</committed-date>
  <authored-date>2009-08-09T16:52:36-07:00</authored-date>
  <message>Array.hs cleanup</message>
  <tree>af1d61df258a22b464a565f5c404337f84d53fb9</tree>
  <committer>
    <name>Brian Lewis</name>
    <email>brian@lorf.org</email>
  </committer>
</commit>
