MemCacheClient
Public Member Functions | Private Attributes

ReadWriteBuffer Class Reference

Basic buffer with read/write cursors. More...

#include <ReadWriteBuffer.h>

List of all members.

Public Member Functions

 ReadWriteBuffer ()
 constructor
 ReadWriteBuffer (const ReadWriteBuffer &)
 Create an internal buffer copy of the supplied buffer.
ReadWriteBufferoperator= (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

Detailed Description

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.


Constructor & Destructor Documentation

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=().


Member Function Documentation

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().

Parameters:
a_nBytesNumber of bytes read
Exceptions:
std::invalid_argumentif 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.

Parameters:
a_nBytesNumber of bytes written
Exceptions:
std::invalid_argumentif 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.

Returns:
Pointer to the data to be read.

Definition at line 189 of file ReadWriteBuffer.cpp.

References mBuf, and mBufIdx.

Referenced by Compact(), operator==(), and ReadBytes().

size_t ReadWriteBuffer::GetReadSize ( ) const

Get the available buffer size for reading.

Returns:
number of bytes available 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.

Parameters:
a_nMinBytesMinimum 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().
Exceptions:
std::overflow_errorwhen 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_allocwhen using an internal buffer and there was an out of memory error on resizing.
Returns:
Writable pointer to a buffer with at least the requested number of bytes available for writing. See the function GetWriteSize() to find the actual number of bytes available.

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.

Returns:
number of bytes available in the write buffer returned from GetWriteBuffer()

Definition at line 172 of file ReadWriteBuffer.cpp.

References mBufLen, and mBufSiz.

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.

Parameters:
a_pBufLocation for the buffer data to be copied to
a_nBufLenLength of the data in bytes to copy
Exceptions:
std::invalid_argumentnot 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.

Parameters:
a_pBufBuffer to use as external store.
a_nBufSizMaximum size of the buffer for writing.
a_nBufLenCurrent length of readable data in buffer.
a_nGrowByWhen 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.
Exceptions:
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.

Parameters:
a_nInitialSizeInitial size to create the internal buffer at
a_nGrowByNumber 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.

Parameters:
a_pBufData to copy to the buffer
a_nBufLenLength of the data in bytes to copy
Exceptions:
std::overflow_errorwhen an external buffer is used and a_nBufLen is not available.
std::bad_allocwhen 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().


The documentation for this class was generated from the following files: