]> gitweb.factorcode.org Git - factor.git/blob - basis/specialized-arrays/specialized-arrays-docs.factor
docs: change $subsection to $subsections
[factor.git] / basis / specialized-arrays / specialized-arrays-docs.factor
1 USING: help.markup help.syntax byte-arrays alien ;
2 IN: specialized-arrays
3
4 HELP: SPECIALIZED-ARRAY:
5 { $syntax "SPECIALIZED-ARRAY: type" }
6 { $values { "type" "a C type" } }
7 { $description "Brings a specialized array for holding values of " { $snippet "type" } " into the vocabulary search path. The generated words are documented in " { $link "specialized-array-words" } "." } ;
8
9 ARTICLE: "specialized-array-words" "Specialized array words"
10 "The " { $link POSTPONE: SPECIALIZED-ARRAY: } " parsing word generates the specialized array type if it hasn't been generated already, and adds the following words to the vocabulary search path, where " { $snippet "T" } " is the C type in question:"
11 { $table
12     { { $snippet "T-array" } { "The class of arrays with elements of type " { $snippet "T" } } }
13     { { $snippet "<T-array>" } { "Constructor for arrays with elements of type " { $snippet "T" } "; stack effect " { $snippet "( len -- array )" } } }
14     { { $snippet "(T-array)" } { "Constructor for arrays with elements of type " { $snippet "T" } ", where the initial contents are uninitialized; stack effect " { $snippet "( len -- array )" } } }
15     { { $snippet "malloc-T-array" } { "Constructor for arrays with elements of type " { $snippet "T" } " backed by newly-allocated unmanaged memory; stack effect " { $snippet "( alien len -- array )" } } }
16     { { $snippet "<direct-T-array>" } { "Constructor for arrays with elements of type " { $snippet "T" } " backed by raw memory; stack effect " { $snippet "( alien len -- array )" } } }
17     { { $snippet "byte-array>T-array" } { "Converts a byte array into a specialized array by interpreting the bytes in as machine-specific values. Code which uses this word is unportable" } }
18     { { $snippet ">T-array" } { "Converts a sequence into a specialized array of type " { $snippet "T" } "; stack effect " { $snippet "( seq -- array )" } } }
19     { { $snippet "T-array{" } { "Literal syntax, consists of a series of values terminated by " { $snippet "}" } } }
20 }
21 "Behind the scenes, these words are placed in a vocabulary named " { $snippet "specialized-arrays.instances.T" } ", however this vocabulary should not be placed in a " { $link POSTPONE: USING: } " form directly. Instead, always use " { $link POSTPONE: SPECIALIZED-ARRAY: } ". This ensures that the vocabulary can get generated the first time it is needed." ;
22
23 ARTICLE: "specialized-array-c" "Passing specialized arrays to C functions"
24 "If a C function is declared as taking a parameter with a pointer or an array type (for example, " { $snippet "float*" } " or " { $snippet "int[3]" } "), instances of the relevant specialized array can be passed in."
25 $nl
26 "C type specifiers for array types are documented in " { $link "c-types-specs" } "."
27 $nl
28 "Here is an example; as is common with C functions, the array length is passed in separately, since C does not offer a runtime facility to determine the array length of a base pointer:"
29 { $code
30     "USING: alien.syntax specialized-arrays ;"
31     "SPECIALIZED-ARRAY: int"
32     "FUNCTION: void process_data ( int* data, int len ) ;"
33     "int-array{ 10 20 30 } dup length process_data"
34 }
35 "Literal specialized arrays, as well as specialized arrays created with " { $snippet "<T-array>" } " and " { $snippet ">T-array" } " are backed by a " { $link byte-array } " in the Factor heap, and can move as a result of garbage collection. If this is unsuitable, the array can be allocated in unmanaged memory instead."
36 $nl
37 "In the following example, it is presumed that the C library holds on to a pointer to the array's data after the " { $snippet "init_with_data()" } " call returns; this is one situation where unmanaged memory has to be used instead. Note the use of destructors to ensure the memory is deallocated after the block ends:"
38 { $code
39     "USING: alien.syntax specialized-arrays ;"
40     "SPECIALIZED-ARRAY: float"
41     "FUNCTION: void init_with_data ( float* data, int len ) ;"
42     "FUNCTION: float compute_result ( ) ;"
43     "["
44     "    100 malloc-float-array &free"
45     "    dup length init_with_data"
46     "    compute_result"
47     "] with-destructors"
48 }
49 "Finally, sometimes a C library returns a pointer to an array in unmanaged memory, together with a length. In this case, a specialized array can be constructed to view this memory using " { $snippet "<direct-T-array>" } ":"
50 { $code
51     "USING: alien.c-types classes.struct ;"
52     ""
53     "STRUCT: device_info"
54     "    { id int }"
55     "    { name char* } ;"
56     ""
57     "FUNCTION: void get_device_info ( int* length ) ;"
58     ""
59     "0 <int> [ get_device_info ] keep <direct-int-array> ."
60 }
61 "For a full discussion of Factor heap allocation versus unmanaged memory allocation, see " { $link "byte-arrays-gc" } "."
62 $nl
63 "Each specialized array has a " { $slot "underlying" } " slot holding a " { $link byte-array } " with the raw data. Passing a specialized array as a parameter to a C function call will automatically extract the underlying data. To get at the underlying data directly, call the " { $link >c-ptr } " word on a specialized array." ;
64
65 ARTICLE: "specialized-array-math" "Vector arithmetic with specialized arrays"
66 "Each specialized array with a numeric type generates specialized versions of the " { $link "math-vectors" } " words. The compiler substitutes calls for these words if it can statically determine input types. The " { $snippet "optimized." } " word in the " { $vocab-link "compiler.tree.debugger" } " vocabulary can be used to determine if this optimization is being performed for a particular piece of code." ;
67
68 ARTICLE: "specialized-array-examples" "Specialized array examples"
69 "Let's import specialized float arrays:"
70 { $code "USING: specialized-arrays math.constants math.functions ;" "SPECIALIZED-ARRAY: float" }
71 "Creating a float array with 3 elements:"
72 { $code "1.0 [ sin ] [ cos ] [ tan ] tri float-array{ } 3sequence ." }
73 "Create a float array and sum the elements:"
74 { $code
75     "1000 iota [ 1000 /f pi * sin ] float-array{ } map-as"
76     "0.0 [ + ] reduce ."
77 } ;
78
79 ARTICLE: "specialized-arrays" "Specialized arrays"
80 "The " { $vocab-link "specialized-arrays" } " vocabulary implements fixed-length sequence types for storing machine values in a space-efficient manner without boxing."
81 $nl
82 "A specialized array type needs to be generated for each element type. This is done with a parsing word:"
83 { $subsections POSTPONE: SPECIALIZED-ARRAY: }
84 "This parsing word adds new words to the search path, documented in the next section."
85 { $subsections
86     "specialized-array-words"
87     "specialized-array-c"
88     "specialized-array-math"
89     "specialized-array-examples"
90 }
91 "The " { $vocab-link "specialized-vectors" } " vocabulary provides a resizable version of this abstraction." ;
92
93 ABOUT: "specialized-arrays"