Let's get this finished

Manuel M. T. Chakravarty chak at cse.unsw.edu.au
Thu Jan 4 02:27:49 EST 2001

The major piece missing from the FFI library is the
high-level marshalling support.  Moreover, `MarshalUtils'
isn't quite there.  In fact, I think, we should have a
family of MarshalXXX modules, which include what currently
is in MarshalUtils plus some more stuff.  

The reason that I don't like MarshalUtils as it is are the

(1) It isn't H98 and in fact its interface cannot be
    implemented with H98[1].  The problem are the routines
    with signatures in patterns:

      malloc :: Storable a => IO (Ptr a)
      malloc :: IO (Ptr a) = mallocBytes (sizeOf (undefined::a))

      alloca :: Storable a => (Ptr a -> IO b) -> IO b
      alloca (act :: Ptr a -> IO b) = allocaBytes (sizeOf (undefined::a)) act


(2) pokeArray, peekArray, etc are really part of what should
    go into the high-level marshalling.

Point (2) is easily resolved by defining a family of modules
including all the necessary functionality.  However, Point
(1) is a problem.  First of all, I guess, we have to revert
to the signature for allocation that I originally used in
the C2HS library:

  malloc       :: Storable a => a -> IO Addr
  malloc dummy  = mallocBytes (sizeOf dummy)

All the other allocation routines have to be altered in a
similar way.  A similar problem arises for copyArray and
moveArray, where I guess the additional argument is in
practice more awkward as for the allocation routines.
Finally, indexPtr is affected, which is especially annoying
since it can't be used infix anymore after the change.

The Marshal modules
I propose the following set of modules:

- MarshalAlloc: 
    The allocation routines that are in MarshalUtils now,
    but modified as outlined above.

- MarshalArray: 
    The array marshalling routines

- MarshalString: 
    Most people seem to be of the opinion that we have to
    handle strings specially due to Unicode conversion
    requirements.  So, this is were string marshalling goes.

- MarshalError: 
    Combinators to test for common error conditions.  The
    main open problem here is the handling of errno, I

- MarshalConv: 
    The C2HS IntConv, BoolConv, etc classes.  Marcin just
    uses `fromIntegral' here, but I think that this is too
    limited - eg, it doesn't handle Bool.  Moreover,
    soemthing along the C2HS ToAddr/FromAddr classes, but it
    probably makes to have `stdArrayPtr' etc as additional
    member functions (this is similar to the newXXX routines
    that are in MarshalUtils now, but with additional
    overloading).  Furthermore, Marcin-syle withXXX routines
    would fo here.

- MarshalUtils: 
    The memcpy/memmove stuff, indexPtr (if it still makes
    sense), and whatever else doesn't get its own module.

I think, we should leave C2HS's marshX routines out.  We
discussed how to make them arity independent for quite a
while and AFAIR, there wasn't a really good proposal.
Finally, all the above modules should be jointly exported by
a module `Marshal'.



[1] Which unfortunately escaped me when we discussed MarshalUtils
    for the first time.

More information about the FFI mailing list