Mon, 21 Feb 2000 10:20:30 +1100, Manuel M. T. Chakravarty <[EMAIL PROTECTED]> pisze:

> I prefer a simple rule like ``when you pass a ForeignObj, you have
> to take care if it can be the last reference and you don't want
> it to be deallocated'' over a complex rule like ``if a ForeignObj
> if passed in a prim type position, then ...; otherwise, ...''
> The latter just complicates matters.

On the one hand, I certainly would not be happy to add to all those 50
foreign imports that use ForeignObjs code dealing with protecting them.
There are only 3 cases where they are passed through a C array of Addrs
(I arranged them such that finalizers won't hurt).

On the other hand, if it means that a foreign import that calls back to
Haskell (in extremely rare circumstances some can) could trigger the
finalizer, which I'm not sure is always harmless in my case... Can it?
Then it's bad. Otherwise IMHO they are safe and convenient enough, the
unsafety if other cases is recognized by using foreignObjToAddr.

I seek for a safe, simple and cheap way of protecting ForeignObjs
across C calls, rarely across a bit more than a C call. The StablePtr
trick, if would be required, has a flaw: when forgotten, silently
leads to incorrect code.

A function of type (Addr -> IO a) -> (ForeignObj -> IO a) which would
do the necessary protection in whatever way would be safe when we
don't ever foreign import a function taking ForeignObj, only is not
enough because there are several patterns of arguments, with various
types in random order, up to 5 arguments. Maybe a good collection of
wrappers would do, akind C2HS's marsh*. Ugh, mere calling is still
more pleasant, if only GHC could do all the protection itself.

The drawback of marsh* is that calls are not uniform. Sometimes
marsh* are not needed, sometimes they are not sufficient (with array
and its length passed as separate arguments), initial arguments are
simpler to pass by partial application than by (return x :> forget),
and in effect various calls look differently, each using a different
shortcut. I have no presise idea how to make them better, sorry,
but I believe it's possible. It could be good if they imposed
little visual overhead for cases they do nothing special, like for
(return x :> forget). And still be as simple as possible (but no more).

-- 
 __("<    Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/              GCS/M d- s+:-- a22 C+++$ UL++>++++$ P+++ L++>++++$ E-
  ^^                  W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK                  5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-

Reply via email to