Let's consider an interface to stat. How should Haskell's mirror of
struct stat look like?

An obvious answer: newtyped ForeignObj. But how to define extractors
of fields? My framework suggests

fileOwner (FS p) = unsafePerformIO $ (#peek struct stat, st_uid) p

which expands to

fileOwner (FS p) = unsafePerformIO $ ((\ptr -> peek (ptr `ptrPlusBytes` 24))) p

but it works only for Ptrs, not for ForeignObjs! There is no good way
to peek ForeignObj contents from Haskell. It would be nice to avoid
C wrappers in such cases.

class Storable could provide alternative versions of all functions,
working on ForeignObjs. This means that users would have to provide
these functions in their own Storable instances, even if they don't
use them. They are not automatically derivable from Ptr versions
because theoretically a GC could happen in a bad moment.

Using Ptr (freed with Weak.addFinalizer) instead of ForeignObj for
struct stat mirror would work here, but is dangerous if such address
is passed to a C function which calls back to Haskell. Better not
promote design that will bite in another case.

Of course casting ForeignObj to Addr is unsafe too.

Making a StablePtr just to prevent freeing ForeignObj is probably
too expensive as a general solution.

The problem really lies in the fact that Haskell functions don't have
a GC lock as most C functions do. Quite a stupid problem.

What should we do?

Maybe there should be an operation that prevents an object from
being freed for some time, as if using StablePtr, with an interface
similar to
    hold :: a -> IO b -> IO b
implemented such that in most common uses (where the action is
simple and inlined) a compiler can determine that physical locking is
really unnecessary because GC cannot happen? It would allow efficient
definitions of ForeignObj operations in terms of Ptr operations.

-- 
 __("<  Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK


Reply via email to