WvStreams
|
A buffer that wraps a pre-allocated array and provides read-write access to its elements using a circular buffering scheme rather than a purely linear one, as used by WvInPlaceBuf. More...
#include <wvbufbase.h>
Public Member Functions | |
WvCircularBufBase (T *_data, size_t _avail, size_t _size, bool _autofree=false) | |
Creates a new circular buffer backed by the supplied array. | |
WvCircularBufBase (size_t _size) | |
Creates a new empty circular buffer backed by a new array. | |
WvCircularBufBase () | |
Creates a new empty buffer with no backing array. | |
virtual | ~WvCircularBufBase () |
Destroys the buffer. | |
T * | ptr () const |
Returns the underlying array pointer. | |
size_t | size () const |
Returns the total size of the buffer. | |
bool | get_autofree () const |
Returns the autofree flag. | |
void | set_autofree (bool _autofree) |
Sets or clears the autofree flag. | |
void | reset (T *_data, size_t _avail, size_t _size, bool _autofree=false) |
Resets the underlying buffer pointer and properties. | |
void | setavail (size_t _avail) |
Sets the amount of available data using the current buffer and resets the read index to the beginning of the buffer. | |
void | normalize () |
Normalizes the arrangement of the data such that the contents of the buffer are stored at the beginning of the array starting with the next element that would be returned by get(size_t). | |
WvBufStore * | getstore () |
Returns a pointer to the underlying storage class object. | |
bool | isreadable () const |
Returns true if the buffer supports reading. | |
size_t | used () const |
Returns the number of elements in the buffer currently available for reading. | |
const T * | get (size_t count) |
Reads exactly the specified number of elements and returns a pointer to a storage location owned by the buffer. | |
T | get () |
Reads the next element from the buffer. | |
void | skip (size_t count) |
Skips exactly the specified number of elements. | |
size_t | optgettable () const |
Returns the optimal maximum number of elements in the buffer currently available for reading without incurring significant overhead. | |
void | unget (size_t count) |
Ungets exactly the specified number of elements by returning them to the buffer for subsequent reads. | |
size_t | ungettable () const |
Returns the maximum number of elements that may be ungotten at this time. | |
const T * | peek (int offset, size_t count) |
Returns a const pointer into the buffer at the specified offset to the specified number of elements without actually adjusting the current get() index. | |
T | peek (int offset=0) |
Returns the element at the specified offset in the buffer. | |
size_t | peekable (int offset) |
size_t | optpeekable (int offset) |
void | zap () |
Clears the buffer. | |
void | move (T *buf, size_t count) |
Efficiently copies the specified number of elements from the buffer to the specified UNINITIALIZED storage location and removes the elements from the buffer. | |
void | copy (T *buf, int offset, size_t count) |
Efficiently copies the specified number of elements from the buffer to the specified UNINITIALIZED storage location but does not remove the elements from the buffer. | |
bool | iswritable () const |
Returns true if the buffer supports writing. | |
size_t | free () const |
Returns the number of elements that the buffer can currently accept for writing. | |
T * | alloc (size_t count) |
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage location owned by the buffer. | |
size_t | optallocable () const |
Returns the optimal maximum number of elements that the buffer can currently accept for writing without incurring significant overhead. | |
void | unalloc (size_t count) |
Unallocates exactly the specified number of elements by removing them from the buffer and releasing their storage. | |
size_t | unallocable () const |
Returns the maximum number of elements that may be unallocated at this time. | |
T * | mutablepeek (int offset, size_t count) |
Returns a non-const pointer info the buffer at the specified offset to the specified number of elements without actually adjusting the current get() index. | |
void | put (const T *data, size_t count) |
Writes the specified number of elements from the specified storage location into the buffer at its tail. | |
void | put (T &value) |
Writes the element into the buffer at its tail. | |
void | poke (const T *data, int offset, size_t count) |
Efficiently copies the specified number of elements from the specified storage location into the buffer at a particular offset. | |
void | poke (T &value, int offset) |
Writes the element into the buffer at the specified offset. | |
void | merge (Buffer &inbuf, size_t count) |
Efficiently moves count bytes from the specified buffer into this one. | |
void | merge (Buffer &inbuf) |
Efficiently merges the entire contents of a buffer into this one. | |
Protected Types | |
typedef T | Elem |
typedef WvBufBase< T > | Buffer |
Protected Attributes | |
WvCircularBufStore | mystore |
WvBufStore * | store |
A buffer that wraps a pre-allocated array and provides read-write access to its elements using a circular buffering scheme rather than a purely linear one, as used by WvInPlaceBuf.
When there is insufficient contigous free/used space to satisfy a read or write request, the data is automatically reordered in-place to coalesce the free/used spaces into sufficiently large chunks. The process may also be manually triggered to explicitly renormalize the array and shift its contents to the front.
"T" is the type of object to store, must be a primitive or a struct without special initialization, copy, or assignment semantics
Definition at line 813 of file wvbufbase.h.
WvCircularBufBase< T >::WvCircularBufBase | ( | T * | _data, |
size_t | _avail, | ||
size_t | _size, | ||
bool | _autofree = false |
||
) | [inline] |
Creates a new circular buffer backed by the supplied array.
"_data" is the array of data to wrap "_avail" is the amount of data available for reading at the beginning of the buffer "_size" is the size of the array "_autofree" is if true, the array will be freed when discarded
Definition at line 830 of file wvbufbase.h.
WvCircularBufBase< T >::WvCircularBufBase | ( | size_t | _size | ) | [inline, explicit] |
Creates a new empty circular buffer backed by a new array.
"_size" is the size of the array
Definition at line 841 of file wvbufbase.h.
WvCircularBufBase< T >::WvCircularBufBase | ( | ) | [inline] |
Creates a new empty buffer with no backing array.
Definition at line 846 of file wvbufbase.h.
virtual WvCircularBufBase< T >::~WvCircularBufBase | ( | ) | [inline, virtual] |
Destroys the buffer.
Frees the underlying array if autofree().
Definition at line 856 of file wvbufbase.h.
T* WvCircularBufBase< T >::ptr | ( | ) | const [inline] |
Returns the underlying array pointer.
Returns: the element pointer
Definition at line 863 of file wvbufbase.h.
size_t WvCircularBufBase< T >::size | ( | ) | const [inline] |
Returns the total size of the buffer.
Returns: the number of elements
Definition at line 873 of file wvbufbase.h.
bool WvCircularBufBase< T >::get_autofree | ( | ) | const [inline] |
void WvCircularBufBase< T >::set_autofree | ( | bool | _autofree | ) | [inline] |
Sets or clears the autofree flag.
"_autofree" is if true, the array will be freed when discarded
Definition at line 893 of file wvbufbase.h.
void WvCircularBufBase< T >::reset | ( | T * | _data, |
size_t | _avail, | ||
size_t | _size, | ||
bool | _autofree = false |
||
) | [inline] |
Resets the underlying buffer pointer and properties.
If the old and new buffer pointers differ and the old buffer was specified as autofree, the old buffer is destroyed.
"_data" is the array of data to wrap "_avail" is the amount of data available for reading at the beginning of the buffer "_size" is the size of the array "_autofree" is if true, the array will be freed when discarded
Definition at line 910 of file wvbufbase.h.
void WvCircularBufBase< T >::setavail | ( | size_t | _avail | ) | [inline] |
Sets the amount of available data using the current buffer and resets the read index to the beginning of the buffer.
"_avail" is the amount of data available for reading at the beginning of the buffer
Definition at line 924 of file wvbufbase.h.
void WvCircularBufBase< T >::normalize | ( | ) | [inline] |
Normalizes the arrangement of the data such that the contents of the buffer are stored at the beginning of the array starting with the next element that would be returned by get(size_t).
After invocation, ungettable() may equal 0.
Definition at line 938 of file wvbufbase.h.
WvBufStore* WvBufBaseCommonImpl< T >::getstore | ( | ) | [inline, inherited] |
Returns a pointer to the underlying storage class object.
Returns: the low-level storage class object pointer, non-null
Definition at line 69 of file wvbufbase.h.
bool WvBufBaseCommonImpl< T >::isreadable | ( | ) | const [inline, inherited] |
Returns true if the buffer supports reading.
Returns: true if reading is supported
Definition at line 81 of file wvbufbase.h.
size_t WvBufBaseCommonImpl< T >::used | ( | ) | const [inline, inherited] |
Returns the number of elements in the buffer currently available for reading.
This function could also be called gettable().
Definition at line 92 of file wvbufbase.h.
Referenced by WvBase64Encoder::_encode(), WvWordWrapEncoder::_encode(), WvHexEncoder::_encode(), WvRateAdjust::_encode(), WvCounterModeEncoder::_encode(), WvBlowfishEncoder::_encode(), WvBase64Decoder::_encode(), WvHexDecoder::_encode(), WvTripleDESEncoder::_encode(), WvGzipEncoder::_encode(), WvBackslashDecoder::_encode(), WvRSAEncoder::_encode(), WvPassthroughEncoder::_encode(), WvSyslog::_end_line(), WvLogBuffer::_end_line(), WvStream::blocking_getline(), WvDBusMsg::demarshal(), WvDBusMsg::demarshal_bytes_needed(), WvEncoder::encode(), WvEncoder::encodebufmem(), WvStreamClone::flush_internal(), WvStream::flush_then_close(), WvBufBaseCommonImpl< unsigned char >::get(), WvBufBase< unsigned char >::getstr(), WvDBusConn::isidle(), WvFdStream::maybe_autoclose(), WvStream::maybe_autoclose(), WvBufBaseCommonImpl< unsigned char >::merge(), WvStreamClone::nowrite(), WvSSLStream::post_select(), WvStreamClone::post_select(), WvFdStream::post_select(), WvEncoderStream::post_select(), WvStream::post_select(), WvSSLStream::pre_select(), WvStreamClone::pre_select(), WvFdStream::pre_select(), WvEncoderStream::pre_select(), WvStream::pre_select(), WvStream::read(), UniIniGen::refresh(), WvX509Mgr::sign(), WvBufBase< unsigned char >::strchr(), WvSSLStream::uread(), WvEncoderStream::uread(), WvSSLStream::uwrite(), WvX509::verify(), WvStream::write(), and wvtcl_getword().
const T* WvBufBaseCommonImpl< T >::get | ( | size_t | count | ) | [inline, inherited] |
Reads exactly the specified number of elements and returns a pointer to a storage location owned by the buffer.
The pointer is only valid until the next non-const buffer member is called. eg. alloc(size_t)
If count == 0, a NULL pointer may be returned.
It is an error for count to be greater than the number of available elements in the buffer.
For maximum efficiency, call this function multiple times with count no greater than optgettable() each time.
After this operation, at least count elements may be ungotten.
Definition at line 114 of file wvbufbase.h.
Referenced by WvXOREncoder::_encode(), WvBackslashEncoder::_encode(), WvRateAdjust::_encode(), WvCounterModeEncoder::_encode(), WvEVPMDDigest::_encode(), WvBlowfishEncoder::_encode(), WvTripleDESEncoder::_encode(), WvBackslashDecoder::_encode(), WvRSAEncoder::_encode(), WvHMACDigest::_encode(), WvCrc32Digest::_encode(), WvAdler32Digest::_encode(), WvSyslog::_end_line(), WvLogBuffer::_end_line(), WvFunctorEncoder< IT, OT, FT >::_typedencode(), WvStream::blocking_getline(), WvDBusMsg::demarshal(), WvStream::read(), WvSSLStream::uwrite(), WvStream::write(), and wvtcl_getword().
T WvBufBaseCommonImpl< T >::get | ( | ) | [inline, inherited] |
Reads the next element from the buffer.
It is an error to invoke this method if used() == 0.
After this operation, at least 1 element may be ungotten.
Returns: the element
Definition at line 272 of file wvbufbase.h.
void WvBufBaseCommonImpl< T >::skip | ( | size_t | count | ) | [inline, inherited] |
Skips exactly the specified number of elements.
This is equivalent to invoking get(size_t) with the count and discarding the result, but may be faster for certain types of buffers. As with get(size_t), the call may be followed up by an unget(size_t).
It is an error for count to be greater than the number of available elements in the buffer.
"count" is the number of elements
Definition at line 136 of file wvbufbase.h.
size_t WvBufBaseCommonImpl< T >::optgettable | ( | ) | const [inline, inherited] |
Returns the optimal maximum number of elements in the buffer currently available for reading without incurring significant overhead.
Invariants:
Returns: the number of elements
Definition at line 154 of file wvbufbase.h.
Referenced by WvXOREncoder::_encode(), WvBackslashEncoder::_encode(), WvCounterModeEncoder::_encode(), WvEVPMDDigest::_encode(), WvBackslashDecoder::_encode(), WvHMACDigest::_encode(), WvCrc32Digest::_encode(), WvAdler32Digest::_encode(), and WvFunctorEncoder< IT, OT, FT >::_typedencode().
void WvBufBaseCommonImpl< T >::unget | ( | size_t | count | ) | [inline, inherited] |
Ungets exactly the specified number of elements by returning them to the buffer for subsequent reads.
This operation may always be safely performed with count less than or equal to that specified in the last get(size_t) if no non-const buffer members have been called since then.
If count == 0, nothing happens.
It is an error for count to be greater than ungettable().
"count" is the number of elements
Definition at line 177 of file wvbufbase.h.
Referenced by WvBackslashEncoder::_encode(), WvGzipEncoder::_encode(), WvBackslashDecoder::_encode(), WvSSLStream::uwrite(), WvStream::write(), and wvtcl_getword().
size_t WvBufBaseCommonImpl< T >::ungettable | ( | ) | const [inline, inherited] |
Returns the maximum number of elements that may be ungotten at this time.
Returns: the number of elements
Definition at line 188 of file wvbufbase.h.
const T* WvBufBaseCommonImpl< T >::peek | ( | int | offset, |
size_t | count | ||
) | [inline, inherited] |
Returns a const pointer into the buffer at the specified offset to the specified number of elements without actually adjusting the current get() index.
The pointer is only valid until the next non-const buffer member is called. eg. alloc(size_t)
If count == 0, a NULL pointer may be returned.
If offset is greater than zero, then elements will be returned beginning with the with the offset'th element that would be returned by get(size_t).
If offset equals zero, then elements will be returned beginning with the next one available for get(size_t).
If offset is less than zero, then elements will be returned beginning with the first one that would be returned on a get(size_t) following an unget(-offset).
It is an error for count to be greater than peekable(offset).
For maximum efficiency, call this function multiple times with count no greater than that returned by optpeekable(size_t) at incremental offsets.
"offset" is the buffer offset "count" is the number of elements Returns: the element storage pointer
Definition at line 225 of file wvbufbase.h.
Referenced by WvDBusMsg::demarshal(), WvDBusMsg::demarshal_bytes_needed(), WvHttpStream::execute(), WvBufBaseCommonImpl< unsigned char >::peek(), WvBufBase< unsigned char >::peekch(), WvX509Mgr::sign(), WvBufBase< unsigned char >::strchr(), and WvX509::verify().
T WvBufBaseCommonImpl< T >::peek | ( | int | offset = 0 | ) | [inline, inherited] |
Returns the element at the specified offset in the buffer.
It is an error to invoke this method if used() == 0.
"offset" is the offset, default 0 Returns: the element
Definition at line 286 of file wvbufbase.h.
void WvBufBaseCommonImpl< T >::zap | ( | ) | [inline, inherited] |
Clears the buffer.
For many types of buffers, calling zap() will increased the amount of free space available for writing (see below) by an amount greater than used(). Hence it is wise to zap() a buffer just before writing to it to maximize free space.
After this operation, used() == 0, and often ungettable() == 0.
Definition at line 257 of file wvbufbase.h.
Referenced by WvRSAEncoder::_encode(), WvNullEncoder::_encode(), WvSyslog::_end_line(), WvLogBuffer::_end_line(), WvBackslashDecoder::_reset(), WvStream::unread(), WvSSLStream::uread(), and WvSSLStream::uwrite().
void WvBufBaseCommonImpl< T >::move | ( | T * | buf, |
size_t | count | ||
) | [inline, inherited] |
Efficiently copies the specified number of elements from the buffer to the specified UNINITIALIZED storage location and removes the elements from the buffer.
It is an error for count to be greater than used().
For maximum efficiency, choose as large a count as possible.
The pointer buf may be NULL only if count == 0.
After this operation, an indeterminate number of elements may be ungotten.
"buf" is the buffer that will receive the elements "count" is the number of elements
Definition at line 309 of file wvbufbase.h.
Referenced by WvBufBase< unsigned char >::getstr().
void WvBufBaseCommonImpl< T >::copy | ( | T * | buf, |
int | offset, | ||
size_t | count | ||
) | [inline, inherited] |
Efficiently copies the specified number of elements from the buffer to the specified UNINITIALIZED storage location but does not remove the elements from the buffer.
It is an error for count to be greater than peekable(offset).
For maximum efficiency, choose as large a count as possible.
The pointer buf may be NULL only if count == 0.
"buf" is the buffer that will receive the elements "offset" is the buffer offset "count" is the number of elements
Definition at line 330 of file wvbufbase.h.
bool WvBufBaseCommonImpl< T >::iswritable | ( | ) | const [inline, inherited] |
Returns true if the buffer supports writing.
Returns: true if writing is supported
Definition at line 342 of file wvbufbase.h.
size_t WvBufBaseCommonImpl< T >::free | ( | ) | const [inline, inherited] |
Returns the number of elements that the buffer can currently accept for writing.
Returns: the number of elements
Definition at line 353 of file wvbufbase.h.
Referenced by WvBackslashEncoder::_encode(), WvBackslashDecoder::_encode(), WvStream::read(), WvSSLStream::uread(), and WvSSLStream::uwrite().
T* WvBufBaseCommonImpl< T >::alloc | ( | size_t | count | ) | [inline, inherited] |
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage location owned by the buffer.
The pointer is only valid until the next non-const buffer member is called. eg. alloc(size_t)
If count == 0, a NULL pointer may be returned.
It is an error for count to be greater than free().
For best results, call this function multiple times with count no greater than optallocable() each time.
After this operation, at least count elements may be unallocated.
"count" is the number of elements Returns: the element storage pointer
Definition at line 379 of file wvbufbase.h.
Referenced by WvXOREncoder::_encode(), WvRateAdjust::_encode(), WvBlowfishEncoder::_encode(), WvTripleDESEncoder::_encode(), WvRSAEncoder::_encode(), WvFunctorEncoder< IT, OT, FT >::_typedencode(), WvStream::blocking_getline(), WvDSAKey::getpem(), WvStream::read(), and WvSSLStream::uread().
size_t WvBufBaseCommonImpl< T >::optallocable | ( | ) | const [inline, inherited] |
Returns the optimal maximum number of elements that the buffer can currently accept for writing without incurring significant overhead.
Invariants:
Returns: the number of elements
Definition at line 397 of file wvbufbase.h.
Referenced by WvFunctorEncoder< IT, OT, FT >::_typedencode().
void WvBufBaseCommonImpl< T >::unalloc | ( | size_t | count | ) | [inline, inherited] |
Unallocates exactly the specified number of elements by removing them from the buffer and releasing their storage.
This operation may always be safely performed with count less than or equal to that specified in the last alloc(size_t) or put(const T*, size_t) if no non-const buffer members have been called since then.
If count == 0, nothing happens.
It is an error for count to be greater than unallocable().
"count" is the number of elements
Definition at line 421 of file wvbufbase.h.
Referenced by WvRateAdjust::_encode(), WvCounterModeEncoder::_encode(), WvBackslashDecoder::_encode(), WvRSAEncoder::_encode(), WvStream::blocking_getline(), WvDSAKey::getpem(), WvStream::read(), and WvSSLStream::uread().
size_t WvBufBaseCommonImpl< T >::unallocable | ( | ) | const [inline, inherited] |
Returns the maximum number of elements that may be unallocated at this time.
For all practical purposes, this number will always be at least as large as the amount currently in use. It is provided primarily for symmetry, but also to handle cases where buffer reading (hence used()) is not supported by the implementation.
Invariants:
Returns: the number of elements
Definition at line 443 of file wvbufbase.h.
T* WvBufBaseCommonImpl< T >::mutablepeek | ( | int | offset, |
size_t | count | ||
) | [inline, inherited] |
Returns a non-const pointer info the buffer at the specified offset to the specified number of elements without actually adjusting the current get() index.
Other than the fact that the returned storage is mutable, operates identically to peek(int, size_t).
"offset" is the buffer offset "count" is the number of elements Returns: the element storage pointer
Definition at line 461 of file wvbufbase.h.
Referenced by WvCounterModeEncoder::_encode(), and WvStream::blocking_getline().
void WvBufBaseCommonImpl< T >::put | ( | const T * | data, |
size_t | count | ||
) | [inline, inherited] |
Writes the specified number of elements from the specified storage location into the buffer at its tail.
It is an error for count to be greater than free().
For maximum efficiency, choose as large a count as possible.
The pointer buf may be NULL only if count == 0.
After this operation, at least count elements may be unallocated.
"data" is the buffer that contains the elements "count" is the number of elements
Definition at line 483 of file wvbufbase.h.
Referenced by WvBufBase< unsigned char >::putch(), and WvBufBase< unsigned char >::putstr().
void WvBufBaseCommonImpl< T >::put | ( | T & | value | ) | [inline, inherited] |
Writes the element into the buffer at its tail.
It is an error to invoke this method if free() == 0.
After this operation, at least 1 element may be unallocated.
"valid" is the element
Definition at line 519 of file wvbufbase.h.
void WvBufBaseCommonImpl< T >::poke | ( | const T * | data, |
int | offset, | ||
size_t | count | ||
) | [inline, inherited] |
Efficiently copies the specified number of elements from the specified storage location into the buffer at a particular offset.
If offset <= used() and offset + count > used(), the remaining data is simply tacked onto the end of the buffer with put().
It is an error for count to be greater than free() - offset.
"data" is the buffer that contains the elements "count" is the number of elements "offset" is the buffer offset, default 0
Definition at line 504 of file wvbufbase.h.
Referenced by WvBufBaseCommonImpl< unsigned char >::poke().
void WvBufBaseCommonImpl< T >::poke | ( | T & | value, |
int | offset | ||
) | [inline, inherited] |
Writes the element into the buffer at the specified offset.
It is an error to invoke this method if free() == 0.
After this operation, at least 1 element may be unallocated.
"value" is the element "offset" is the buffer offset
Definition at line 535 of file wvbufbase.h.
void WvBufBaseCommonImpl< T >::merge | ( | Buffer & | inbuf, |
size_t | count | ||
) | [inline, inherited] |
Efficiently moves count bytes from the specified buffer into this one.
In some cases, this may be a zero-copy operation.
It is an error for count to be greater than inbuf.used().
For maximum efficiency, choose as large a count as possible.
After this operation, an indeterminate number of elements may be ungotten from inbuf.
"inbuf" is the buffer from which to read "count" is the number of elements
Definition at line 558 of file wvbufbase.h.
Referenced by WvPassthroughEncoder::_encode(), WvEncoderStream::finish_read(), WvEncoderStream::flush_read(), WvBufBaseCommonImpl< unsigned char >::merge(), WvStream::read(), and WvStream::unread().
void WvBufBaseCommonImpl< T >::merge | ( | Buffer & | inbuf | ) | [inline, inherited] |
Efficiently merges the entire contents of a buffer into this one.
"inbuf" is the buffer from which to read
Definition at line 568 of file wvbufbase.h.