Geant4  10.03
G4AllocatorPool.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4AllocatorPool.hh 67970 2013-03-13 10:10:06Z gcosmo $
28 //
29 //
30 // -------------------------------------------------------------------
31 // GEANT 4 class header file
32 //
33 // Class description:
34 //
35 // Class implementing a memory pool for fast allocation and deallocation
36 // of memory chunks. The size of the chunks for small allocated objects
37 // is fixed to 1Kb and takes into account of memory alignment; for large
38 // objects it is set to 10 times the object's size.
39 // The implementation is derived from: B.Stroustrup, The C++ Programming
40 // Language, Third Edition.
41 
42 // -------------- G4AllocatorPool ----------------
43 //
44 // Author: G.Cosmo (CERN), November 2000
45 // -------------------------------------------------------------------
46 
47 #ifndef G4AllocatorPool_h
48 #define G4AllocatorPool_h 1
49 
51 {
52  public:
53 
54  explicit G4AllocatorPool( unsigned int n=0 );
55  // Create a pool of elements of size n
57  // Destructor. Return storage to the free store
58 
59  inline void* Alloc();
60  // Allocate one element
61  inline void Free( void* b );
62  // Return an element back to the pool
63 
64  inline unsigned int Size() const;
65  // Return storage size
66  void Reset();
67  // Return storage to the free store
68 
69  inline int GetNoPages() const;
70  // Return the total number of allocated pages
71  inline unsigned int GetPageSize() const;
72  // Accessor for default page size
73  inline void GrowPageSize( unsigned int factor );
74  // Increase default page size by a given factor
75 
76  private:
77 
79  // Provate copy constructor
81  // Private equality operator
82 
83  struct G4PoolLink
84  {
86  };
88  {
89  public:
90  explicit G4PoolChunk(unsigned int sz)
91  : size(sz), mem(new char[size]), next(0) {;}
92  ~G4PoolChunk() { delete [] mem; }
93  const unsigned int size;
94  char* mem;
96  };
97 
98  void Grow();
99  // Make pool larger
100 
101  private:
102 
103  const unsigned int esize;
104  unsigned int csize;
107  int nchunks;
108 };
109 
110 // ------------------------------------------------------------
111 // Inline implementation
112 // ------------------------------------------------------------
113 
114 // ************************************************************
115 // Alloc
116 // ************************************************************
117 //
118 inline void*
120 {
121  if (head==0) { Grow(); }
122  G4PoolLink* p = head; // return first element
123  head = p->next;
124  return p;
125 }
126 
127 // ************************************************************
128 // Free
129 // ************************************************************
130 //
131 inline void
133 {
134  G4PoolLink* p = static_cast<G4PoolLink*>(b);
135  p->next = head; // put b back as first element
136  head = p;
137 }
138 
139 // ************************************************************
140 // Size
141 // ************************************************************
142 //
143 inline unsigned int
145 {
146  return nchunks*csize;
147 }
148 
149 // ************************************************************
150 // GetNoPages
151 // ************************************************************
152 //
153 inline int
155 {
156  return nchunks;
157 }
158 
159 // ************************************************************
160 // GetPageSize
161 // ************************************************************
162 //
163 inline unsigned int
165 {
166  return csize;
167 }
168 
169 // ************************************************************
170 // GrowPageSize
171 // ************************************************************
172 //
173 inline void
175 {
176  csize = (sz) ? sz*csize : csize;
177 }
178 
179 #endif
unsigned int Size() const
void GrowPageSize(unsigned int factor)
const unsigned int esize
G4PoolChunk * chunks
unsigned int GetPageSize() const
G4AllocatorPool & operator=(const G4AllocatorPool &right)
int GetNoPages() const
G4AllocatorPool(unsigned int n=0)
const G4int n
void Free(void *b)
G4PoolLink * head
unsigned int csize