vrq
cobstack.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (C) 1997-2007, Mark Hummel
3  * This file is part of Vrq.
4  *
5  * Vrq is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * Vrq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA
19  *****************************************************************************
20  */
21 /******************************************************************************
22  *
23  *
24  * cobstack.hpp
25  * - class definition for stack like bulk allocation
26  *
27  *
28  ******************************************************************************
29  */
30 
31 #ifndef COBSTACK_HPP
32 #define COBSTACK_HPP
33 
34 #include "glue.h"
35 #include <string>
36 #include <vector>
37 #include <list>
38 using namespace std;
39 
46 class CObstack
47 {
48 private:
49  static list<CObstack*>* obstackList;
50 
54  typedef struct obstackChunk_tag {
55  struct obstackChunk_tag *next;
56  long size;
57  long objectOffset;
58  long freeOffset;
59  char data[4];
60  } Chunk_t;
61 
62  static const int OBSTACK_DEFAULT_BLOCK_SIZE =
63  1024*64;
64 
65  string name;
66  Chunk_t* currentChunk;
67  long alignment;
68  long defaultSize;
69  int chunkCount;
71  int maxChunkCount;
72 public:
77  static void OnExitDumpStats();
84  CObstack( const char* name, int chunkSize = OBSTACK_DEFAULT_BLOCK_SIZE );
88  ~CObstack( void );
94  void* Alloc( INT32 size );
99  void Free( void* object );
106  void* GetBase( void );
113  void* NextFree( void );
120  void* Finish( void );
127  void* Copy( const void* ptr, INT32 size );
135  void* Copy0( const void* ptr, INT32 size );
140  INT32 GetObjectSize( void );
147  void Grow( const void* ptr, INT32 size );
153  void Grow( INT32 size );
159  void PtrGrow( void* ptr );
165  int IsOwner( void* ptr );
166 private:
170  static void _OnExitDumpStats();
171  void DumpStats();
172  void* GrowChunk( INT32 );
176 };
177 
185 template<class T>
186 inline T** Finalize( CObstack* heap, vector<T*>& v )
187 {
188  typename vector<T*>::iterator ptr;
189 
190  for( ptr = v.begin(); ptr != v.end(); ++ptr ) {
191  heap->Grow( &*ptr, sizeof(T*) );
192  }
193  heap->PtrGrow(NULL);
194  v.erase( v.begin(), v.end() );
195  return (T**)heap->Finish();
196 }
197 
198 
199 #endif // COBSTACK_HPP
void * Finish(void)
Finialized the object.
long INT32
Short cut for signed 32 bit integer.
Definition: glue.h:38
Bulk object allocation object.
Definition: cobstack.h:46
void Grow(const void *ptr, INT32 size)
Grow current object and copy block of data to heap.
T ** Finalize(CObstack *heap, vector< T * > &v)
Transfer vector of pointers to a linear array allocated on the given obstack.
Definition: cobstack.h:186
void PtrGrow(void *ptr)
Grow current object and copy pointer to heap.