#include <ByteBuffer.h>
Public Member Functions | |
void | init (SharedByteBuffer pBuffer) |
Initialize a buffer, valid from index 0. | |
void | reset () |
Reinitializes buffer to empty condition. | |
uint | getCapacity () const |
Returns the capacity of the buffer. | |
IndexT | getStart () const |
Returns the index of the first entry stored in the buffer. | |
IndexT | getEnd () const |
Returns the index of the end of data (the first entry with no data). | |
IndexT | getLimit () |
Returns the write limit of the buffer (the first invalid index). | |
PBuffer | getMem (IndexT index, uint &size) |
Returns contiguous memory at position. | |
void | advance (IndexT pos) |
Advances the start of the buffer, releasing part of the buffer to store more values. | |
void | advanceEnd (IndexT pos) |
Advances the end pointer, initializing new entries to zero. | |
UnsignedByte | getByte (IndexT index) const |
Returns the value of the byte at the specified index. | |
void | mergeByte (IndexT index, UnsignedByte val) |
Merge a byte into the buffer. | |
void | mergeMem (IndexT index, PConstBuffer byteSeg, uint len) |
Merge a series of bytes into the buffer. | |
Private Member Functions | |
uint | getOffset (IndexT index) const |
Retrieves the offset of index in buffer. | |
uint | getUnwrappedMemSize (IndexT index) |
Returns size of buffer starting at index, which is not wrapped by the circular buffer. | |
Private Attributes | |
SharedByteBuffer | pBuffer |
Internal buffer. | |
uint | windowSize |
Maximum size of window. | |
uint | startOffset |
Offset of current start index. | |
IndexT | start |
Current start index; buffer has no data for index < start. | |
IndexT | end |
Index not yet written; buffer has no data for index >= end. |
It's like an array whose start and end are not fixed. At any time, the buffer is valid for entries between start and end. However, the start may be advanced, as data is read. The end may be advanced as more data is written. The amount of data is limited by the buffer's capacity, and data cannot be written past the limit.
The contents of a ByteWindow can be considered to be initialized to zero. A ByteWindow neither allocates nor frees any memory. It is templatized to support different kinds of indexes.
Definition at line 166 of file ByteBuffer.h.
uint ByteWindow< IndexT >::getOffset | ( | IndexT | index | ) | const [inline, private] |
Retrieves the offset of index in buffer.
This call will fail if the index is invalid
Definition at line 197 of file ByteBuffer.h.
References ByteWindow< IndexT >::start, ByteWindow< IndexT >::startOffset, and ByteWindow< IndexT >::windowSize.
Referenced by ByteWindow< IndexT >::advance(), ByteWindow< IndexT >::advanceEnd(), ByteWindow< IndexT >::getByte(), ByteWindow< IndexT >::getMem(), ByteWindow< IndexT >::getUnwrappedMemSize(), ByteWindow< IndexT >::mergeByte(), and ByteWindow< IndexT >::mergeMem().
00198 { 00199 assert(index >= start); 00200 assert(index < start + windowSize); 00201 uint pos = index - start + startOffset; 00202 if (pos >= windowSize) { 00203 pos -= windowSize; 00204 } 00205 return pos; 00206 }
uint ByteWindow< IndexT >::getUnwrappedMemSize | ( | IndexT | index | ) | [inline, private] |
Returns size of buffer starting at index, which is not wrapped by the circular buffer.
Definition at line 212 of file ByteBuffer.h.
References ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::start, and ByteWindow< IndexT >::windowSize.
Referenced by ByteWindow< IndexT >::advanceEnd(), and ByteWindow< IndexT >::mergeMem().
00213 { 00214 assert(start <= index && index <= start + windowSize); 00215 return windowSize - getOffset(index); 00216 }
void ByteWindow< IndexT >::init | ( | SharedByteBuffer | pBuffer | ) | [inline] |
Initialize a buffer, valid from index 0.
Definition at line 222 of file ByteBuffer.h.
References ByteWindow< IndexT >::reset(), and ByteWindow< IndexT >::windowSize.
00223 { 00224 this->pBuffer = pBuffer; 00225 windowSize = pBuffer->getSize(); 00226 reset(); 00227 }
void ByteWindow< IndexT >::reset | ( | ) | [inline] |
Reinitializes buffer to empty condition.
Definition at line 232 of file ByteBuffer.h.
References ByteWindow< IndexT >::end, ByteWindow< IndexT >::start, and ByteWindow< IndexT >::startOffset.
Referenced by ByteWindow< IndexT >::init().
00233 { 00234 startOffset = 0; 00235 start = end = 0; 00236 }
uint ByteWindow< IndexT >::getCapacity | ( | ) | const [inline] |
Returns the capacity of the buffer.
Definition at line 241 of file ByteBuffer.h.
References ByteWindow< IndexT >::windowSize.
00242 { 00243 return windowSize; 00244 }
IndexT ByteWindow< IndexT >::getStart | ( | ) | const [inline] |
Returns the index of the first entry stored in the buffer.
Definition at line 249 of file ByteBuffer.h.
References ByteWindow< IndexT >::start.
00250 { 00251 return start; 00252 }
IndexT ByteWindow< IndexT >::getEnd | ( | ) | const [inline] |
Returns the index of the end of data (the first entry with no data).
Definition at line 257 of file ByteBuffer.h.
References ByteWindow< IndexT >::end.
00258 { 00259 return end; 00260 }
IndexT ByteWindow< IndexT >::getLimit | ( | ) | [inline] |
Returns the write limit of the buffer (the first invalid index).
Definition at line 265 of file ByteBuffer.h.
References ByteWindow< IndexT >::start, and ByteWindow< IndexT >::windowSize.
Referenced by ByteWindow< IndexT >::advanceEnd(), ByteWindow< IndexT >::mergeByte(), and ByteWindow< IndexT >::mergeMem().
00266 { 00267 return start + windowSize; 00268 }
PBuffer ByteWindow< IndexT >::getMem | ( | IndexT | index, | |
uint & | size | |||
) | [inline] |
Returns contiguous memory at position.
Definition at line 273 of file ByteBuffer.h.
References ByteWindow< IndexT >::getOffset(), and ByteWindow< IndexT >::pBuffer.
void ByteWindow< IndexT >::advance | ( | IndexT | pos | ) | [inline] |
Advances the start of the buffer, releasing part of the buffer to store more values.
Definition at line 282 of file ByteBuffer.h.
References ByteWindow< IndexT >::end, ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::start, and ByteWindow< IndexT >::startOffset.
00283 { 00284 assert (pos >= start); 00285 00286 // If we not keeping any data, reset the start pointer to the 00287 // beginning of the internal buffer. Otherwise, advance it. 00288 if (pos >= end) { 00289 startOffset = 0; 00290 end = pos; 00291 } else { 00292 startOffset = getOffset(pos); 00293 } 00294 start = pos; 00295 }
void ByteWindow< IndexT >::advanceEnd | ( | IndexT | pos | ) | [inline] |
Advances the end pointer, initializing new entries to zero.
Definition at line 300 of file ByteBuffer.h.
References ByteWindow< IndexT >::end, ByteWindow< IndexT >::getLimit(), ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::getUnwrappedMemSize(), min(), and ByteWindow< IndexT >::pBuffer.
Referenced by ByteWindow< IndexT >::mergeByte(), and ByteWindow< IndexT >::mergeMem().
00301 { 00302 assert(pos > end); 00303 assert(pos <= getLimit()); 00304 00305 uint len = pos - end; 00306 uint chunkSize = std::min(len, getUnwrappedMemSize(end)); 00307 pBuffer->setMem(getOffset(end), 0, chunkSize); 00308 if (len > chunkSize) { 00309 pBuffer->setMem(0, 0, len - chunkSize); 00310 } 00311 end = pos; 00312 }
UnsignedByte ByteWindow< IndexT >::getByte | ( | IndexT | index | ) | const [inline] |
Returns the value of the byte at the specified index.
Definition at line 317 of file ByteBuffer.h.
References ByteWindow< IndexT >::end, ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::pBuffer, and ByteWindow< IndexT >::start.
00318 { 00319 assert(index >= start && index < end); 00320 return pBuffer->getByte(getOffset(index)); 00321 }
void ByteWindow< IndexT >::mergeByte | ( | IndexT | index, | |
UnsignedByte | val | |||
) | [inline] |
Merge a byte into the buffer.
Definition at line 326 of file ByteBuffer.h.
References ByteWindow< IndexT >::advanceEnd(), ByteWindow< IndexT >::end, ByteWindow< IndexT >::getLimit(), ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::pBuffer, and ByteWindow< IndexT >::start.
00327 { 00328 assert(index >= start); 00329 assert(index < getLimit()); 00330 if (index >= end) { 00331 advanceEnd(index + 1); 00332 } 00333 pBuffer->mergeByte(getOffset(index), val); 00334 }
void ByteWindow< IndexT >::mergeMem | ( | IndexT | index, | |
PConstBuffer | byteSeg, | |||
uint | len | |||
) | [inline] |
Merge a series of bytes into the buffer.
Definition at line 339 of file ByteBuffer.h.
References ByteWindow< IndexT >::advanceEnd(), ByteWindow< IndexT >::end, ByteWindow< IndexT >::getLimit(), ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::getUnwrappedMemSize(), min(), ByteWindow< IndexT >::pBuffer, ByteWindow< IndexT >::start, and ByteWindow< IndexT >::windowSize.
00340 { 00341 assert(index >= start); 00342 assert(len < windowSize); 00343 assert(index + len <= getLimit()); 00344 assert(len > 0); 00345 00346 // zero from end to index 00347 IndexT writeEnd = index + len; 00348 if (writeEnd > end) { 00349 advanceEnd(writeEnd); 00350 } 00351 00352 uint chunkSize = std::min(len, getUnwrappedMemSize(index)); 00353 pBuffer->mergeMem(getOffset(index), byteSeg, chunkSize); 00354 if (len > chunkSize) { 00355 pBuffer->mergeMem(0, byteSeg + chunkSize, len - chunkSize); 00356 } 00357 }
SharedByteBuffer ByteWindow< IndexT >::pBuffer [private] |
Internal buffer.
Definition at line 171 of file ByteBuffer.h.
Referenced by ByteWindow< IndexT >::advanceEnd(), ByteWindow< IndexT >::getByte(), ByteWindow< IndexT >::getMem(), ByteWindow< IndexT >::mergeByte(), and ByteWindow< IndexT >::mergeMem().
uint ByteWindow< IndexT >::windowSize [private] |
Maximum size of window.
Definition at line 176 of file ByteBuffer.h.
Referenced by ByteWindow< IndexT >::getCapacity(), ByteWindow< IndexT >::getLimit(), ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::getUnwrappedMemSize(), ByteWindow< IndexT >::init(), and ByteWindow< IndexT >::mergeMem().
uint ByteWindow< IndexT >::startOffset [private] |
Offset of current start index.
Definition at line 181 of file ByteBuffer.h.
Referenced by ByteWindow< IndexT >::advance(), ByteWindow< IndexT >::getOffset(), and ByteWindow< IndexT >::reset().
IndexT ByteWindow< IndexT >::start [private] |
Current start index; buffer has no data for index < start.
Definition at line 186 of file ByteBuffer.h.
Referenced by ByteWindow< IndexT >::advance(), ByteWindow< IndexT >::getByte(), ByteWindow< IndexT >::getLimit(), ByteWindow< IndexT >::getOffset(), ByteWindow< IndexT >::getStart(), ByteWindow< IndexT >::getUnwrappedMemSize(), ByteWindow< IndexT >::mergeByte(), ByteWindow< IndexT >::mergeMem(), and ByteWindow< IndexT >::reset().
IndexT ByteWindow< IndexT >::end [private] |
Index not yet written; buffer has no data for index >= end.
Definition at line 191 of file ByteBuffer.h.
Referenced by ByteWindow< IndexT >::advance(), ByteWindow< IndexT >::advanceEnd(), ByteWindow< IndexT >::getByte(), ByteWindow< IndexT >::getEnd(), ByteWindow< IndexT >::mergeByte(), ByteWindow< IndexT >::mergeMem(), and ByteWindow< IndexT >::reset().