src/tools/tArray.cpp

Go to the documentation of this file.
00001 
00002 /*
00003 
00004 *************************************************************************
00005 
00006 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00007 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00008 
00009 **************************************************************************
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024   
00025 ***************************************************************************
00026 
00027 */
00028 
00029 #ifndef NO_MALLOC_REPLACEMENT
00030 #define NO_MALLOC_REPLACEMENT
00031 #endif
00032 
00033 #include "tMemManager.h"
00034 #include <iostream>
00035 #include <stdlib.h>
00036 #include <string>
00037 #include "tArray.h"
00038 #include <string.h>
00039 
00040 void GrowingArrayBase::ComplainIfFull(){
00041     if (Len()>0)
00042         tERR_ERROR("Array should be empty.");
00043 }
00044 
00045 GrowingArrayBase::GrowingArrayBase(int firstsize,int size_of_T, bool useMalloc)
00046 {
00047     // dump(low,dump,"con:size " << firstsize << ",element size " << size_of_T);
00048     len=firstsize;
00049     if (firstsize) size=firstsize;
00050     else             size=1;
00051 
00052     if ( useMalloc )
00053         base=malloc(size * size_of_T);
00054     else
00055         base=tNEW( char[size * size_of_T] );
00056 
00057     if(NULL==base) {
00058         tERR_ERROR("Error Allocating " << size_of_T*(size) << " bytes." );
00059     }
00060     for(int i=size*size_of_T-1;i>=0;i--)
00061         (reinterpret_cast<char *>(base))[i]=0;
00062 
00063 }
00064 
00065 GrowingArrayBase::~GrowingArrayBase(){
00066     tASSERT( base == NULL );
00067     // dump(very_low,flow,"des");
00068 }
00069 
00070 void GrowingArrayBase::Delete( bool useMalloc ){
00071     // dump(very_low,flow,"des");
00072     if ( useMalloc )
00073     {
00074         free(base);
00075     }
00076     else
00077     {
00078         delete[] (char*)base;
00079     }
00080 
00081     base = NULL;
00082     size = len = 0;
00083 }
00084 
00085 void GrowingArrayBase::ResizeBase(int i,int size_of_T, bool useMalloc){
00086 
00087     i++;
00088 
00089     // dump(very_low,flow,"Array-base resize");
00090 
00091     unsigned int oldsize=size;
00092 
00093     int size_a=i+(1<<12);
00094     int size_b=i+(i>>2);
00095 
00096     int new_size;
00097 
00098     if (size_a<size_b)
00099         new_size=size_a;
00100     else
00101         new_size=size_b;
00102 
00103     //  void *newbase=NULL;//realloc(base,size_of_T*(size));
00104     //  void *newbase=realloc(base,size_of_T*(size));
00105     //  if(NULL==newbase){
00106 
00107     void *newbase;
00108     if ( useMalloc )
00109         newbase = malloc(new_size * size_of_T);
00110     else
00111         newbase=tNEW( char[new_size * size_of_T] );
00112 
00113 
00114     if(NULL==newbase){
00115         tERR_ERROR("Error reallocating " << size_of_T*(new_size) << " bytes.");
00116     }
00117     else{
00118         memcpy(newbase,base,size_of_T*(oldsize));
00119         if ( useMalloc )
00120         {
00121             free(base);
00122         }
00123         else
00124         {
00125             delete[] (char*)base;
00126         }
00127     }
00128     //
00129     size = new_size;
00130 
00131     // clear the newly allocated memory
00132     char *start=(reinterpret_cast<char *>(newbase)) + oldsize*size_of_T;
00133     for(int j=(size-oldsize)*size_of_T-1;j>=0;j--)
00134         start[j]=0;
00135 
00136     base=newbase;
00137 }
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 

Generated on Sat Mar 15 22:55:59 2008 for Armagetron Advanced by  doxygen 1.5.4