Creates an FFI structure type from named field types. By default, libffi
uses natural alignment (each field aligned to its size). Use the .pack
parameter to specify tighter packing similar to #pragma pack(n) in C.
Packing
The .pack parameter affects ffi_offsetof(), ffi_sizeof(),
ffi_get_field(), and ffi_set_field().
Packed Structs By Value
Packed structs cannot be passed by value - GCC compiles functions taking
__attribute__((packed)) structs to expect arguments on the stack, but
libffi passes via registers. Use pointers instead.
Examples
# Natural alignment (default)
Point <- ffi_struct(x = ffi_int(), y = ffi_int())
# Packed struct (1-byte alignment)
PackedData <- ffi_struct(
flag = ffi_uint8(),
value = ffi_int32(),
.pack = 1
)
# Check sizes
ffi_sizeof(Point) # Natural size
#> [1] 8
ffi_sizeof(PackedData) # Packed size (smaller)
#> [1] 5
# Note: Packed structs cannot be passed by value to C functions.
# Use pointers instead:
# ffi_function("some_func", ffi_void(), ffi_pointer())