vrq
/usr/src/RPM/BUILD/vrq-1.0.96/src/cobstack.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1997-2007, Mark Hummel
00003  * This file is part of Vrq.
00004  *
00005  * Vrq is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * Vrq is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00018  * Boston, MA  02110-1301  USA
00019  *****************************************************************************
00020  */
00021 /******************************************************************************
00022  *
00023  *
00024  *         cobstack.hpp
00025  *                      - class definition for stack like bulk allocation
00026  *
00027  *
00028  ******************************************************************************
00029  */
00030 
00031 #ifndef COBSTACK_HPP
00032 #define COBSTACK_HPP
00033 
00034 #include "glue.h"
00035 #include <string>
00036 #include <vector>
00037 #include <list>
00038 using namespace std;
00039 
00046 class CObstack
00047 {
00048 private:
00049         static  list<CObstack*>* obstackList;   
00050 
00054         typedef struct  obstackChunk_tag {
00055                 struct  obstackChunk_tag *next; 
00056                 long    size;                   
00057                 long    objectOffset;           
00058                 long    freeOffset;             
00059                 char    data[4];                
00060         } Chunk_t;
00061 
00062         static const    int OBSTACK_DEFAULT_BLOCK_SIZE = 
00063                                                 1024*64; 
00064 
00065         string          name;           
00066         Chunk_t*        currentChunk;   
00067         long            alignment;      
00068         long            defaultSize;    
00069 
00070         int             chunkCount;     
00071         int             maxChunkCount;  
00072 public:
00077         static void OnExitDumpStats();
00084         CObstack( const char* name, int chunkSize = OBSTACK_DEFAULT_BLOCK_SIZE );
00088         ~CObstack( void );      
00094         void*   Alloc( INT32 size );
00099         void    Free( void* object );
00106         void*   GetBase( void );
00113         void*   NextFree( void );
00120         void*   Finish( void );
00127         void*   Copy( const void* ptr, INT32 size );
00135         void*   Copy0( const void* ptr, INT32 size );
00140         INT32   GetObjectSize( void );
00147         void    Grow( const void* ptr, INT32 size );
00153         void    Grow( INT32 size );
00159         void    PtrGrow( void* ptr );
00165         int     IsOwner( void* ptr );
00166 private:
00170         static void _OnExitDumpStats();
00171         void DumpStats();
00172         void*   GrowChunk( INT32 );
00176 };
00177 
00185 template<class T>
00186 inline T** Finalize( CObstack* heap, vector<T*>& v )
00187 {
00188         typename vector<T*>::iterator ptr;
00189 
00190         for( ptr = v.begin(); ptr != v.end(); ++ptr ) {
00191                 heap->Grow( &*ptr, sizeof(T*) );
00192         }
00193         heap->PtrGrow(NULL);
00194         v.erase( v.begin(), v.end() );
00195         return (T**)heap->Finish();
00196 }
00197 
00198 
00199 #endif // COBSTACK_HPP