00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "fennel/common/CommonPreamble.h"
00024 #include "fennel/common/ByteBuffer.h"
00025
00026 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/common/ByteBuffer.cpp#7 $");
00027 ByteBuffer::ByteBuffer()
00028 {
00029 nBuffers = 0;
00030 bufferSize = 0;
00031 }
00032
00033 void ByteBuffer::init(
00034 boost::shared_array<PBuffer> ppBuffers, uint nBuffers, uint bufSize)
00035 {
00036 this->ppBuffers = ppBuffers;
00037 this->nBuffers = nBuffers;
00038 this->bufferSize = bufSize;
00039
00040
00041
00042
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 }
00053
00054 uint ByteBuffer::getSize()
00055 {
00056 return nBuffers * bufferSize;
00057 }
00058
00059 void ByteBuffer::setMem(uint pos, UnsignedByte value, uint len)
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 }
00077
00078 void ByteBuffer::copyMem(uint pos, PConstBuffer data, uint len)
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 }
00098
00099 void ByteBuffer::mergeMem(uint pos, PConstBuffer data, uint len)
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 }
00119
00120 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/common/ByteBuffer.cpp#7 $");
00121
00122