2008-06-12 04:49:59 -04:00
USING: help.markup help.syntax byte-arrays alien destructors ;
2007-09-20 18:09:08 -04:00
IN: io.buffers
ARTICLE: "buffers" "Locked I/O buffers"
2008-06-29 03:17:32 -04:00
"I/O buffers are first-in-first-out queues of bytes."
$nl
"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."
$nl
"Buffers are used to implement native I/O backends."
2007-09-20 18:09:08 -04:00
$nl
2008-09-24 02:59:05 -04:00
"Buffer words are found in the " { $vocab-link "io.buffers" } " vocabulary."
2009-10-01 15:56:36 -04:00
{ $subsections
buffer
<buffer>
}
2008-06-12 04:49:59 -04:00
"Buffers must be manually deallocated by calling " { $link dispose } "."
$nl
2007-09-20 18:09:08 -04:00
"Buffer operations:"
2009-10-01 15:56:36 -04:00
{ $subsections
buffer-reset
buffer-length
buffer-empty?
buffer-capacity
buffer@
}
2007-09-20 18:09:08 -04:00
"Reading from the buffer:"
2009-10-01 15:56:36 -04:00
{ $subsections
buffer-peek
buffer-pop
buffer-read
}
2007-09-20 18:09:08 -04:00
"Writing to the buffer:"
2009-10-01 15:56:36 -04:00
{ $subsections
byte>buffer
>buffer
n>buffer
} ;
2007-09-20 18:09:08 -04:00
ABOUT: "buffers"
HELP: buffer
2008-02-21 16:22:49 -05:00
{ $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."
2007-09-20 18:09:08 -04:00
$nl
"Buffers have two internal pointers:"
{ $list
2008-08-29 11:27:31 -04:00
{ { $snippet "fill" } " - the fill pointer, a write index where new data is added" }
{ { $snippet "pos" } " - the position, a read index where data is consumed" }
2007-09-20 18:09:08 -04:00
} } ;
HELP: <buffer>
{ $values { "n" "a non-negative integer" } { "buffer" buffer } }
2014-04-13 19:26:43 -04:00
{ $description "Allocates a buffer with an initial capacity of " { $snippet "n" } " bytes." } ;
2007-09-20 18:09:08 -04:00
HELP: buffer-reset
{ $values { "n" "a non-negative integer" } { "buffer" buffer } }
{ $description "Resets the fill pointer to 0 and the position to " { $snippet "count" } "." } ;
HELP: buffer-consume
{ $values { "n" "a non-negative integer" } { "buffer" buffer } }
{ $description "Moves the position forward by " { $snippet "n" } " bytes. If it exceeds the fill pointer, both are reset to 0." } ;
HELP: buffer@
{ $values { "buffer" buffer } { "alien" alien } }
{ $description "Outputs the memory address of the current buffer position." } ;
HELP: buffer-end
{ $values { "buffer" buffer } { "alien" alien } }
{ $description "Outputs the memory address of the current fill-pointer." } ;
2008-03-29 01:59:05 -04:00
HELP: buffer-read
2008-03-08 03:51:26 -05:00
{ $values { "n" "a non-negative integer" } { "buffer" buffer } { "byte-array" byte-array } }
2013-10-15 07:27:49 -04:00
{ $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." }
{ $examples
{ $example
2014-04-13 19:26:43 -04:00
"USING: destructors io.buffers kernel prettyprint ;"
"5 100 <buffer> [ B{ 7 14 21 } over >buffer buffer-read ] with-disposal ."
2013-10-15 07:27:49 -04:00
"B{ 7 14 21 }"
}
} ;
2007-09-20 18:09:08 -04:00
HELP: buffer-length
{ $values { "buffer" buffer } { "n" "a non-negative integer" } }
2013-10-15 07:27:49 -04:00
{ $description "Outputs the number of unconsumed bytes in the buffer." }
{ $examples
{ $example
2014-04-13 19:26:43 -04:00
"USING: destructors io.buffers kernel prettyprint ;"
"100 <buffer> [ B{ 7 14 21 } over >buffer buffer-length ] with-disposal ."
2013-10-15 07:27:49 -04:00
"3"
}
} ;
2007-09-20 18:09:08 -04:00
HELP: buffer-capacity
{ $values { "buffer" buffer } { "n" "a non-negative integer" } }
2013-10-15 07:27:49 -04:00
{ $description "Outputs the buffer's maximum capacity before growing." }
{ $examples
{ $example
2014-04-13 19:26:43 -04:00
"USING: destructors io.buffers prettyprint ;"
"100 <buffer> [ buffer-capacity ] with-disposal ."
2013-10-15 07:27:49 -04:00
"100"
}
} ;
2007-09-20 18:09:08 -04:00
HELP: buffer-empty?
{ $values { "buffer" buffer } { "?" "a boolean" } }
2013-10-15 07:27:49 -04:00
{ $description "Tests if the buffer contains no more data to be read or written." } ;
2007-09-20 18:09:08 -04:00
HELP: >buffer
2008-03-08 03:51:26 -05:00
{ $values { "byte-array" byte-array } { "buffer" buffer } }
2008-06-29 03:17:32 -04:00
{ $description "Copies a byte array to the buffer's fill pointer, and advances it accordingly." }
{ $warning "This word will corrupt memory if the byte array is larger than the space available in the buffer." } ;
2007-09-20 18:09:08 -04:00
2008-03-08 03:51:26 -05:00
HELP: byte>buffer
{ $values { "byte" "a byte" } { "buffer" buffer } }
2008-06-29 03:17:32 -04:00
{ $description "Appends a single byte to a buffer." }
2013-10-15 07:27:49 -04:00
{ $warning "This word will corrupt memory if the buffer is full." }
{ $examples
{ $example
2014-04-13 19:26:43 -04:00
"USING: destructors io.buffers kernel prettyprint ;"
"100 <buffer> [ 237 over byte>buffer buffer-pop ] with-disposal ."
2013-10-15 07:27:49 -04:00
"237"
}
} ;
2007-09-20 18:09:08 -04:00
HELP: n>buffer
{ $values { "n" "a non-negative integer" } { "buffer" buffer } }
{ $description "Advances the fill pointer by " { $snippet "n" } " bytes." }
2008-06-29 03:17:32 -04:00
{ $warning "This word will leave the buffer in an invalid state if it does not have " { $snippet "n" } " bytes available." } ;
2007-09-20 18:09:08 -04:00
HELP: buffer-peek
2008-03-11 20:51:58 -04:00
{ $values { "buffer" buffer } { "byte" "a byte" } }
2007-09-20 18:09:08 -04:00
{ $description "Outputs the byte at the buffer position." } ;
HELP: buffer-pop
2008-03-11 20:51:58 -04:00
{ $values { "buffer" buffer } { "byte" "a byte" } }
2007-09-20 18:09:08 -04:00
{ $description "Outputs the byte at the buffer position and advances the position." } ;