[plt-scheme] Eli Barzilay's new FFI stuff

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Aug 7 16:50:18 EDT 2004

On Aug  7, Christopher Armstrong wrote:
> On Sat, 7 Aug 2004 02:33:49 -0400, Eli Barzilay <eli at barzilay.org> wrote:
> > Now, note that since this is specifying simple memory layouts, you
> > could in many cases fake it in the same way that you can do it in C:
> > 
> >   (define-cstruct _substructname
> >     ([super _structname]
> >      [y _type]))
> > 
> > The disadvantage is that to access the `x' slot of an instance of
> > substructname, you have to use structname-x, which is confusing -- but
> > this is fine because define-struct does just this. 
> 
> Well, that alone doesn't bother me, but in addition to this, it
> seems I *also* have to ptr-ref the struct to the type that the slot
> is defined on, which is frustrating.
> 
> (super-slot (ptr-ref obj  _super))
> 
> It's frustrating because I have really long slot and type names, and
> that I'm duplicating the type-name in the same expression. Should I
> just suck it up?

Definitely not!  It's not the names which are a problem -- it's the
fact that the ptr-ref will copy the contained substructure instance.
But this is only a problem that is imposed by the pointer tags, which
is the technical problem I mentioned in the previous email.


> > The reason I said "many cases" is that I'm not sure if there can
> > some alignment problems that will make `foo' have different
> > layouts if you use:
> 
> Well, I asked some people knowledgeable in C and they said that
> there shouldn't be any alignment issues, and that their equivalence
> is defined by the ANSI standard...? If this is the case, then I
> definitely don't need any special support in define-cstruct for
> substructing; this is quite tolerable.

OK, I'll see if I can hack something quick.


On Aug  7, Christopher Armstrong wrote:
> I found another problem: I've got a string object struct that
> defines a string with 'size' and 'val' fields, where the val field
> is just defined as 'char sval[1];' in the real struct definition.

You mean that the string is contained in the struct instead of just a
pointer to it?


> I figured make-sized-byte-string would be the best way to extract
> the string, but I don't have the pointer to the beginning of the
> string necessary to pass to that function. Maybe I'm going about
> this the wrong way, but if I'm not, would it make sense for there to
> be 'type-slot-pointer' functions generated in addition to the
> 'type-slot' functions, with define-cstruct? Or maybe I didn't notice
> some other way to get the pointer to the start of the struct
> (without doing my own pointer arithmetic...).

Currently there is no reasonable way to do pointer arithmetic for the
same reason that a `mystring-sval-pointer' is a bad idea -- it will
give you a pointer to the middle of an object, but the GC will have
problems with something like that.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.