FFI/Alien: Structures with arrrayed fields

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

FFI/Alien: Structures with arrrayed fields

Schwab,Wilhelm K
Hello all,

I am faced with something like the following:

struct complex { double real; imaginary; };
struct ugly {
        int gratuitousSize;
        long importantStuff[64];
        complex * moreImportantStuff[64];
};

The question is how to explain this to FFI?  The closest things I have found always seem to be byte arrays, so the "third element" in the arrays of #fields is not well-revealed.  Does anyone know whether it is additive or multiplicative, and/or how to use it to make sense of the above struct?

Bill


Reply | Threaded
Open this post in threaded view
|

Re: FFI/Alien: Structures with arrrayed fields

Bert Freudenberg
On 15.03.2010, at 14:29, Schwab,Wilhelm K wrote:

>
> Hello all,
>
> I am faced with something like the following:
>
> struct complex { double real; imaginary; };
> struct ugly {
> int gratuitousSize;
> long importantStuff[64];
> complex * moreImportantStuff[64];
> };
>
> The question is how to explain this to FFI?  The closest things I have found always seem to be byte arrays, so the "third element" in the arrays of #fields is not well-revealed.

Why not make an ExternalStructure subclass for "complex" and use that in the fields definition for "ugly"?


- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: FFI/Alien: Structures with arrrayed fields

Schwab,Wilhelm K
In reply to this post by Schwab,Wilhelm K
Bert,

Re your suggestion about making an external structure for "complex," please consider that done.  The question is how to then handle the array (of pointers), and even the array of longs.

Bill





-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Schwab,Wilhelm K
Sent: Monday, March 15, 2010 8:29 AM
To: [hidden email]; [hidden email]
Subject: [Pharo-project] FFI/Alien: Structures with arrrayed fields

Hello all,

I am faced with something like the following:

struct complex { double real; imaginary; }; struct ugly {
        int gratuitousSize;
        long importantStuff[64];
        complex * moreImportantStuff[64];
};

The question is how to explain this to FFI?  The closest things I have found always seem to be byte arrays, so the "third element" in the arrays of #fields is not well-revealed.  Does anyone know whether it is additive or multiplicative, and/or how to use it to make sense of the above struct?

Bill


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply | Threaded
Open this post in threaded view
|

Re: FFI/Alien: Structures with arrrayed fields

Bert Freudenberg
On 15.03.2010, at 16:33, Schwab,Wilhelm K wrote:
>
> Bert,
>
> Re your suggestion about making an external structure for "complex," please consider that done.  The question is how to then handle the array (of pointers), and even the array of longs.
>
> Bill

Ah. Well given this definition:

fields
        "self defineFields"
        ^#(
                (gratuitousSize 'long')
                (importantStuff 'long*' 256) "64 * 4"
                (moreImportantStuff 'ComplexStructure*' 256) "64 * 4"
        )

then FFI only defines the accessors to the first item:

importantStuff
        "This method was automatically generated"
        ^ExternalData fromHandle: (handle pointerAt: 5) type: ExternalType long asPointerType

moreImportantStuff
        "This method was automatically generated"
        ^ComplexStructure fromHandle: (handle pointerAt: 261)

You need to write the array accessors yourself:

importantStuffAt: index
        (index between: 0 and: 63)
                ifFalse: [^ self errorSubscriptBounds: index].
        ^ExternalData fromHandle: (handle pointerAt: index * 4 + 5) type: ExternalType long asPointerType

moreImportantStuffAt: index
        (index between: 0 and: 63)
                ifFalse: [^ self errorSubscriptBounds: index].
        ^ComplexStructure fromHandle: (handle pointerAt: index * 4 + 261)

