Array operations and pinning

2005-11-02 Thread Rene de Visser

Hello,

Where is the documentation on how pinning works in the GHC garbage collector 
(from a GHC users point of view).


I have copied the following code from array/IO.hs and am thinking that it is 
assuming that the array is pinned? What triggers the pinning?


On a second note.
Why is the type signiture so constricted. The code below works on any 
IOUArray (which is very usefull, not just on Int Word8). Naturally this 
assumes the particular in memory array layout that GHC uses on a particular 
platform, so would not be compatible (probably) with other Haskell 
compilers.


Rene.

hPutArray
:: Handle   -- ^ Handle to write to
- IOUArray Int Word8-- ^ Array to write from
- Int   -- ^ Number of 'Word8's to write
- IO ()

hPutArray handle (IOUArray (STUArray l u raw)) count
 | count == 0
 = return ()
 | count  0 || count  rangeSize (l,u)
 = illegalBufferSize handle hPutArray count
 | otherwise
  = do wantWritableHandle hPutArray handle $
 \ [EMAIL PROTECTED] haFD=fd, haBuffer=ref, haIsStream=stream } - 
do


 [EMAIL PROTECTED] bufBuf=old_raw, bufRPtr=r, bufWPtr=w, bufSize=size 
}

- readIORef ref

 -- enough room in handle buffer?
 if (size - w  count)
-- There's enough room in the buffer:
-- just copy the data in and update bufWPtr.
then do memcpy_baoff_ba old_raw w raw (fromIntegral count)
writeIORef ref old_buf{ bufWPtr = w + count }
return ()

-- else, we have to flush
else do flushed_buf - flushWriteBuffer fd stream old_buf
writeIORef ref flushed_buf
let this_buf =
Buffer{ bufBuf=raw, bufState=WriteBuffer,
bufRPtr=0, bufWPtr=count, bufSize=count }
flushWriteBuffer fd stream this_buf
return ()


___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


RE: Array operations and pinning

2005-11-02 Thread Simon Marlow
On 02 November 2005 11:15, Rene de Visser wrote:

 Where is the documentation on how pinning works in the GHC garbage
 collector (from a GHC users point of view).
 
 I have copied the following code from array/IO.hs and am thinking
 that it is assuming that the array is pinned? What triggers the
 pinning? 

Actually this code does not assume that any memory is pinned.  It is ok
to pass the underlying ByteArr# directly to C, as long as the C call is
annotated unsafe, which means that GC cannot happen while the call is
running.

If you want to pass ByteArr# to a safe C call, then you have to
allocate the ByteArr# using newPinnedByteArray#.  This is the only way
to get a pinned object in GHC, and the only kind of pinned object that
is supported is a MutByteArr# or ByteArr# (this is to simplify the GC;
it doesn't need to traverse pinned objects because they don't contain
any pointers, all it needs to do is remember that the memory block they
occupy is still alive).

Note that all this is GHC-specific; the right high-level interface to
allocating pinned memory is mallocForeignPtr.

 On a second note.
 Why is the type signiture so constricted. The code below works on any
 IOUArray (which is very usefull, not just on Int Word8). Naturally
 this assumes the particular in memory array layout that GHC uses on a
 particular platform, so would not be compatible (probably) with other
 Haskell compilers.

I think the type is right - it makes it clear that the representation
being written to the file is an array of bytes.  You can use
castIOUArray, although that isn't ideal (it doesn't change the bounds).
We should do something better here.

Cheers,
Simon
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users