NAME

sliding_buffer -- growing buffer where old contents can be expired


SYNOPSIS

   #include <afblib/sliding_buffer.h>
   typedef struct {
      size_t offset;
      stralloc sa;
   } sliding_buffer;
   bool sliding_buffer_ready(sliding_buffer* buffer, size_t minspace);
   void sliding_buffer_free(sliding_buffer* buffer);


DESCRIPTION

A sliding buffer is a stralloc-based buffer that dynamically grows but where just the contents from buffer position offset to sa.len is possibly used and where the buffer contents before offset is no longer needed. New contents may be added anytime, causing sa.len to grow. offset may be increased anytime but must not exceed sa.len. This allows to expire no longer needed buffer contents.

sliding_buffer_ready makes sure that minspace bytes are available between sa.len and sa.a. To assure this, sliding_buffer_ready is free either to grow the buffer (i.e. by resizing sa.a much like stralloc_readyplus would do it) or by moving its contents, causing offset to be set to 0, and sa.len to shrink accordingly. On success, true is returned, in case of failures, i.e. out of memory, false is returned.

sliding_buffer_free is a shorthand for stralloc_free on sa.


AUTHOR

Andreas F. Borchert