> > Libraries could even use pointers instead of ForeignObj.
> 
> Currently it's indeed simpler for e.g. stat. ForeignObj provides
> nothing because struct stat is almost not used from C functions:
> 
> --------------------------------------------------------------
> ----------
> newtype FileStatus = FS (Ptr ())
> 
> getFileStatus:: FilePath -> IO FileStatus
> getFileStatus name = do
>     ptr <- autoPtrToBytes (#const sizeof (struct stat))
>     call (throwIf_1 "stat" stat) name ptr :: IO CInt
>     return (FS ptr)
> foreign import unsafe stat :: CString -> Ptr () -> IO CInt
> 
> fileOwner:: FileStatus -> UserID
> fileOwner (FS ptr) = unsafePerformIO $ hold ptr $
>     (#peek struct stat, st_uid) ptr
> --------------------------------------------------------------
> ----------
> 
> autoPtrTo* functions have the same interface as malloc* but the memory
> is freed automatically when unreachable from Haskell.

I'm not sure I understand; how does autoPtrToBytes work if it doesn't use a
ForeignObj?

Cheers,
        Simon

Reply via email to