|
MemCacheClient
|
Basic buffer with read/write cursors. More...
#include <ReadWriteBuffer.h>
Public Member Functions | |
| ReadWriteBuffer () | |
| constructor | |
| ReadWriteBuffer (const ReadWriteBuffer &) | |
| Create an internal buffer copy of the supplied buffer. | |
| ReadWriteBuffer & | operator= (const ReadWriteBuffer &) |
| Copy the supplied buffer to an internal buffer. | |
| ~ReadWriteBuffer () | |
| destructor | |
| void | Deallocate () |
| Deallocate and clear out any existing buffer. | |
| void | SetEmpty () |
| Clear the existing buffer to zero lengths for read and write buffers. | |
| void | SetExternalBuffer (void *a_pBuf, size_t a_nBufSiz, size_t a_nBufLen=0, size_t a_nGrowBy=0) |
| Set the buffer as an alias to an external buffer. | |
| void | SetInternalBuffer (size_t a_nInitialSize, size_t a_nGrowBy=1024) |
| Replace the current buffer with an internal growable buffer. | |
| char * | GetWriteBuffer (size_t a_nMinBytes=0) |
| Get a write pointer into the buffer guaranteeing that the specified number of bytes are available. | |
| void | CommitWriteBytes (size_t a_nBytes) |
| Commit the specified number of bytes to the buffer. | |
| size_t | GetWriteSize () const |
| Get the available buffer size for writing. | |
| void | WriteBytes (const void *a_pBuf, size_t a_nBufLen) |
| Write the supplied data to the write buffer. | |
| const char * | GetReadBuffer () const |
| Get the read buffer. | |
| void | CommitReadBytes (size_t a_nBytes) |
| Update the read bytes available. | |
| size_t | GetReadSize () const |
| Get the available buffer size for reading. | |
| void | ReadBytes (void *a_pBuf, size_t a_nBufLen) |
| Read the data from the buffer. | |
| void | Compact () |
| Compact the buffer. | |
| bool | operator== (const ReadWriteBuffer &) const |
| Do a memcmp on the read buffers. | |
Private Attributes | |
| size_t | mBufSiz |
| max buf size | |
| size_t | mBufLen |
| committed writes | |
| size_t | mBufIdx |
| committed reads | |
| size_t | mGrowBy |
| growby rate | |
| bool | mIsExt |
| is this an external buffer? | |
| char * | mBuf |
| buffer | |
Basic buffer with read/write cursors.
The buffer may be backed by either an internally allocated and managed growable buffer, or a statically sized external buffer may be used.
Example:
ReadWriteBuffer buf; char * p = buf.GetWriteBuffer(sizeof(data1));
memcpy(p, data1, sizeof(data1));
buf.CommitWriteBytes(sizeof(data1)); p = buf.GetWriteBuffer(sizeof(data2));
memcpy(p, data2, sizeof(data2));
buf.CommitWriteBytes(sizeof(data2)); while (buf.GetReadSize() > 0) {
fputc(*buf.GetReadBuffer(), fp);
buf.CommitReadBytes(1);
}
Definition at line 42 of file ReadWriteBuffer.h.
| ReadWriteBuffer::ReadWriteBuffer | ( | const ReadWriteBuffer & | rhs | ) |
Create an internal buffer copy of the supplied buffer.
Only the committed data will be copied. The available bytes may be different to the source buffer.
Definition at line 25 of file ReadWriteBuffer.cpp.
References operator=().
| void ReadWriteBuffer::CommitReadBytes | ( | size_t | a_nBytes | ) |
Update the read bytes available.
Indicate that the specified number of bytes have been read and are not necessary anymore. These bytes are now available for reclaiming by Compact().
| a_nBytes | Number of bytes read |
| std::invalid_argument | if more bytes were committed then is available |
Definition at line 195 of file ReadWriteBuffer.cpp.
References mBufIdx, and mBufLen.
Referenced by ReadBytes(), and MemCacheClient::Store().
| void ReadWriteBuffer::CommitWriteBytes | ( | size_t | a_nBytes | ) |
Commit the specified number of bytes to the buffer.
This function should be called after the buffer returned by GetWriteBuffer() is written to. After calling this function, the specified bytes will then be available for reading from the GetReadBuffer() and GetReadSize() functions.
| a_nBytes | Number of bytes written |
| std::invalid_argument | if more bytes were committed then is available |
Definition at line 163 of file ReadWriteBuffer.cpp.
References mBufLen, and mBufSiz.
Referenced by MemCacheClient::HandleGetResponse(), and WriteBytes().
| void ReadWriteBuffer::Compact | ( | ) |
Compact the buffer.
All remaining unread bytes are moved to the beginning of the buffer leaving the largest size available for writing.
Definition at line 225 of file ReadWriteBuffer.cpp.
References GetReadBuffer(), GetReadSize(), mBuf, mBufIdx, and mBufLen.
| const char * ReadWriteBuffer::GetReadBuffer | ( | ) | const |
Get the read buffer.
See GetReadSize() for the number of bytes available to be read from this pointer.
Definition at line 189 of file ReadWriteBuffer.cpp.
Referenced by Compact(), operator==(), and ReadBytes().
| size_t ReadWriteBuffer::GetReadSize | ( | ) | const |
Get the available buffer size for reading.
Definition at line 204 of file ReadWriteBuffer.cpp.
References mBufIdx, and mBufLen.
Referenced by Compact(), operator==(), ReadBytes(), and MemCacheClient::Store().
| char * ReadWriteBuffer::GetWriteBuffer | ( | size_t | a_nMinBytes = 0 | ) |
Get a write pointer into the buffer guaranteeing that the specified number of bytes are available.
Call CommitWriteBytes() after accessing the buffer to specify the number of bytes actually written.
| a_nMinBytes | Minimum writable size required for the returned pointer. If 0 then the currently available buffer will be returned without resizing it. The available bytes for writing can be determined via GetWriteSize(). |
| std::overflow_error | when an external buffer is used, conversion to an internal buffer on overflow is disabled, and a_nMinBytes is not available in the buffer. |
| std::bad_alloc | when using an internal buffer and there was an out of memory error on resizing. |
Definition at line 134 of file ReadWriteBuffer.cpp.
References mBuf, mBufLen, mBufSiz, mGrowBy, mIsExt, and roundup().
Referenced by MemCacheClient::HandleGetResponse(), operator=(), and WriteBytes().
| size_t ReadWriteBuffer::GetWriteSize | ( | ) | const |
Get the available buffer size for writing.
Definition at line 172 of file ReadWriteBuffer.cpp.
| ReadWriteBuffer & ReadWriteBuffer::operator= | ( | const ReadWriteBuffer & | rhs | ) |
Copy the supplied buffer to an internal buffer.
Only the committed data will be copied. The available bytes may be different to the source buffer.
Definition at line 39 of file ReadWriteBuffer.cpp.
References Deallocate(), GetWriteBuffer(), mBuf, mBufIdx, mBufLen, mBufSiz, mGrowBy, and SetInternalBuffer().
Referenced by ReadWriteBuffer().
| void ReadWriteBuffer::ReadBytes | ( | void * | a_pBuf, |
| size_t | a_nBufLen | ||
| ) |
Read the data from the buffer.
This function is a simple wrapper around calls to GetReadSize, GetReadBuffer, memcpy, CommitReadBytes.
| a_pBuf | Location for the buffer data to be copied to |
| a_nBufLen | Length of the data in bytes to copy |
| std::invalid_argument | not enough bytes in the buffer |
Definition at line 210 of file ReadWriteBuffer.cpp.
References CommitReadBytes(), GetReadBuffer(), and GetReadSize().
| void ReadWriteBuffer::SetExternalBuffer | ( | void * | a_pBuf, |
| size_t | a_nBufSiz, | ||
| size_t | a_nBufLen = 0, |
||
| size_t | a_nGrowBy = 0 |
||
| ) |
Set the buffer as an alias to an external buffer.
| a_pBuf | Buffer to use as external store. |
| a_nBufSiz | Maximum size of the buffer for writing. |
| a_nBufLen | Current length of readable data in buffer. |
| a_nGrowBy | When 0, running out of space in the buffer throws an exception. When > 0, the buffer is automatically converted to an internal buffer with this growby value. |
| std::invalid_argument |
Definition at line 82 of file ReadWriteBuffer.cpp.
References Deallocate(), mBuf, mBufIdx, mBufLen, mBufSiz, mGrowBy, and mIsExt.
| void ReadWriteBuffer::SetInternalBuffer | ( | size_t | a_nInitialSize, |
| size_t | a_nGrowBy = 1024 |
||
| ) |
Replace the current buffer with an internal growable buffer.
Internal buffers will be grown by adding a_nMinBytes rounded up to an even multiples of a_nGrowBy.
| a_nInitialSize | Initial size to create the internal buffer at |
| a_nGrowBy | Number of bytes to grow the buffer by whenever it runs out of space. |
Definition at line 115 of file ReadWriteBuffer.cpp.
References Deallocate(), mBuf, mBufSiz, mGrowBy, and roundup().
Referenced by operator=().
| void ReadWriteBuffer::WriteBytes | ( | const void * | a_pBuf, |
| size_t | a_nBufLen | ||
| ) |
Write the supplied data to the write buffer.
This function is a simple wrapper around calls to GetWriteBuffer, memcpy, CommitWriteBytes.
| a_pBuf | Data to copy to the buffer |
| a_nBufLen | Length of the data in bytes to copy |
| std::overflow_error | when an external buffer is used and a_nBufLen is not available. |
| std::bad_alloc | when using an internal buffer and there was an out of memory error on resizing. |
Definition at line 178 of file ReadWriteBuffer.cpp.
References CommitWriteBytes(), and GetWriteBuffer().
1.7.3