ByteBuffer.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/common/ByteBuffer.cpp#7 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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     // 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 }
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 // End ByteBuffer.cpp

Generated on Mon Jun 22 04:00:17 2009 for Fennel by  doxygen 1.5.1