All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AllocatorPool.h
Go to the documentation of this file.
1 // $Id: AllocatorPool.h,v 1.3 2007/05/24 14:39:11 hmd Exp $
2 // ============================================================================
3 // CVS tag $Name: $, version $Revision: 1.3 $
4 // ============================================================================
10 // ============================================================================
11 
12 //
13 // ********************************************************************
14 // * DISCLAIMER *
15 // * *
16 // * The following disclaimer summarizes all the specific disclaimers *
17 // * of contributors to this software. The specific disclaimers,which *
18 // * govern, are listed with their locations in: *
19 // * http://cern.ch/geant4/license *
20 // * *
21 // * Neither the authors of this software system, nor their employing *
22 // * institutes,nor the agencies providing financial support for this *
23 // * work make any representation or warranty, express or implied, *
24 // * regarding this software system or assume any liability for its *
25 // * use. *
26 // * *
27 // * This code implementation is the intellectual property of the *
28 // * GEANT4 collaboration. *
29 // * By copying, distributing or modifying the Program (or any work *
30 // * based on the Program) you indicate your acceptance of this *
31 // * statement, and all its terms. *
32 // ********************************************************************
33 //
34 // -------------------------------------------------------------------
35 // GEANT 4 class header file
36 //
37 // Class description:
38 //
39 // Class implementing a memory pool for fast allocation and deallocation
40 // of memory chunks. The size of the chunks for small allocated objects
41 // is fixed to 1Kb and takes into account of memory alignment; for large
42 // objects it is set to 10 times the object's size.
43 // The implementation is derived from: B.Stroustrup, The C++ Programming
44 // Language, Third Edition.
45 
46 // -------------- G4AllocatorPool ----------------
47 //
48 // Author: G.Cosmo (CERN), November 2000
49 // -------------------------------------------------------------------
50 
51 #ifndef GAUDIKERNEL_AllocatorPool_h
52 #define GAUDIKERNEL_AllocatorPool_h 1
53 
54 #include "GaudiKernel/Kernel.h"
55 
56 namespace GaudiUtils
57 {
66  {
67  public:
68 
70  explicit AllocatorPool( unsigned int n=0 );
72  ~AllocatorPool();
74  AllocatorPool(const AllocatorPool& right);
76  inline void* Alloc();
78  inline void Free( void* b );
80  inline unsigned int Size() const;
82  void Reset();
83 
84  private:
85 
87  AllocatorPool& operator= (const AllocatorPool& right);
88 
89  private:
90 
91  struct PoolLink
92  {
94  };
95  class PoolChunk
96  {
97  public:
98  explicit PoolChunk(unsigned int sz)
99  : size(sz), mem(new char[size]), next(0) {;}
100  ~PoolChunk() { delete [] mem; }
101  const unsigned int size;
102  char* mem;
104  };
105 
107  void Grow();
108 
109  private:
110 
111  const unsigned int esize;
112  const unsigned int csize;
115  int nchunks;
116  };
117 
118 } // end of namespace GaudiUtils
119 
120 
121 
122 // ************************************************************
123 // Alloc
124 // ************************************************************
125 //
126 inline void*
128 {
129  if ( head==0 ) { Grow(); }
130  PoolLink* p = head; // return first element
131  head = p->next;
132  return p;
133 }
134 
135 // ************************************************************
136 // Free
137 // ************************************************************
138 //
139 inline void
141 {
142  PoolLink* p = static_cast<PoolLink*>(b);
143  p->next = head; // put b back as first element
144  head = p;
145 }
146 
147 // ************************************************************
148 // Size
149 // ************************************************************
150 //
151 inline unsigned int
153 {
154  return nchunks*csize;
155 }
156 
157 
158 // ============================================================================
159 // The END
160 // ============================================================================
161 #endif
162 // ============================================================================
163 
void Grow()
Make pool larger.
unsigned int Size() const
Return storage size.
const unsigned int esize
void * Alloc()
Allocate one element.
void Free(void *b)
Return an element back to the pool.
const unsigned int csize
#define GAUDI_API
Definition: Kernel.h:108