(that's assuming you like 0-based indices for FFI calls - would be easy to change here).

HTH.

- Bert -

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Schwab,Wilhelm K
> Sent: Monday, March 15, 2010 8:29 AM
> To: [hidden email]; [hidden email]
> Subject: [Pharo-project] FFI/Alien: Structures with arrrayed fields
>
> Hello all,
>
> I am faced with something like the following:
>
> struct complex { double real; imaginary; }; struct ugly {
> int gratuitousSize;
> long importantStuff[64];
> complex * moreImportantStuff[64];
> };
>
> The question is how to explain this to FFI?  The closest things I have found always seem to be byte arrays, so the "third element" in the arrays of #fields is not well-revealed.  Does anyone know whether it is additive or multiplicative, and/or how to use it to make sense of the above struct?
>
> Bill


Reply | Threaded
Open this post in threaded view
|

Re: FFI/Alien: Structures with arrrayed fields

Bert Freudenberg
On 15.03.2010, at 17:09, Bert Freudenberg wrote:

>
> On 15.03.2010, at 16:33, Schwab,Wilhelm K wrote:
>>
>> Bert,
>>
>> Re your suggestion about making an external structure for "complex," please consider that done.  The question is how to then handle the array (of pointers), and even the array of longs.
>>
>> Bill
>
> Ah. Well given this definition:
>
> fields
> "self defineFields"
> ^#(
> (gratuitousSize 'long')
> (importantStuff 'long*' 256) "64 * 4"
> (moreImportantStuff 'ComplexStructure*' 256) "64 * 4"
> )
>
> then FFI only defines the accessors to the first item:
>
> importantStuff
> "This method was automatically generated"
> ^ExternalData fromHandle: (handle pointerAt: 5) type: ExternalType long asPointerType
>
> moreImportantStuff
> "This method was automatically generated"
> ^ComplexStructure fromHandle: (handle pointerAt: 261)
>
> You need to write the array accessors yourself:
>
> importantStuffAt: index
> (index between: 0 and: 63)
> ifFalse: [^ self errorSubscriptBounds: index].
> ^ExternalData fromHandle: (handle pointerAt: index * 4 + 5) type: ExternalType long asPointerType
>
> moreImportantStuffAt: index
> (index between: 0 and: 63)
> ifFalse: [^ self errorSubscriptBounds: index].
> ^ComplexStructure fromHandle: (handle pointerAt: index * 4 + 261)
>
> (that's assuming you like 0-based indices for FFI calls - would be easy to change here).
>
> HTH.
>
> - Bert -


Meh. For a long instead long* it's of course

importantStuffAt: index
        (index between: 0 and: 63)
                ifFalse: [^ self errorSubscriptBounds: index].
        ^handle signedLongAt: index * 4 + 5

But I guess you got my point - let FFI generate the accessors for the first item, define the rest yourself based on that one. These array accessors could of course be generated automatically if FFI knew this was an array, but it does not - it just knows it's supposed to leave some more space between fields. And given that it's easy enough to add when you need it that's okay I think.

- Bert -

>
>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]] On Behalf Of Schwab,Wilhelm K
>> Sent: Monday, March 15, 2010 8:29 AM
>> To: [hidden email]; [hidden email]
>> Subject: [Pharo-project] FFI/Alien: Structures with arrrayed fields
>>
>> Hello all,
>>
>> I am faced with something like the following:
>>
>> struct complex { double real; imaginary; }; struct ugly {
>> int gratuitousSize;
>> long importantStuff[64];
>> complex * moreImportantStuff[64];
>> };
>>
>> The question is how to explain this to FFI?  The closest things I have found always seem to be byte arrays, so the "third element" in the arrays of #fields is not well-revealed.  Does anyone know whether it is additive or multiplicative, and/or how to use it to make sense of the above struct?
>>
>> Bill
>
>



Reply | Threaded
Open this post in threaded view
|

RE: FFI/Alien: Structures with arrrayed fields

Schwab,Wilhelm K
In reply to this post by Schwab,Wilhelm K
Bert,

So the "third element" is the field's (array's) size in bytes, right?

Thanks!!!!

Bill


Reply | Threaded
Open this post in threaded view
|

RE: FFI/Alien: Structures with arrrayed fields

Bert Freudenberg
On 15.03.2010, at 17:36, Schwab,Wilhelm K wrote:
>
> Bert,
>
> So the "third element" is the field's (array's) size in bytes, right?
>
> Thanks!!!!
>
> Bill

Yes.

- Bert -