Hello,
I'm using cffi for trying to revive the Qt bindings. Doing so I found out that for the calculation of the size of struct types, the alignment of the struct itself is not taking into account. For the following struct for example
(defcstruct smoke-class (class-name string) (parents index) (class-fn :pointer) (enum-fn :pointer) (flags :unsigned-short))
cffi calculates a size of 18, which should be 20 (please correct me if I'm wrong). Attached is a patch which defines an extra method for foreign-type-size for a foreign-struct-type argument. This method is used to apply adjust-for-alignment on the newly defined struct.
Greetings, Wilco
On 4/ago/2005, at 13:33, Wilco Greven wrote:
I'm using cffi for trying to revive the Qt bindings. Doing so I found out that for the calculation of the size of struct types, the alignment of the struct itself is not taking into account. For the following struct for example
What should the alignment of a struct type be? I think it depends on the ABI and I think right now they are being aligned as if they were void* pointers, that doesn't sound right.
(defcstruct smoke-class (class-name string) (parents index) (class-fn :pointer) (enum-fn :pointer) (flags :unsigned-short))
Is index a struct type? If not, what is the relevance of struct alignment here?
cffi calculates a size of 18, which should be 20 (please correct me if I'm wrong). Attached is a patch which defines an extra method for foreign-type-size for a foreign-struct-type argument. This method is used to apply adjust-for-alignment on the newly defined struct.
Maybe I'm missing something, why should the struct's size be influenced by its own alignment requirements?
Op donderdag 04 augustus 2005 20:37, schreef Luis Oliveira:
On 4/ago/2005, at 13:33, Wilco Greven wrote:
I'm using cffi for trying to revive the Qt bindings. Doing so I found out that for the calculation of the size of struct types, the alignment of the struct itself is not taking into account. For the following struct for example
What should the alignment of a struct type be? I think it depends on the ABI and I think right now they are being aligned as if they were void* pointers, that doesn't sound right.
(defcstruct smoke-class (class-name string) (parents index) (class-fn :pointer) (enum-fn :pointer) (flags :unsigned-short))
Is index a struct type? If not, what is the relevance of struct alignment here?
The example I gave was rather bad. Let me give a better one.
typedef struct { int a; short b; } TestStruct;
(defcstruct test-struct (a :int) (e :short))
The problem arose when I wanted to access elements in an array of structs. Say I have an array "TestStruct *tests". I expected to be able to access the individual elements of this array by
(inc-ptr tests (* (foreign-struct-size 'test-struct) array-index)
The problem is that (foreign-type-size 'test-struct) returns a size of 6, while sizeof(TestStruct) returns 8. Therefore the code above doesn't work correctly.
My guess was that this had something to do with alignment of the struct, but I have to admit that my knowledge about memory alignment is pretty much nil. zero.
Wilco Greven greven@kde.org writes:
The example I gave was rather bad. Let me give a better one.
typedef struct { int a; short b; } TestStruct;
(defcstruct test-struct (a :int) (e :short))
The problem arose when I wanted to access elements in an array of structs. Say I have an array "TestStruct *tests". I expected to be able to access the individual elements of this array by
(inc-ptr tests (* (foreign-struct-size 'test-struct) array-index)
The problem is that (foreign-type-size 'test-struct) returns a size of 6, while sizeof(TestStruct) returns 8. Therefore the code above doesn't work correctly.
My guess was that this had something to do with alignment of the struct, but I have to admit that my knowledge about memory alignment is pretty much nil. zero.
Indeed this is a bug, but I probably won't have time to look at it for a little while...
Probably foreign structure types need to sum the alignment of each member to calculate the total alignment of the structure (and I think there are some wrinkles with double float alignment on Darwin also).
James
On 5/ago/2005, at 09:02, James Bielman wrote:
My guess was that this had something to do with alignment of the struct, but I have to admit that my knowledge about memory alignment is pretty much nil. zero.
Indeed this is a bug, but I probably won't have time to look at it for a little while...
Probably foreign structure types need to sum the alignment of each member to calculate the total alignment of the structure (and I think there are some wrinkles with double float alignment on Darwin also).
Yeah, the structures are being aligned as if they were pointers. Now, I don't know how the structure is supposed to be aligned. I'm told that this depends on the ABI so I'm checking out the psABI docs. Should also look into what the various open-source lisps do.
If structure alignment is the same regardless of the structure type, I suppose we could add a new operator to CFFI-SYS to tells this, but I don't know if this assumption is true at all...
I have added this to my list of known issues. :-)
On 4/ago/2005, at 13:33, Wilco Greven wrote:
I'm using cffi for trying to revive the Qt bindings. Doing so I found out that for the calculation of the size of struct types, the alignment of the struct itself is not taking into account.
First off, my appologies for taking so long to fix this. My excuse is that I wasn't able to find the relevant ABI docs when I first tried to fix this.
So, I found those docs (for x86) and my branch should a have a fix for this. Both struct alignments and sizes should be correctly calculated now.
Again, sorry for taking almost a month :-/ and thanks for your report.