) where
import GlaExts
-import ST
import PrelIOBase
-import PrelHandle (readHandle, writeHandle, newHandle, getBMode__, getHandleFd )
+import PrelHandle (newHandle, getBMode__, getHandleFd,
+ freeFileObject, freeStdFileObject )
import IO
-import PackedString ( unpackPS, unsafeByteArrayToPS, psToByteArrayST )
import Addr
import Foreign
+import Weak ( addForeignFinaliser )
+import CString ( freeze, allocChars, packStringIO, unpackNBytesBAIO )
import PosixUtil
import PosixFiles ( stdInput, stdOutput, stdError )
(or as a result of) program termination.
-}
#ifndef __PARALLEL_HASKELL__
- fo <-
- (if fd == stdInput || fd == stdOutput || fd == stdError then
- makeForeignObj fo (``&freeStdFile''::Addr)
- else
- makeForeignObj fo (``&freeFileObject''::Addr))
+ fo <- makeForeignObj fo
+ if fd == stdInput || fd == stdOutput || fd == stdError then
+ addForeignFinaliser fo (freeStdFileObject fo)
+ else
+ addForeignFinaliser fo (freeFileObject fo)
#endif
(bm, bf_size) <- getBMode__ fo
mkBuffer__ fo bf_size
0 -> fail (IOError Nothing EOF "fdRead" "EOF")
n | n == nbytes -> do
buf <- freeze bytes
- return (unpackPS (unsafeByteArrayToPS buf n), n)
+ s <- unpackNBytesBAIO buf n
+ return (s, n)
| otherwise -> do
-- Let go of the excessively long ByteArray# by copying to a
-- shorter one. Maybe we need a new primitive, shrinkCharArray#?
_casm_ ``do {I_ i; for(i = 0; i < %2; i++) ((B_)%0)[i] = ((B_)%1)[i];
} while(0);'' bytes' bytes n
buf <- freeze bytes'
- return (unpackPS (unsafeByteArrayToPS buf n), n)
+ s <- unpackNBytesBAIO buf n
+ return (s, n)
fdWrite :: Fd -> String -> IO ByteCount
fdWrite fd str = do
- buf <- stToIO (psToByteArrayST str)
+ buf <- packStringIO str
rc <- _ccall_ write fd buf (length str)
if rc /= -1
then return rc
WriteLock -> ``F_WRLCK''
Unlock -> ``F_UNLCK''
-lock2Bytes :: FileLock -> IO (MutableByteArray RealWorld ())
+lock2Bytes :: FileLock -> IO (MutableByteArray RealWorld Int)
lock2Bytes (kind, mode, start, len) = do
bytes <- allocChars ``sizeof(struct flock)''
_casm_ ``do { struct flock *fl = (struct flock *)%0;
return bytes
-- where
-bytes2ProcessIDAndLock :: MutableByteArray s () -> IO (ProcessID, FileLock)
+bytes2ProcessIDAndLock :: MutableByteArray s Int -> IO (ProcessID, FileLock)
bytes2ProcessIDAndLock bytes = do
ltype <- _casm_ ``%r = ((struct flock *)%0)->l_type;'' bytes
lwhence <- _casm_ ``%r = ((struct flock *)%0)->l_whence;'' bytes