ByteBuffer Class Reference

ByteBuffer allows access to an array of buffers as a single memory space. More...

#include <ByteBuffer.h>

List of all members.

Public Member Functions

 ByteBuffer ()
void init (boost::shared_array< PBuffer > ppBuffers, uint nBuffers, uint bufSize)
 Provides storage for the virtual byte buffer.
uint getSize ()
 Returns the size of the buffer, in bytes.
UnsignedByte getByte (uint pos)
 Returns the byte at pos.
void setByte (uint pos, UnsignedByte b)
 Sets byte at pos.
void mergeByte (uint pos, UnsignedByte b)
 Merges (OR's) byte at pos.
PBuffer getMem (uint pos, uint &size)
 Returns a pointer to contiguous memory.
void setMem (uint pos, UnsignedByte value, uint len)
 Initializes a run of bytes in the buffer.
void copyMem (uint pos, PConstBuffer mem, uint len)
 Copies a run of bytes into the buffer.
void mergeMem (uint pos, PConstBuffer mem, uint len)
 Merges (OR's) a run of bytes into the buffer; similar to memmerge.

Private Member Functions

uint getI (uint pos)
 Returns which buffer to use to access pos.
uint getJ (uint pos)
 Returns offset within buffer to use to access pos.

Static Private Member Functions

static void memmerge (PBuffer dest, PConstBuffer src, uint len)
 Merges (OR's) one buffer into another.

Private Attributes

boost::shared_array< PBufferppBuffers
uint nBuffers
uint bufferSize
uint bufferMask
uint bufferShift


Detailed Description

ByteBuffer allows access to an array of buffers as a single memory space.

It allows for optimization with direct memory access. It has an interface for accessing one byte at a time (less overhead), and an interface for accessing runs of bytes (more overhead, but processes one memory chunk at a time). The methods setMem and copyMem are similar to memset and memcpy.

This class neither allocates nor deallocates memory, except through shared pointers. As usual, indexes are zero-based.

Author:
John Pham
Version:
Id
//open/dev/fennel/common/ByteBuffer.h#9

Definition at line 48 of file ByteBuffer.h.


Constructor & Destructor Documentation

ByteBuffer::ByteBuffer (  )  [explicit]

Definition at line 27 of file ByteBuffer.cpp.

References bufferSize, and nBuffers.

00028 {
00029     nBuffers = 0;
00030     bufferSize = 0;
00031 }


Member Function Documentation

uint ByteBuffer::getI ( uint  pos  )  [inline, private]

Returns which buffer to use to access pos.

Definition at line 59 of file ByteBuffer.h.

00060     {
00061         uint i = pos >> bufferShift;
00062         assert(i < nBuffers);
00063         return i;
00064     }

uint ByteBuffer::getJ ( uint  pos  )  [inline, private]

Returns offset within buffer to use to access pos.

Definition at line 69 of file ByteBuffer.h.

00070     {
00071         uint j = pos & bufferMask;
00072         assert(j < bufferSize);
00073         return j;
00074     }

static void ByteBuffer::memmerge ( PBuffer  dest,
PConstBuffer  src,
uint  len 
) [inline, static, private]

Merges (OR's) one buffer into another.

Definition at line 79 of file ByteBuffer.h.

Referenced by mergeMem().

00080     {
00081         PBuffer end = dest + len;
00082         while (dest < end) {
00083             *dest++ |= *src++;
00084         }
00085     }

void ByteBuffer::init ( boost::shared_array< PBuffer ppBuffers,
uint  nBuffers,
uint  bufSize 
)

Provides storage for the virtual byte buffer.

Definition at line 33 of file ByteBuffer.cpp.

References bufferMask, bufferShift, and bufferSize.

Referenced by LbmUnionExecStream::open().

00035 {
00036     this->ppBuffers = ppBuffers;
00037     this->nBuffers = nBuffers;
00038     this->bufferSize = bufSize;
00039 
00040     // Currently we use bit masking (rather than mod) to calculate virtual
00041     // offsets. This scheme requires that the buffer size be a power of 2.
00042     // If this cannot be guaranteed, we should change to another scheme.
00043     bufferMask = bufSize - 1;
00044     assert((bufferMask & bufSize) == 0);
00045 
00046     bufferShift = 0;
00047     uint tmp = bufferMask;
00048     while (tmp > 0) {
00049         bufferShift++;
00050         tmp >>=1;
00051     }
00052 }

uint ByteBuffer::getSize (  ) 

Returns the size of the buffer, in bytes.

Definition at line 54 of file ByteBuffer.cpp.

References bufferSize, and nBuffers.

Referenced by copyMem(), mergeMem(), and setMem().

00055 {
00056     return nBuffers * bufferSize;
00057 }

UnsignedByte ByteBuffer::getByte ( uint  pos  )  [inline]

Returns the byte at pos.

Definition at line 104 of file ByteBuffer.h.

00105     {
00106         return ppBuffers[getI(pos)][getJ(pos)];
00107     }

void ByteBuffer::setByte ( uint  pos,
UnsignedByte  b 
) [inline]

Sets byte at pos.

Definition at line 112 of file ByteBuffer.h.

00113     {
00114         ppBuffers[getI(pos)][getJ(pos)] = b;
00115     }

void ByteBuffer::mergeByte ( uint  pos,
UnsignedByte  b 
) [inline]

Merges (OR's) byte at pos.

Definition at line 120 of file ByteBuffer.h.

00121     {
00122         ppBuffers[getI(pos)][getJ(pos)] |= b;
00123     }

PBuffer ByteBuffer::getMem ( uint  pos,
uint size 
) [inline]

Returns a pointer to contiguous memory.

Parameters:
pos memory position
size returns size of the contiguous memory

Definition at line 131 of file ByteBuffer.h.

Referenced by copyMem(), mergeMem(), and setMem().

00132     {
00133         size = bufferSize - getJ(pos);
00134         return &ppBuffers[getI(pos)][getJ(pos)];
00135     }

void ByteBuffer::setMem ( uint  pos,
UnsignedByte  value,
uint  len 
)

Initializes a run of bytes in the buffer.

Definition at line 59 of file ByteBuffer.cpp.

References getMem(), and getSize().

00060 {
00061     assert(pos + len <= getSize());
00062     uint current = pos;
00063     uint remaining = len;
00064 
00065     while (remaining > 0) {
00066         uint chunkLen;
00067         PBuffer mem = getMem(current, chunkLen);
00068         if (chunkLen >= remaining) {
00069             memset(mem, value, remaining);
00070             break;
00071         }
00072         memset(mem, value, chunkLen);
00073         current += chunkLen;
00074         remaining -= chunkLen;
00075     }
00076 }

void ByteBuffer::copyMem ( uint  pos,
PConstBuffer  mem,
uint  len 
)

Copies a run of bytes into the buffer.

Definition at line 78 of file ByteBuffer.cpp.

References getMem(), and getSize().

00079 {
00080     assert(pos + len <= getSize());
00081     uint current = pos;
00082     PConstBuffer currentData = data;
00083     uint remaining = len;
00084 
00085     while (remaining > 0) {
00086         uint chunkLen;
00087         PBuffer mem = getMem(current, chunkLen);
00088         if (chunkLen >= remaining) {
00089             memcpy(mem, currentData, remaining);
00090             break;
00091         }
00092         memcpy(mem, currentData, chunkLen);
00093         current += chunkLen;
00094         currentData += chunkLen;
00095         remaining -= chunkLen;
00096     }
00097 }

void ByteBuffer::mergeMem ( uint  pos,
PConstBuffer  mem,
uint  len 
)

Merges (OR's) a run of bytes into the buffer; similar to memmerge.

Definition at line 99 of file ByteBuffer.cpp.

References getMem(), getSize(), and memmerge().

00100 {
00101     assert(pos + len <= getSize());
00102     uint current = pos;
00103     PConstBuffer currentData = data;
00104     uint remaining = len;
00105 
00106     while (remaining > 0) {
00107         uint chunkLen;
00108         PBuffer mem = getMem(current, chunkLen);
00109         if (chunkLen >= remaining) {
00110             memmerge(mem, currentData, remaining);
00111             break;
00112         }
00113         memmerge(mem, currentData, chunkLen);
00114         current += chunkLen;
00115         currentData += chunkLen;
00116         remaining -= chunkLen;
00117     }
00118 }


Member Data Documentation

boost::shared_array<PBuffer> ByteBuffer::ppBuffers [private]

Definition at line 50 of file ByteBuffer.h.

uint ByteBuffer::nBuffers [private]

Definition at line 51 of file ByteBuffer.h.

Referenced by ByteBuffer(), and getSize().

uint ByteBuffer::bufferSize [private]

Definition at line 52 of file ByteBuffer.h.

Referenced by ByteBuffer(), getSize(), and init().

uint ByteBuffer::bufferMask [private]

Definition at line 53 of file ByteBuffer.h.

Referenced by init().

uint ByteBuffer::bufferShift [private]

Definition at line 54 of file ByteBuffer.h.

Referenced by init().


The documentation for this class was generated from the following files:
Generated on Mon Jun 22 04:00:26 2009 for Fennel by  doxygen 1.5.1