]> gitweb.factorcode.org Git - factor.git/blob - core/handbook/alien.facts
de00358174e23591164452b9363703fe421e1b99
[factor.git] / core / handbook / alien.facts
1 IN: alien
2 USING: arrays errors help libc math words definitions
3 kernel-internals ;
4
5 ARTICLE: "c-types-numeric" "Integer and floating point C types"
6 "The following numerical types are available; a " { $snippet "u" } " prefix denotes an unsigned type:"
7 { $table
8     { "C type" "Notes" }
9     { { $snippet "char" } "always 1 byte" }
10     { { $snippet "uchar" } { } }
11     { { $snippet "short" } "always 2 bytes" }
12     { { $snippet "ushort" } { } }
13     { { $snippet "int" } "always 4 bytes" }
14     { { $snippet "uint" } { } }
15     { { $snippet "long" } { "same size as CPU word size and " { $snippet "void*" } ", except on 64-bit Windows, where it is 4 bytes" } }
16     { { $snippet "ulong" } { } }
17     { { $snippet "longlong" } "always 8 bytes" }
18     { { $snippet "ulonglong" } { } }
19     { { $snippet "float" } { } }
20     { { $snippet "double" } { "same format as " { $link float } " objects" } }
21 }
22 "When making alien calls, Factor numbers are converted to and from the above types in a canonical way. Converting a Factor number to a C value may result in a loss of precision." ;
23
24 ARTICLE: "aliens" "Alien addresses"
25 "Instances of the " { $link alien } " class represent pointers to C data outside the Factor heap:"
26 { $subsection <alien> }
27 { $subsection <displaced-alien> }
28 { $subsection alien-address }
29 { $subsection expired? }
30 "Anywhere that a " { $link alien } " instance is accepted, the " { $link f } " singleton may be passed in to denote a null pointer."
31 $terpri
32 "C " { $snippet "void*" } " values returned by functions are wrapped inside fresh " { $link alien } " objects." ;
33
34 ARTICLE: "byte-arrays" "Byte arrays"
35 "Instances of the " { $link byte-array } " class store arbitrary binary data which can be passed to C functions."
36 $terpri
37 "Byte arrays can be allocated directly with a byte count:"
38 { $subsection <byte-array> }
39 "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:"
40 { $subsection <c-object> }
41 { $warning
42 "The Factor garbage collector can move byte arrays around, and it is only safe to pass byte arrays to C functions if the function does not store a pointer to the byte array in some global structure, or retain it in any way after returning."
43 $terpri
44 "Long-lived data for use by C libraries can be allocated manually, just as when programming in C. See " { $link "malloc" } "." } ;
45
46 ARTICLE: "malloc" "Manual memory management"
47 "Sometimes data passed to C functions must be allocated at a fixed address, and so garbage collector managed byte arrays cannot be used. See the warning at the bottom of " { $link "byte-arrays" } " for a description of when this is the case."
48 $terpri
49 "Allocating a C datum with a fixed address:"
50 { $subsection <malloc-object> }
51 "There is a set of words in the " { $vocab-link "libc" } " vocabulary which directly call C standard library memory management functions:"
52 { $subsection malloc }
53 { $subsection calloc }
54 { $subsection realloc }
55 "The return value of the above three words must always be checked for a memory allocation failure:"
56 { $subsection check-ptr }
57 "You must always free pointers returned by any of the above words when the block of memory is no longer in use:"
58 { $subsection free }
59 "You can unsafely copy a range of bytes from one memory location to another:"
60 { $subsection memcpy }
61 "A wrapper for temporarily allocating a block of memory:"
62 { $subsection with-malloc } ;
63
64 ARTICLE: "c-strings" "C strings"
65 "The C library interface defines two types of C strings:"
66 { $table
67     { "C type" "Notes" }
68     { { $snippet "char*" } "8-bit per character null-terminated ASCII" }
69     { { $snippet "ushort*" } "16-bit per character null-terminated UTF16" }
70 }
71 "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. 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."
72 "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:"
73 { $subsection string>char-alien }
74 { $subsection string>u16-alien }
75 { $subsection <malloc-string> }
76 "The first two allocate " { $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:"
77 { $subsection free }
78 "Finally, a set of words can be used to read and write " { $snippet "char*" } " and " { $snippet "ushort*" } " strings at arbitrary addresses:"
79 { $subsection alien>char-string }
80 { $subsection alien>u16-string }
81 { $subsection memory>string }
82 { $subsection string>memory } ;
83
84 ARTICLE: "c-structs" "C structure types"
85 "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."
86 { $subsection POSTPONE: BEGIN-STRUCT: }
87 { $subsection POSTPONE: FIELD: }
88 { $subsection POSTPONE: END-STRUCT }
89 "Great care must be taken when working with C structures since no type or bounds checking is possible."
90 $terpri
91 "An example:"
92 { $code
93     "BEGIN-STRUCT: surface"
94     "    FIELD: uint    flags"
95     "    FIELD: format* format"
96     "    FIELD: int     w"
97     "    FIELD: int     h"
98     "    FIELD: ushort  pitch"
99     "    FIELD: void*   pixels"
100     "    FIELD: int     offset"
101     "    FIELD: void*   hwdata"
102     "    FIELD: short   clip-x"
103     "    FIELD: short   clip-y"
104     "    FIELD: ushort  clip-w"
105     "    FIELD: ushort  clip-h"
106     "    FIELD: uint    unused1"
107     "    FIELD: uint    locked"
108     "    FIELD: int     map"
109     "    FIELD: uint    format_version"
110     "    FIELD: int     refcount"
111     "END-STRUCT"
112 } ;
113
114 ARTICLE: "c-unions" "C unions"
115 "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."
116 { $subsection POSTPONE: C-UNION: } ;
117
118 ARTICLE: "c-out-params" "Output parameters in C"
119 "A frequently-occurring idiom in C code is the \"out parameter\". If a C function returns more than one value, the caller passes pointers of the correct type, and the C function writes its return values to those locations."
120 $terpri
121 "Each numerical C type, together with " { $snippet "void*" } ", has an associated " { $emphasis "out parameter constructor" } " word which takes a Factor object as input, constructs a byte array of the correct size, and converts the Factor object to a C value stored into the byte array:"
122 { $subsection <char> }
123 { $subsection <uchar> }
124 { $subsection <short> }
125 { $subsection <ushort> }
126 { $subsection <int> }
127 { $subsection <uint> }
128 { $subsection <long> }
129 { $subsection <ulong> }
130 { $subsection <longlong> }
131 { $subsection <ulonglong> }
132 { $subsection <float> }
133 { $subsection <double> }
134 { $subsection <void*> }
135 "You call the out parameter constructor with the required initial value, then pass the byte array to the C function, which receives a pointer to the start of the byte array's data area. The C function then returns, leaving the result in the byte array; you read it back using the next set of words:"
136 { $subsection *char }
137 { $subsection *uchar }
138 { $subsection *short }
139 { $subsection *ushort }
140 { $subsection *int }
141 { $subsection *uint }
142 { $subsection *long }
143 { $subsection *ulong }
144 { $subsection *longlong }
145 { $subsection *ulonglong }
146 { $subsection *float }
147 { $subsection *double }
148 { $subsection *void* }
149 { $subsection *char* }
150 { $subsection *ushort* }
151 "Note that while structure and union types do not get these words defined for them, there is no loss of generality since " { $link <void*> } " and " { $link *void* } " may be used." ;
152
153 ARTICLE: "c-arrays" "C arrays"
154 "When calling a C function expecting an array as input, use a utility word to allocate a block of memory of the required size:"
155 { $subsection <c-array> }
156 { $subsection <malloc-array> }
157 "The first one 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:"
158 { $subsection free }
159 "Each C type has a pair of words, " { $snippet { $emphasis "type" } "-nth" } " and " { $snippet "set-" { $emphasis "type" } "-nth" } ", for reading and writing values of this type stored in an array. This set of words includes but is not limited to:"
160 { $subsection char-nth }
161 { $subsection set-char-nth }
162 { $subsection uchar-nth }
163 { $subsection set-uchar-nth }
164 { $subsection short-nth }
165 { $subsection set-short-nth }
166 { $subsection ushort-nth }
167 { $subsection set-ushort-nth }
168 { $subsection int-nth }
169 { $subsection set-int-nth }
170 { $subsection uint-nth }
171 { $subsection set-uint-nth }
172 { $subsection long-nth }
173 { $subsection set-long-nth }
174 { $subsection ulong-nth }
175 { $subsection set-ulong-nth }
176 { $subsection longlong-nth }
177 { $subsection set-longlong-nth }
178 { $subsection ulonglong-nth }
179 { $subsection set-ulonglong-nth }
180 { $subsection float-nth }
181 { $subsection set-float-nth }
182 { $subsection double-nth }
183 { $subsection set-double-nth }
184 { $subsection void*-nth }
185 { $subsection set-void*-nth }
186 { $subsection char*-nth }
187 { $subsection ushort*-nth } ;
188
189 ARTICLE: "reading-writing-memory" "Reading and writing memory directly"
190 "Numerical values can be read from memory addresses and converted to Factor objects using the various typed memory accessor words:"
191 { $subsection alien-signed-1 }
192 { $subsection alien-unsigned-1 }
193 { $subsection alien-signed-2 }
194 { $subsection alien-unsigned-2 }
195 { $subsection alien-signed-4 }
196 { $subsection alien-unsigned-4 }
197 { $subsection alien-signed-cell }
198 { $subsection alien-unsigned-cell }
199 { $subsection alien-signed-8 }
200 { $subsection alien-unsigned-8 }
201 { $subsection alien-float }
202 { $subsection alien-double }
203 "Factor numbers can also be converted to C values and stored to memory:"
204 { $subsection set-alien-signed-1 }
205 { $subsection set-alien-unsigned-1 }
206 { $subsection set-alien-signed-2 }
207 { $subsection set-alien-unsigned-2 }
208 { $subsection set-alien-signed-4 }
209 { $subsection set-alien-unsigned-4 }
210 { $subsection set-alien-signed-cell }
211 { $subsection set-alien-unsigned-cell }
212 { $subsection set-alien-signed-8 }
213 { $subsection set-alien-unsigned-8 }
214 { $subsection set-alien-float }
215 { $subsection set-alien-double } ;
216
217 ARTICLE: "c-data" "Passing data between Factor and C"
218 "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. The C library interface defines its own set of 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. Also Factor's garbage collector can move objects in memory, which means that special support has to be provided for passing blocks of memory, such as structures and unions, to C code."
219 $terpri
220 "C types are identified by strings, and type names occur as parameters to the " { $link alien-invoke } ", " { $link alien-indirect } " and " { $link alien-callback } " words."
221 { $subsection "aliens" }
222 { $subsection "byte-arrays" }
223 { $subsection "malloc" }
224 { $subsection "c-types-numeric" }
225 { $subsection "c-out-params" }
226 { $subsection "c-strings" }
227 { $subsection "c-arrays" }
228 "C-style enumerated types are supported:"
229 { $subsection POSTPONE: C-ENUM: }
230 "C types can be aliased for convenience and consitency with native library documentation:"
231 { $subsection POSTPONE: TYPEDEF: }
232 "New C types can be defined:"
233 { $subsection "c-structs" }
234 { $subsection "c-unions" }
235 { $subsection "reading-writing-memory" } ;
236
237 ARTICLE: "alien" "C library interface"
238 "Factor can directly call C functions in native libraries. It is also possible to compile callbacks which run Factor code, and pass them to native libraries as function pointers."
239 $terpri
240 "The C library interface is entirely self-contained; there is no C code which one must write in order to wrap a library."
241 $terpri
242 "C library interface words are found in the " { $vocab-link "alien" } " vocabulary."
243 { $warning "Since C does not retain runtime type information or do any kind of runtime type checking, any C library interface is not pointer safe. Improper use of C functions can crash the runtime or corrupt memory in unpredictible ways." }
244 { $subsection "loading-libs" }
245 { $subsection "alien-invoke" }
246 { $subsection "alien-callback" }
247 { $subsection "c-data" }
248 { $subsection "dll-internals" } ;
249
250 ARTICLE: "loading-libs" "Loading native libraries"
251 "Before calling a C library, you must associate its path name on disk with a logical name which Factor uses to identify the library:"
252 { $subsection add-library }
253 "Once a library has been defined, you can try loading it to see if the path name is correct:"
254 { $subsection load-library } ;
255
256 ARTICLE: "alien-invoke" "Calling C from Factor"
257 "The easiest way to call into a C library is to define bindings using a pair of parsing words:"
258 { $subsection POSTPONE: LIBRARY: }
259 { $subsection POSTPONE: FUNCTION: }
260 "Don't forget to compile your binding word after defining it; C library calls cannot be made from an interpreted definition."
261 $terpri
262 "The above parsing words create word definitions which call a lower-level word; you can use it directly, too:"
263 { $subsection alien-invoke }
264 "Sometimes it is necessary to invoke a C function pointer, rather than a named C function:"
265 { $subsection alien-indirect }
266 "There are some details concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-data" } "." ;
267
268 ARTICLE: "alien-callback" "Calling Factor from C"
269 "Callbacks can be defined and passed to C code as function pointers; the C code can then invoke the callback and run Factor code:"
270 { $subsection alien-callback }
271 "There are some details concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-data" } "."
272 { $subsection "alien-callback-gc" } ;
273
274 ARTICLE: "alien-callback-gc" "Callbacks and code GC"
275 "A callback consits of two parts; the callback word, which pushes the address of the callback on the stack when executed, and the callback body itself. If the callback word is redefined, removed from the dictionary using " { $link forget } ", or recompiled, the callback body will not be reclaimed by the garbage collector, since potentially C code may be holding a reference to the callback body."
276 $terpri
277 "This is the safest approach, however it can lead to code heap leaks when repeatedly reloading code which defines callbacks. If you are " { $emphasis "completely sure" } " that no running C code is holding a reference to any callbacks, you can blow them all away:"
278 { $code "USE: alien callbacks get clear-hash code-gc" }
279 "This will reclaim all callback bodies which are otherwise unreachable from the dictionary (that is, their associated callback words have since been redefined, recompiled or forgotten)." ;
280
281 ARTICLE: "dll-internals" "DLL handles"
282 "DLL handles are a built-in class of objects which represent loaded native libraries. DLL handles are instances of the " { $link dll } " class, and have a literal syntax used for debugging prinouts; see " { $link "syntax-aliens" } "."
283 $terpri
284 "Usually one never has to deal with DLL handles directly; the C library interface creates them as required. However if direct access to these operating system facilities is required, the following primitives can be used:"
285 { $subsection dlopen }
286 { $subsection dlsym }
287 { $subsection dlclose } ;