00001 /* 00002 00003 ************************************************************************* 00004 00005 ArmageTron -- Just another Tron Lightcycle Game in 3D. 00006 Copyright (C) 2000 Manuel Moos (manuel@moosnet.de) 00007 00008 ************************************************************************** 00009 00010 This program is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU General Public License 00012 as published by the Free Software Foundation; either version 2 00013 of the License, or (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program; if not, write to the Free Software 00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00023 00024 *************************************************************************** 00025 00026 */ 00027 00028 #include "tMemStack.h" 00029 #include "tArray.h" 00030 #include <stdlib.h> 00031 00032 static int& ST_Size() 00033 00034 { 00035 #ifdef DEBUG 00036 static int st_size=10; 00037 #else 00038 static int st_size=1000; 00039 #endif 00040 return st_size; 00041 00042 } 00043 00044 00045 class tMemStackItem 00046 { 00047 public: 00048 void* memory; 00049 int size; 00050 00051 tMemStackItem() 00052 { 00053 memory = NULL; 00054 size = 0; 00055 } 00056 00057 ~tMemStackItem() 00058 { 00059 if ( memory ) 00060 free(memory); 00061 } 00062 00063 void Alloc() 00064 { 00065 if ( ST_Size() > size ) 00066 { 00067 size = ST_Size(); 00068 if ( memory ) 00069 free(memory); 00070 00071 memory = malloc( size ); 00072 00073 00074 for ( int i = size-1; i>=0; --i ) 00075 00076 { 00077 00078 ((char*)(memory))[i] = 0; 00079 00080 } 00081 00082 } 00083 } 00084 }; 00085 00086 static tArray<tMemStackItem, true>& ST_Stack() 00087 00088 { 00089 00090 static tArray<tMemStackItem, true> st_stack; 00091 00092 return st_stack; 00093 00094 } 00095 00096 00097 00098 static int& ST_Index() 00099 00100 { 00101 00102 static int st_index=0; 00103 00104 return st_index; 00105 00106 } 00107 00108 00109 static void st_Pop() 00110 { 00111 --ST_Index(); 00112 } 00113 00114 tMemStackItem& tMemStack::Item() const 00115 { 00116 return ST_Stack()[this->index]; 00117 } 00118 00119 tMemStack::tMemStack (int minSize ) 00120 : index( ST_Index()++ ) 00121 { 00122 if ( ST_Size() < minSize ) 00123 ST_Size() = minSize; 00124 00125 Item().Alloc(); 00126 } 00127 00128 tMemStack::~tMemStack () 00129 { 00130 st_Pop(); 00131 00132 #ifdef DEBUG 00133 if( index != ST_Index() ) 00134 { 00135 st_Breakpoint(); 00136 } 00137 #endif 00138 } 00139 00140 00141 // get the memory pointer 00142 void* tMemStack::GetMem() const 00143 { 00144 return Item().memory; 00145 } 00146 00147 // get the memory size 00148 int tMemStack::GetSize() const 00149 { 00150 return Item().size; 00151 } 00152 00153 // recreate the buffer a bit larger 00154 void tMemStack::IncreaseMem() 00155 { 00156 ST_Size() *= 2; 00157 00158 Item().Alloc(); 00159 } 00160