StreamBuffer.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include <string.h>
00019 #include "debug/DebugUtils.h"
00020 #include "debug/Log.h"
00021 #include "StreamBuffer.h"
00022 
00023 namespace oasys {
00024 
00025 /***************************************************************************
00026  *
00027  * StreamBuffer
00028  *
00029  **************************************************************************/
00030 StreamBuffer::StreamBuffer(size_t size) : 
00031     start_(0), end_(0), size_(size)
00032 {
00033     if (size_ == 0)
00034         size_ = 4;
00035 
00036     buf_ = static_cast<char*>(malloc(size_));
00037     ASSERT(buf_);
00038 }
00039 
00040 StreamBuffer::~StreamBuffer()
00041 {
00042     if (buf_) 
00043     {
00044         free(buf_);
00045         buf_ = 0;
00046     }
00047 }
00048 
00049 void
00050 StreamBuffer::set_size(size_t size)
00051 {
00052     ASSERT(fullbytes() <= size);
00053     moveup();
00054     
00055     realloc(size);
00056 }
00057 
00058 char*
00059 StreamBuffer::start()
00060 {
00061     return &buf_[start_];
00062 }
00063 
00064 char*
00065 StreamBuffer::end()
00066 {
00067     return &buf_[end_];
00068 }
00069 
00070 void
00071 StreamBuffer::reserve(size_t amount)
00072 {
00073     if (amount <= tailbytes())
00074     {
00075         // do nothing
00076     } 
00077     else if (amount <= (start_ + tailbytes())) 
00078     {
00079         moveup();
00080     } 
00081     else
00082     {
00083         moveup();
00084         realloc(((amount + fullbytes())> size_*2) ? 
00085                 (amount + fullbytes()): (size_*2));
00086     }
00087 
00088     ASSERT(amount <= tailbytes());
00089 }
00090 
00091 void
00092 StreamBuffer::fill(size_t amount)
00093 {
00094     ASSERT(amount <= tailbytes());
00095     
00096     end_ += amount;
00097 }
00098 
00099 void
00100 StreamBuffer::consume(size_t amount)
00101 {
00102     ASSERT(amount <= fullbytes());
00103 
00104     start_ += amount;
00105     if (start_ == end_)
00106     {
00107         start_ = end_ = 0;
00108     }
00109 }
00110 
00111 void
00112 StreamBuffer::clear()
00113 {
00114     start_ = end_ = 0;
00115 }
00116 
00117 size_t
00118 StreamBuffer::fullbytes() 
00119 {
00120     return end_ - start_;
00121 }
00122 
00123 size_t
00124 StreamBuffer::tailbytes() 
00125 {
00126     return size_ - end_;
00127 }
00128 
00129 void
00130 StreamBuffer::realloc(size_t size)
00131 {
00132     buf_ = (char*)::realloc(buf_, size);
00133     if (buf_ == 0)
00134     {
00135         logf("/StreamBuffer", LOG_CRIT, "Out of memory");
00136         ASSERT(0);
00137     }
00138     
00139     size_ = size;
00140 }
00141 
00142 void
00143 StreamBuffer::moveup()
00144 {
00145     if (start_ == 0) {
00146         return;
00147     }
00148             
00149     memmove(&buf_[0], &buf_[start_], end_ - start_);
00150     end_   = end_ - start_;
00151     start_ = 0;
00152 }
00153 
00154 } // namespace oasys

Generated on Sat Sep 8 08:43:34 2007 for DTN Reference Implementation by  doxygen 1.5.3