]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/data/data-docs.factor
move some allocation words that don't really have much to do with c types out of...
[factor.git] / basis / alien / data / data-docs.factor
1 USING: alien alien.c-types help.syntax help.markup libc kernel.private
2 byte-arrays math strings hashtables alien.syntax alien.strings sequences
3 io.encodings.string debugger destructors vocabs.loader ;
4 IN: alien.data
5
6 HELP: <c-array>
7 { $values { "len" "a non-negative integer" } { "c-type" "a C type" } { "array" byte-array } }
8 { $description "Creates a byte array large enough to hold " { $snippet "n" } " values of a C type." }
9 { $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." }
10 { $errors "Throws an error if the type does not exist, the necessary specialized array vocabulary is not loaded, or the requested size is negative." } ;
11
12 HELP: <c-object>
13 { $values { "type" "a C type" } { "array" byte-array } }
14 { $description "Creates a byte array suitable for holding a value with the given C type." }
15 { $errors "Throws an " { $link no-c-type } " error if the type does not exist." } ;
16
17 { <c-object> malloc-object } related-words
18
19 HELP: memory>byte-array
20 { $values { "alien" c-ptr } { "len" "a non-negative integer" } { "byte-array" byte-array } }
21 { $description "Reads " { $snippet "len" } " bytes starting from " { $snippet "base" } " and stores them in a new byte array." } ;
22
23 HELP: byte-array>memory
24 { $values { "byte-array" byte-array } { "base" c-ptr } }
25 { $description "Writes a byte array to memory starting from the " { $snippet "base" } " address." }
26 { $warning "This word is unsafe. Improper use can corrupt memory." } ;
27
28 HELP: malloc-array
29 { $values { "n" "a non-negative integer" } { "type" "a C type" } { "alien" alien } }
30 { $description "Allocates an unmanaged memory block large enough to hold " { $snippet "n" } " values of a C type, then wraps the memory in a sequence object using " { $link <c-direct-array> } "." }
31 { $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." }
32 { $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
33 { $errors "Throws an error if the type does not exist, if the requested size is negative, if a direct specialized array class appropriate to the type is not loaded, or if memory allocation fails." } ;
34
35 HELP: malloc-object
36 { $values { "type" "a C type" } { "alien" alien } }
37 { $description "Allocates an unmanaged memory block large enough to hold a value of a C type." }
38 { $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
39 { $errors "Throws an error if the type does not exist or if memory allocation fails." } ;
40
41 HELP: malloc-byte-array
42 { $values { "byte-array" byte-array } { "alien" alien } }
43 { $description "Allocates an unmanaged memory block of the same size as the byte array, and copies the contents of the byte array there." }
44 { $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
45 { $errors "Throws an error if memory allocation fails." } ;
46
47 { <c-array> <c-direct-array> malloc-array } related-words
48
49 { string>alien alien>string malloc-string } related-words
50
51 ARTICLE: "malloc" "Manual memory management"
52 "Sometimes data passed to C functions must be allocated at a fixed address. See " { $link "byte-arrays-gc" } " for an explanation of when this is the case."
53 $nl
54 "Allocating a C datum with a fixed address:"
55 { $subsection malloc-object }
56 { $subsection malloc-array }
57 { $subsection malloc-byte-array }
58 "There is a set of words in the " { $vocab-link "libc" } " vocabulary which directly call C standard library memory management functions:"
59 { $subsection malloc }
60 { $subsection calloc }
61 { $subsection realloc }
62 "You must always free pointers returned by any of the above words when the block of memory is no longer in use:"
63 { $subsection free }
64 "Utilities for automatically freeing memory in conjunction with " { $link with-destructors } ":"
65 { $subsection &free }
66 { $subsection |free }
67 "The " { $link &free } " and " { $link |free } " words are generated using " { $link "alien.destructors" } "."
68 $nl
69 "You can unsafely copy a range of bytes from one memory location to another:"
70 { $subsection memcpy }
71 "You can copy a range of bytes from memory into a byte array:"
72 { $subsection memory>byte-array }
73 "You can copy a byte array to memory unsafely:"
74 { $subsection byte-array>memory } ;
75
76
77 ARTICLE: "c-byte-arrays" "Passing data in byte arrays"
78 "Instances of the " { $link byte-array } " class can be passed to C functions; the C function receives a pointer to the first element of the array."
79 $nl
80 "Byte arrays can be allocated directly with a byte count using the " { $link <byte-array> } " word. However in most cases, instead of computing a size in bytes directly, it is easier to use a higher-level word which expects C type and outputs a byte array large enough to hold that type:"
81 { $subsection <c-object> }
82 { $subsection <c-array> }
83 { $warning
84 "The Factor garbage collector can move byte arrays around, and code passing byte arrays to C must obey important guidelines. See " { $link "byte-arrays-gc" } "." }
85 { $see-also "c-arrays" } ;
86
87 ARTICLE: "c-data" "Passing data between Factor and C"
88 "Two defining characteristics of Factor are dynamic typing and automatic memory management, which are somewhat incompatible with the machine-level data model exposed by C. Factor's C library interface defines its own set of C data types, distinct from Factor language types, together with automatic conversion between Factor values and C types. For example, C integer types must be declared and are fixed-width, whereas Factor supports arbitrary-precision integers."
89 $nl
90 "Furthermore, Factor's garbage collector can move objects in memory; for a discussion of the consequences, see " { $link "byte-arrays-gc" } "."
91 { $subsection "c-types-specs" }
92 { $subsection "c-byte-arrays" }
93 { $subsection "malloc" }
94 { $subsection "c-strings" }
95 { $subsection "c-arrays" }
96 { $subsection "c-out-params" }
97 "Important guidelines for passing data in byte arrays:"
98 { $subsection "byte-arrays-gc" }
99 "C-style enumerated types are supported:"
100 { $subsection POSTPONE: C-ENUM: }
101 "C types can be aliased for convenience and consitency with native library documentation:"
102 { $subsection POSTPONE: TYPEDEF: }
103 "New C types can be defined:"
104 { $subsection "c-structs" }
105 { $subsection "c-unions" }
106 "A utility for defining " { $link "destructors" } " for deallocating memory:"
107 { $subsection "alien.destructors" }
108 { $see-also "aliens" } ;
109 HELP: malloc-string
110 { $values { "string" string } { "encoding" "an encoding descriptor" } { "alien" c-ptr } }
111 { $description "Encodes a string together with a trailing null code point using the given encoding, and stores the resulting bytes in a freshly-allocated unmanaged memory block." }
112 { $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
113 { $errors "Throws an error if one of the following conditions occurs:"
114     { $list
115         "the string contains null code points"
116         "the string contains characters not representable using the encoding specified"
117         "memory allocation fails"
118     }
119 } ;
120
121 HELP: require-c-array
122 { $values { "c-type" "a C type" } }
123 { $description "Generates a specialized array of " { $snippet "c-type" } " using the " { $link <c-array> } " or " { $link <c-direct-array> } " vocabularies." }
124 { $notes "This word must be called inside a compilation unit. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence types loaded." } ;
125
126 HELP: <c-direct-array>
127 { $values { "alien" c-ptr } { "len" integer } { "c-type" "a C type" } { "array" "a specialized direct array" } }
128 { $description "Constructs a new specialized array of length " { $snippet "len" } " and element type " { $snippet "c-type" } " over the range of memory referenced by " { $snippet "alien" } "." }
129 { $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." } ;
130
131 ARTICLE: "c-strings" "C strings"
132 "C string types are arrays with shape " { $snippet "{ \"char*\" encoding }" } ", where " { $snippet "encoding" } " is an encoding descriptor. The type " { $snippet "\"char*\"" } " is an alias for " { $snippet "{ \"char*\" utf8 }" } ". See " { $link "encodings-descriptors" } " for information about encoding descriptors."
133 $nl
134 "Passing a Factor string to a C function expecting a C string allocates a " { $link byte-array } " in the Factor heap; the string is then converted to the requested format and a raw pointer is passed to the function."
135 $nl
136 "If the conversion fails, for example if the string contains null bytes or characters with values higher than 255, a " { $link c-string-error. } " is thrown."
137 $nl
138 "Care must be taken if the C function expects a " { $snippet "char*" } " with a length in bytes, rather than a null-terminated " { $snippet "char*" } "; passing the result of calling " { $link length } " on the string object will not suffice. This is because a Factor string of " { $emphasis "n" } " characters will not necessarily encode to " { $emphasis "n" } " bytes. The correct idiom for C functions which take a string with a length is to first encode the string using " { $link encode } ", and then pass the resulting byte array together with the length of this byte array."
139 $nl
140 "Sometimes a C function has a parameter type of " { $snippet "void*" } ", and various data types, among them strings, can be passed in. In this case, strings are not automatically converted to aliens, and instead you must call one of these words:"
141 { $subsection string>alien }
142 { $subsection malloc-string }
143 "The first allocates " { $link byte-array } "s, and the latter allocates manually-managed memory which is not moved by the garbage collector and has to be explicitly freed by calling " { $link free } ". See " { $link "byte-arrays-gc" } " for a discussion of the two approaches."
144 $nl
145 "A word to read strings from arbitrary addresses:"
146 { $subsection alien>string }
147 "For example, if a C function returns a " { $snippet "char*" } " but stipulates that the caller must deallocate the memory afterward, you must define the function as returning " { $snippet "void*" } ", and call one of the above words before passing the pointer to " { $link free } "." ;
148