Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

PreAllocBuffer.cxx

00001 /*
00002  * Copyright (C) 1991-9 Eric M. Hopper <hopper@omnifarious.mn.org>
00003  * 
00004  *     This program is free software; you can redistribute it and/or modify it
00005  *     under the terms of the GNU Lesser General Public License as published
00006  *     by the Free Software Foundation; either version 2 of the License, or
00007  *     (at your option) any later version.
00008  * 
00009  *     This program is distributed in the hope that it will be useful, but
00010  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *     Lesser General Public License for more details.
00013  * 
00014  *     You should have received a copy of the GNU Lesser General Public
00015  *     License along with this program; if not, write to the Free Software
00016  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 /* $Header: /home/hopper/src/cvs/C++/StrMod/PreAllocBuffer.cxx,v 1.4 2002/02/05 22:50:06 hopper Exp $ */
00020 
00021 // For a log, see ChangeLog
00022 
00023 #ifdef __GNUG__
00024 #  pragma implementation "PreAllocBuffer.h"
00025 #endif
00026 
00027 #include "StrMod/PreAllocBuffer.h"
00028 #include <iostream>
00029 #include <cstdlib>  // malloc, realloc, and free.
00030 #include <cstring>  // memcpy
00031 
00032 namespace strmod {
00033 namespace strmod {
00034 
00035 const STR_ClassIdent PreAllocBufferBase::identifier(38UL);
00036 
00037 void
00038 PreAllocBufferBase::a_silly_member_function_to_make_sure_a_vtable_is_generated()
00039 {
00040 }
00041 
00042 PreAllocBufferBase::~PreAllocBufferBase()
00043 {
00044 }
00045 
00046 void PreAllocBufferBase::i_destruct(const U1Byte * const preallocbuf)
00047 {
00048    if (buf_ != preallocbuf)
00049    {
00050       if (buf_ != 0)
00051       {
00052          free(buf_);
00053       }
00054    }
00055    buf_ = 0;
00056    buflen_ = 0;
00057 }
00058 
00059 void PreAllocBufferBase::i_resize(const unsigned int newsize,
00060                                   const unsigned int prebufsize,
00061                                   U1Byte * const preallocbuf) throw(std::bad_alloc)
00062 {
00063    if (newsize <= prebufsize)
00064    {
00065       if (buf_ != preallocbuf)
00066       {
00067          if (buf_ != 0)
00068          {
00069             memcpy(preallocbuf, buf_, newsize);
00070             free(buf_);
00071             buf_ = 0;
00072          }
00073          if (newsize != 0)
00074          {
00075             buf_ = preallocbuf;
00076          }
00077       }
00078       buflen_ = newsize;
00079    }
00080    else
00081    {
00082       U1Byte *newbuf = 0;
00083 
00084       if ((buf_ == preallocbuf) || (buf_ == 0))
00085       {
00086          newbuf = static_cast<U1Byte *>(malloc(newsize));
00087          if ((newbuf != 0) && (buf_ != 0))
00088          {
00089             assert(buflen_ <= newsize);
00090             memcpy(newbuf, buf_, buflen_);
00091          }
00092       }
00093       else
00094       {
00095          newbuf = static_cast<U1Byte *>(realloc(buf_, newsize));
00096       }
00097       if (newbuf == 0)
00098       {
00099          throw std::bad_alloc();
00100       }
00101       buf_ = newbuf;
00102       buflen_ = newsize;
00103    }
00104 }
00105 
00106 bool PreAllocBufferBase::i_invariant(const unsigned int prebufsize,
00107                                      const void * const prebuf) const
00108 {
00109    if (buflen_ == 0)
00110    {
00111       return((buf_ == 0) || (buf_ == prebuf));
00112    }
00113    else if (buflen_ <= prebufsize)
00114    {
00115       return(buf_ == prebuf);
00116    }
00117    else
00118    {
00119       return(buf_ != 0);
00120    }
00121 }
00122 
00123 void PreAllocBufferBase::i_printState(std::ostream &os,
00124                                       const unsigned int prebufsize,
00125                                       const void * const prebuf) const
00126 {
00127    os << "PreAllocBuffer<" << prebufsize << ">(buf_ == ";
00128    if (buf_ == prebuf)
00129    {
00130       os << "preallocbuf_";
00131    }
00132    else
00133    {
00134       os << buf_;
00135    }
00136    os << ", buflen_ == " << buflen_ << ", prebufsize == " << prebufsize << ")";
00137 }
00138 
00139 };  // End namespace strmod
00140 };  // End namespace strmod

Generated on Wed Jan 29 00:32:44 2003 for libNet by doxygen1.3-rc1