00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef __GNUG__
00024 # pragma implementation "PreAllocBuffer.h"
00025 #endif
00026
00027 #include "StrMod/PreAllocBuffer.h"
00028 #include <iostream>
00029 #include <cstdlib>
00030 #include <cstring>
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 };
00140 };