[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
6.20.5.4 Foreign Structs
Finally, one last note on foreign values before moving on to actually calling foreign functions. Sometimes you need to deal with C structs, which requires interpreting each element of the struct according to the its type, offset, and alignment. Guile has some primitives to support this.
- Scheme Procedure: sizeof type
- C Function: scm_sizeof (type)
Return the size of type, in bytes.
type should be a valid C type, like
int
. Alternately type may be the symbol*
, in which case the size of a pointer is returned. type may also be a list of types, in which case the size of astruct
with ABI-conventional packing is returned.
- Scheme Procedure: alignof type
- C Function: scm_alignof (type)
Return the alignment of type, in bytes.
type should be a valid C type, like
int
. Alternately type may be the symbol*
, in which case the alignment of a pointer is returned. type may also be a list of types, in which case the alignment of astruct
with ABI-conventional packing is returned.
Guile also provides some convenience methods to pack and unpack foreign pointers wrapping C structs.
- Scheme Procedure: make-c-struct types vals
Create a foreign pointer to a C struct containing vals with types
types
.vals and
types
should be lists of the same length.
- Scheme Procedure: parse-c-struct foreign types
Parse a foreign pointer to a C struct, returning a list of values.
types
should be a list of C types.
For example, to create and parse the equivalent of a struct {
int64_t a; uint8_t b; }
:
(parse-c-struct (make-c-struct (list int64 uint8) (list 300 43)) (list int64 uint8)) ⇒ (300 43)
As yet, Guile only has convenience routines to support
conventionally-packed structs. But given the bytevector->pointer
and pointer->bytevector
routines, one can create and parse
tightly packed structs and unions by hand. See the code for
(system foreign)
for details.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on April 20, 2013 using texi2html 5.0.