]> gitweb.factorcode.org Git - factor.git/blob - core/io/buffer/buffer.facts
b0131c35205a1d2a7b027a1b4b198dc35babfc06
[factor.git] / core / io / buffer / buffer.facts
1 USING: help io-internals ;
2
3 HELP: buffer
4 { $class-description "The class of I/O buffers, which are allocated in the system malloc arena, and thus have a fixed address, unlike garbage-collected heap objects which are moved around. Buffers must be de-allocated manually."
5 $terpri
6 "Buffers have two internal pointers:"
7 { $list
8     { "the fill pointer -- a write index where new data is added; accessor: " { $link buffer-fill } }
9     { "the position -- a read index where data is consumed; accessor: " { $link buffer-pos } }
10 } } ;
11
12 HELP: <buffer>
13 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } }
14 { $description "Creates a buffer with an initial capacity of " { $snippet "n" } " bytes." } ;
15
16 HELP: buffer-free
17 { $values { "buffer" "a buffer" } }
18 { $description "De-allocates a buffer's underlying storage. The buffer may not be used after being freed." }
19 { $warning "You " { $emphasis "must" } " free a buffer using this word, before letting the GC collect the buffer tuple instance." } ;
20
21 HELP: buffer-contents
22 { $values { "buffer" "a buffer" } { "string" "a string" } }
23 { $description "Collects the entire contents of the buffer into a string." } ;
24
25 HELP: buffer-reset
26 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } }
27 { $description "Resets the fill pointer to 0 and the position to " { $snippet "count" } "." } ;
28
29 HELP: buffer-consume
30 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } }
31 { $description "Moves the position forward by " { $snippet "n" } " bytes. If it exceeds the fill pointer, both are reset to 0." } ;
32
33 HELP: buffer@
34 { $values { "buffer" "a buffer" } { "n" "a non-negative integer" } }
35 { $description "Outputs the memory address of the current buffer position." } ;
36
37 HELP: buffer-end
38 { $values { "buffer" "a buffer" } { "n" "a non-negative integer" } }
39 { $description "Outputs the memory address of the current fill-pointer." } ;
40
41 HELP: buffer-first-n
42 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } }
43 { $description "Outputs a string of the first " { $snippet "n" } " characters at the buffer's current position. If there are less than " { $snippet "n" } " characters available, the output is truncated." }
44 { $see-also buffer> } ;
45
46 HELP: buffer>
47 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } { "string" "a string" } }
48 { $description "Collects a string of " { $snippet "n" } " characters starting from the buffer's current position, and advances the position accordingly. If there are less than " { $snippet "n" } " characters available, the output is truncated." } ;
49
50 HELP: buffer>>
51 { $values { "buffer" "a buffer" } { "string" "a string" } }
52 { $description "Collects the contents of the buffer into a string, and resets the position and fill pointer to 0." } ;
53
54 HELP: buffer-length
55 { $values { "buffer" "a buffer" } { "n" "a non-negative integer" } }
56 { $description "Outputs the number of unconsumed bytes in the buffer." } ;
57
58 HELP: buffer-capacity
59 { $values { "buffer" "a buffer" } { "n" "a non-negative integer" } }
60 { $description "Outputs the buffer's maximum capacity before growing." } ;
61
62 HELP: buffer-empty?
63 { $values { "buffer" "a buffer" } { "?" "a boolean" } }
64 { $description "Tests if the buffer contains no more data to be read." } ;
65
66 HELP: extend-buffer
67 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } }
68 { $description "Grows a buffer to fit " { $snippet "n" } " bytes of data." } ;
69
70 HELP: check-overflow
71 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } }
72 { $description "Grows the buffer, if possible, so it can accomodate " { $snippet "n" } " bytes." }
73 { $warning "I/O system implementations should call this word or one of the other words that calls this word, at the beginning of an I/O transaction, when the buffer is empty. Buffers cannot be resized if they contain data; one of the requirements of a buffer is to remain fixed in memory while I/O operations are in progress." }
74 { $errors "Throws an error if the buffer contains unread data, and the new data does not fit." } ;
75
76 HELP: >buffer
77 { $values { "string" "a string" } { "buffer" "a buffer" } }
78 { $description "Copies a string to the buffer's fill pointer, and advances it accordingly." } ;
79
80 HELP: ch>buffer
81 { $values { "ch" "a character" } { "buffer" "a buffer" } }
82 { $description "Appends a single byte to a buffer." } ;
83
84 HELP: buffer-bound
85 { $values { "buffer" "a buffer" } { "n" "a non-negative integer" } }
86 { $description "Outputs the address pointing to the end of the buffer." } ;
87
88 HELP: n>buffer
89 { $values { "n" "a non-negative integer" } { "buffer" "a buffer" } }
90 { $description "Advances the fill pointer by " { $snippet "n" } " bytes." }
91 { $errors "Throws an error if the buffer does not contain " { $snippet "n" } " bytes of data." } ;
92
93 HELP: buffer-peek
94 { $values { "buffer" "a buffer" } { "ch" "a character" } }
95 { $description "Outputs the byte at the buffer position." }
96 { $see-also buffer-pop } ;
97
98 HELP: buffer-pop
99 { $values { "buffer" "a buffer" } { "ch" "a character" } }
100 { $description "Outputs the byte at the buffer position and advances the position." } ;