Re: [racket-dev] [racket] newbie: foreign C structure definition question

2010-11-18 Thread Eli Barzilay
[redirected to dev again]

15 minutes ago, Hendrik Boom wrote:
 On Tue, Nov 16, 2010 at 11:37:17PM -0500, Eli Barzilay wrote:
  Three minutes ago, Jay McCarthy wrote:
   We could use a syntax parameter in define-cstruct to communicate to
   the vector syntax to give them the correct size in structs and make
   my macro's approach the normal behavior.
  
  * Types in a cstruct are plain expressions (that should evaluate to
ctype values).
  
  * Arrays should be available as a type outside of cstructs too.
 
 An array of, say, ten elements should be passes the same way as a
 struct with the same ten elements.

Yes, what I said earlier is that it really boils down to being able to
define a libffi type descriptor with some manually specified size and
alignment.  It's also how the current hackarounds all work.


 That C doesn't do this is a flaw in its calling interface.

And that actually is another problem for Sam when he implements this
-- dealing with whatever situations make C expect the array to be
passed by value (a real one, with the whole array on the stack).


 But given that it's possible to pass references, it should be
 possible to pass references to C arrays when the C funtion is coded
 as if it expects an array (when, of course, it really expects a
 reference).

Right, this works assuming sane C, and IIRC, with small arrays it can
expect the whole array to be on the stack.  (But maybe the rules for
structs are the same, so it works out by size only.)

-- 
  ((lambda (x) (x x)) (lambda (x) (x x)))  Eli Barzilay:
http://barzilay.org/   Maze is Life!
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/dev


Re: [racket-dev] [racket] newbie: foreign C structure definition question

2010-11-18 Thread Hendrik Boom
On Thu, Nov 18, 2010 at 10:06:34PM -0500, Eli Barzilay wrote:
 [redirected to dev again]
 
 15 minutes ago, Hendrik Boom wrote:
  On Tue, Nov 16, 2010 at 11:37:17PM -0500, Eli Barzilay wrote:
   Three minutes ago, Jay McCarthy wrote:
We could use a syntax parameter in define-cstruct to communicate to
the vector syntax to give them the correct size in structs and make
my macro's approach the normal behavior.
   
   * Types in a cstruct are plain expressions (that should evaluate to
 ctype values).
   
   * Arrays should be available as a type outside of cstructs too.
  
  An array of, say, ten elements should be passes the same way as a
  struct with the same ten elements.
 
 Yes, what I said earlier is that it really boils down to being able to
 define a libffi type descriptor with some manually specified size and
 alignment.  It's also how the current hackarounds all work.
 
 
  That C doesn't do this is a flaw in its calling interface.
 
 And that actually is another problem for Sam when he implements this
 -- dealing with whatever situations make C expect the array to be
 passed by value (a real one, with the whole array on the stack).
 
 
  But given that it's possible to pass references, it should be
  possible to pass references to C arrays when the C funtion is coded
  as if it expects an array (when, of course, it really expects a
  reference).
 
 Right, this works assuming sane C, and IIRC, with small arrays it can
 expect the whole array to be on the stack.  (But maybe the rules for
 structs are the same, so it works out by size only.)

C defines that when an array is the actual parameter, the address of it 
is passed instead.  It depends on the fact that the type of the 
argunment is an array and nothing else.  If you pass a one-element array 
of integer (note the singular), you're supposed to get its address 
instead of the array itself.

Structures are passed in their entirety.

Of course, some compilers when functions with prototypes are called, may 
do incredibly convoluted things instead to attain greater efficiency.  
FFI would presumably have to match that complexity in terms of hardware 
register hacking.

-- hendrik
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/dev


Re: [racket-dev] [racket] newbie: foreign C structure definition question

2010-11-17 Thread Sam Tobin-Hochstadt
On Wed, Nov 17, 2010 at 2:12 AM, Eli Barzilay e...@barzilay.org wrote:
 Two hours ago, Sam Tobin-Hochstadt wrote:

 Python's 'ctypes' library, which also uses libffi under the hood, has arrays:
   http://docs.python.org/library/ctypes.html#arrays
 and also unions, which we don't have:
   http://docs.python.org/library/ctypes.html#structures-and-unions

 So it seems like this should be possible, and maybe we can even use
 their code.

 They guy who did ctypes knows how to deal with the libffi internals.
 (He did the msvc version of the code.)

 If you look through the libffi documentation you'll see no mention of
 arrays, and an explicit mention of no special support for unions.

I understand that, but ctypes proves 3 things
 - it can be done
 - there is code to read and people to ask to figure out how
 - Python(!) has a nicer FFI than us for this.
-- 
sam th
sa...@ccs.neu.edu
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/dev


Re: [racket-dev] [racket] newbie: foreign C structure definition question

2010-11-17 Thread Eli Barzilay
6 hours ago, Jay McCarthy wrote:
 I may be parsing your responses wrong Eli, but I don't think we
 should let our use of libffi be a barrier to a good ffi. I see
 (require ffi) to mean get a good Racket FFI, not get a Racket
 encoding of libffi.

Of course.  I even detailed a number of times how it could be done:
what you need is to find a hook in libffi that allows you to create
new types with a manually specified size and alignment, then use that
from the racket side.  That does require some digging, but it sounds
like Sam has already done some, so you just need to wait for a patch
from him.

-- 
  ((lambda (x) (x x)) (lambda (x) (x x)))  Eli Barzilay:
http://barzilay.org/   Maze is Life!
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/dev


Re: [racket-dev] [racket] newbie: foreign C structure definition question

2010-11-16 Thread Sam Tobin-Hochstadt
[redirected to d...@]

On Tue, Nov 16, 2010 at 11:28 PM, Eli Barzilay e...@barzilay.org wrote:
 20 minutes ago, Jay McCarthy wrote:

 I've complained about this before, which is why I was able to
 quickly answer your question. I don't endorse the current behavior.

 (Just a reminder: this is something that libffi doesn't have AFAICT.)

Python's 'ctypes' library, which also uses libffi under the hood, has arrays:
  http://docs.python.org/library/ctypes.html#arrays
and also unions, which we don't have:
  http://docs.python.org/library/ctypes.html#structures-and-unions

So it seems like this should be possible, and maybe we can even use their code.
-- 
sam th
sa...@ccs.neu.edu
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/dev