]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/structs/structs-docs.factor
62a3817feca954f8bdb484333398f1e7edaf6813
[factor.git] / basis / alien / structs / structs-docs.factor
1 USING: alien.c-types strings help.markup help.syntax alien.syntax
2 sequences io arrays kernel words assocs namespaces ;
3 IN: alien.structs
4
5 ARTICLE: "c-structs" "C structure types"
6 "A " { $snippet "struct" } " in C is essentially a block of memory with the value of each structure field stored at a fixed offset from the start of the block. The C library interface provides some utilities to define words which read and write structure fields given a base address."
7 { $subsection POSTPONE: C-STRUCT: }
8 "Great care must be taken when working with C structures since no type or bounds checking is possible."
9 $nl
10 "An example:"
11 { $code
12     "C-STRUCT: XVisualInfo"
13     "    { \"Visual*\" \"visual\" }"
14     "    { \"VisualID\" \"visualid\" }"
15     "    { \"int\" \"screen\" }"
16     "    { \"uint\" \"depth\" }"
17     "    { \"int\" \"class\" }"
18     "    { \"ulong\" \"red_mask\" }"
19     "    { \"ulong\" \"green_mask\" }"
20     "    { \"ulong\" \"blue_mask\" }"
21     "    { \"int\" \"colormap_size\" }"
22     "    { \"int\" \"bits_per_rgb\" } ;"
23 }
24 "C structure objects can be allocated by calling " { $link <c-object> } " or " { $link malloc-object } "."
25 $nl
26 "Arrays of C structures can be created with the " { $vocab-link "specialized-arrays" } " vocabulary." ;
27
28 ARTICLE: "c-unions" "C unions"
29 "A " { $snippet "union" } " in C defines a type large enough to hold its largest member. This is usually used to allocate a block of memory which can hold one of several types of values."
30 { $subsection POSTPONE: C-UNION: }
31 "C union objects can be allocated by calling " { $link <c-object> } " or " { $link malloc-object } "."
32 $nl
33 "Arrays of C unions can be created with the " { $vocab-link "specialized-arrays" } " vocabulary." ;