1 USING: help.markup help.syntax byte-arrays alien destructors ;
4 ARTICLE: "buffers" "Locked I/O buffers"
5 "I/O buffers are first-in-first-out queues of bytes."
7 "Buffers are backed by manually allocated storage that does not get moved by the garbage collector; they are also low-level and sacrifice error checking for efficiency."
9 "Buffers are used to implement native I/O backends."
11 "Buffer words are found in the " { $vocab-link "io.buffers" } " vocabulary."
12 { $subsection buffer }
13 { $subsection <buffer> }
14 "Buffers must be manually deallocated by calling " { $link dispose } "."
17 { $subsection buffer-reset }
18 { $subsection buffer-length }
19 { $subsection buffer-empty? }
20 { $subsection buffer-capacity }
21 { $subsection buffer@ }
22 "Reading from the buffer:"
23 { $subsection buffer-peek }
24 { $subsection buffer-pop }
25 { $subsection buffer-read }
26 "Writing to the buffer:"
27 { $subsection byte>buffer }
28 { $subsection >buffer }
29 { $subsection n>buffer } ;
34 { $class-description "The class of I/O buffers, which resemble FIFO queues, but are optimized for holding bytes, are have underlying storage allocated at a fixed address. Buffers must be de-allocated manually."
36 "Buffers have two internal pointers:"
38 { { $snippet "fill" } " - the fill pointer, a write index where new data is added" }
39 { { $snippet "pos" } " - the position, a read index where data is consumed" }
43 { $values { "n" "a non-negative integer" } { "buffer" buffer } }
44 { $description "Creates a buffer with an initial capacity of " { $snippet "n" } " bytes." } ;
47 { $values { "n" "a non-negative integer" } { "buffer" buffer } }
48 { $description "Resets the fill pointer to 0 and the position to " { $snippet "count" } "." } ;
51 { $values { "n" "a non-negative integer" } { "buffer" buffer } }
52 { $description "Moves the position forward by " { $snippet "n" } " bytes. If it exceeds the fill pointer, both are reset to 0." } ;
55 { $values { "buffer" buffer } { "alien" alien } }
56 { $description "Outputs the memory address of the current buffer position." } ;
59 { $values { "buffer" buffer } { "alien" alien } }
60 { $description "Outputs the memory address of the current fill-pointer." } ;
63 { $values { "n" "a non-negative integer" } { "buffer" buffer } { "byte-array" byte-array } }
64 { $description "Collects a byte array of " { $snippet "n" } " bytes starting from the buffer's current position, and advances the position accordingly. If there are less than " { $snippet "n" } " bytes available, the output is truncated." } ;
67 { $values { "buffer" buffer } { "n" "a non-negative integer" } }
68 { $description "Outputs the number of unconsumed bytes in the buffer." } ;
71 { $values { "buffer" buffer } { "n" "a non-negative integer" } }
72 { $description "Outputs the buffer's maximum capacity before growing." } ;
75 { $values { "buffer" buffer } { "?" "a boolean" } }
76 { $description "Tests if the buffer contains no more data to be read." } ;
79 { $values { "byte-array" byte-array } { "buffer" buffer } }
80 { $description "Copies a byte array to the buffer's fill pointer, and advances it accordingly." }
81 { $warning "This word will corrupt memory if the byte array is larger than the space available in the buffer." } ;
84 { $values { "byte" "a byte" } { "buffer" buffer } }
85 { $description "Appends a single byte to a buffer." }
86 { $warning "This word will corrupt memory if the buffer is full." } ;
89 { $values { "n" "a non-negative integer" } { "buffer" buffer } }
90 { $description "Advances the fill pointer by " { $snippet "n" } " bytes." }
91 { $warning "This word will leave the buffer in an invalid state if it does not have " { $snippet "n" } " bytes available." } ;
94 { $values { "buffer" buffer } { "byte" "a byte" } }
95 { $description "Outputs the byte at the buffer position." } ;
98 { $values { "buffer" buffer } { "byte" "a byte" } }
99 { $description "Outputs the byte at the buffer position and advances the position." } ;