Issue #131 resolved

Allow structure alignment specification when using ABI (similar to ctypes' _pack_ attribute)

Nick Vatamaniuc
created an issue

It is not uncommon for libraries, especially the ones that serialize and serialize data internally and send it over a socket, to constrain alignment of structs exposed in its API.

When using CFFI in ABI mode, it would be useful to let user specify structure packing modes. For example, ctypes accomplishes this with _pack_ = 1 attribute.

http://docs.python.org/2/library/ctypes.html#structure-union-alignment-and-byte-order

Microsoft compiler understand a #pragma directive that can enable packing mode for a set of structures. gcc has __attribute__((packed)).

Ideally c parser would automatically parse those during cdef call.

If not at least having an option during structure creation in new() method. For example:

inst_s = ffi.new("struct s*", pack=1) 

Comments (3)

  1. Armin Rigo

    We can't parse these easily without hacking more at pycparser, but we could have a Python-level keyword. Either:

    ffi.cdef("""struct s { .. }; struct t { ... };""", pack=1)
    

    with the "pack" keyword having an effect on all structs in this call to cdef() (you can call cdef several times); or:

    ffi.cdef("""struct s { ,, }; struct t { .. };""")
    ffi.pack_structure("struct s")
    ffi.pack_structure("struct t")
    
  2. Log in to comment