00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef COBSTACK_HPP
00032 #define COBSTACK_HPP
00033
00034 #include "glue.h"
00035 #include <vector>
00036 #include <list>
00037 using namespace std;
00038
00045 class CObstack
00046 {
00047 private:
00048 static list<CObstack*>* obstackList;
00049
00053 typedef struct obstackChunk_tag {
00054 struct obstackChunk_tag *next;
00055 long size;
00056 long objectOffset;
00057 long freeOffset;
00058 char data[4];
00059 } Chunk_t;
00060
00061 static const int OBSTACK_DEFAULT_BLOCK_SIZE =
00062 1024*64;
00063
00064 string name;
00065 Chunk_t* currentChunk;
00066 long alignment;
00067 long defaultSize;
00068
00069 int chunkCount;
00070 int maxChunkCount;
00071 public:
00076 static void OnExitDumpStats();
00083 CObstack( const char* name, int chunkSize = OBSTACK_DEFAULT_BLOCK_SIZE );
00087 ~CObstack( void );
00093 void* Alloc( INT32 size );
00098 void Free( void* object );
00105 void* GetBase( void );
00112 void* NextFree( void );
00119 void* Finish( void );
00126 void* Copy( const void* ptr, INT32 size );
00134 void* Copy0( const void* ptr, INT32 size );
00139 INT32 GetObjectSize( void );
00146 void Grow( const void* ptr, INT32 size );
00152 void Grow( INT32 size );
00158 void PtrGrow( void* ptr );
00164 int IsOwner( void* ptr );
00165 private:
00169 static void _OnExitDumpStats();
00170 void DumpStats();
00171 void* GrowChunk( INT32 );
00175 };
00176
00184 template<class T>
00185 inline T** Finalize( CObstack* heap, vector<T*>& v )
00186 {
00187 typename vector<T*>::iterator ptr;
00188
00189 for( ptr = v.begin(); ptr != v.end(); ++ptr ) {
00190 heap->Grow( &*ptr, sizeof(T*) );
00191 }
00192 heap->PtrGrow(NULL);
00193 v.erase( v.begin(), v.end() );
00194 return (T**)heap->Finish();
00195 }
00196
00197
00198 #endif // COBSTACK_HPP