Add an annotation for the alignment of a structure
Some types need a particular alignment to ensure that the compiler can accurately size, pack, or optimize them. One example is SIMD types, like __m128
, which should always be aligned to the 16 bytes boundaries—i.e. 4 * sizeof (float)
. Aligned types also need special handling when it comes to memory allocation on the heap, and typically require using an aligned allocation and de-allocation.
GIR should preserve this information so that bindings can use the appropriate policies when allocating aligned structures:
- dynamic languages, like Python, would be able to use allocators like C11's
aligned_alloc()
, POSIX'sposix_memalign()
, or Windows's_aligned_alloc()
to allocate the structures on the heap with the appropriate alignment as derived by the introspection data - static languages, like Rust, would be able to generate alignment annotations in the code, and tell the compiler how to align the data types
GIRepository already computes the alignment for every structure, union, and field at compile time, but bindings don't typically use this information, and assume everything works with the existing alignment. Additionally, there's nothing in the XML, for languages consuming it.
Steps
-
Add an aligned N
annotation for structures -
Add a c:aligned="N"
attribute for records -
(Maybe) If the XML has an alignment attribute, use that instead of computing it when compiling the XML into a typelib