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." 
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection <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:" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer-reset }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer-length }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer-empty? }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer-capacity }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer@ }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								"Reading from the buffer:" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer-peek }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer-pop }
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 01:59:05 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection buffer-read }
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								"Writing to the buffer:" 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-08 03:51:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection byte>buffer }
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection >buffer }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection n>buffer } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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 } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Creates a buffer with an initial capacity of "  { $snippet "n"  } " bytes."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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 } }
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 01:59:05 -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."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  buffer-length 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "buffer"  buffer } { "n"  "a non-negative integer"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Outputs the number of unconsumed bytes in the buffer."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  buffer-capacity 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "buffer"  buffer } { "n"  "a non-negative integer"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Outputs the buffer's maximum capacity before growing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  buffer-empty? 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "buffer"  buffer } { "?"  "a boolean"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Tests if the buffer contains no more data to be read."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $warning "This word will corrupt memory if the buffer is full."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											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."  } ;