[plt-scheme] FFI questions:

From: Ernie Smith (esmith at acanac.net)
Date: Wed Nov 12 17:18:22 EST 2008

I feel as though I'm missing key pieces of information
which would permit me finally to grasp the FFI documentation.

In an effort to discover the missing link I've come up with a
few questions, I'd be grateful for answers.  More importantly,
if in reading these questions it dawns on someone that I am
missing a concept please feel free to slap me into consciousness
with the necessary point.

1- Given that you create a type using this:

   (make-ctype ctype ...)

   How do you create an initialized instance of that type?

   Something along the lines of:
   (define my-instance (somehow-make-one-with-this-value 99))

   I suppose that applying the (somehow ..) would cause the conversion
   to C encoding from 99 to occur and the instance would contain the C
   encoded value?

   Then evaluating my-instance would trigger the conversion
   back to a scheme value, 99 presuming our transformation is

   If I'm on track, what are the building blocks for implementing
   (somehow-make-one..) starting from (make-ctype ..)?

2- Given that you create a struct type using this:
   (make-cstruct-type ctypes)

   How do you create an initialized instance of that structure?

   Another way of posing this question would be:
   What are the steps necessary to implement (_list-struct ) starting
   from (make-cstruct-type )?

3- Given that you create a pointer type using this:
   (define-cpointer-type _name ...)
   How do you instantiate a pointer of that type initialized
   so that it points to a valid instance of whatever the pointer
   is intended to point to.
   (for example how to instantiate a pointer such that it points to
   a field of the correct type in an existing instance of a structure)

4- There appears to be no way of declaring a vector type,
   only a vector instance.
   So when declaring a structure field which *is* a vector,
   the declaration needs to be made in the form of a substructure
   declaration with the correct number of repetitions of the single
   type.  Then access to that vector in an instance of the containing
   structure can be accomplished by applying
   (make-cvector* ...) to what is returned by the
   id-field-id accessor for that field.
   Is that a correct and reasonable approach to dealing with this issue?

5- Since all C vectors are single type like SRFI-4 vectors, it
   makes sense to always use the SRFI-4 vector for efficiency
   whenever the type matches.  Is it fair to assume that
   the (make-cvector ..) implementation does this, or is it
   necessary to hand code the selection of either
   (make-cvector ..) or the srfi-4 vector constructor depending
   on type?


Posted on the users mailing list.