ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AllocatorPool.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AllocatorPool.hh
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 //
28 //
29 // -------------------------------------------------------------------
30 // GEANT 4 class header file
31 //
32 // Class description:
33 //
34 // Class implementing a memory pool for fast allocation and deallocation
35 // of memory chunks. The size of the chunks for small allocated objects
36 // is fixed to 1Kb and takes into account of memory alignment; for large
37 // objects it is set to 10 times the object's size.
38 // The implementation is derived from: B.Stroustrup, The C++ Programming
39 // Language, Third Edition.
40 
41 // -------------- G4AllocatorPool ----------------
42 //
43 // Author: G.Cosmo (CERN), November 2000
44 // -------------------------------------------------------------------
45 
46 #ifndef G4AllocatorPool_h
47 #define G4AllocatorPool_h 1
48 
50 {
51  public:
52 
53  explicit G4AllocatorPool( unsigned int n=0 );
54  // Create a pool of elements of size n
56  // Destructor. Return storage to the free store
57 
59  // Copy constructor
61  // Equality operator
62 
63  inline void* Alloc();
64  // Allocate one element
65  inline void Free( void* b );
66  // Return an element back to the pool
67 
68  inline unsigned int Size() const;
69  // Return storage size
70  void Reset();
71  // Return storage to the free store
72 
73  inline int GetNoPages() const;
74  // Return the total number of allocated pages
75  inline unsigned int GetPageSize() const;
76  // Accessor for default page size
77  inline void GrowPageSize( unsigned int factor );
78  // Increase default page size by a given factor
79 
80  private:
81 
82  struct G4PoolLink
83  {
85  };
87  {
88  public:
89  explicit G4PoolChunk(unsigned int sz)
90  : size(sz), mem(new char[size]), next(0) {;}
91  ~G4PoolChunk() { delete [] mem; }
92  const unsigned int size;
93  char* mem;
95  };
96 
97  void Grow();
98  // Make pool larger
99 
100  private:
101 
102  const unsigned int esize;
103  unsigned int csize;
106  int nchunks;
107 };
108 
109 // ------------------------------------------------------------
110 // Inline implementation
111 // ------------------------------------------------------------
112 
113 // ************************************************************
114 // Alloc
115 // ************************************************************
116 //
117 inline void*
119 {
120  if (head==0) { Grow(); }
121  G4PoolLink* p = head; // return first element
122  head = p->next;
123  return p;
124 }
125 
126 // ************************************************************
127 // Free
128 // ************************************************************
129 //
130 inline void
132 {
133  G4PoolLink* p = static_cast<G4PoolLink*>(b);
134  p->next = head; // put b back as first element
135  head = p;
136 }
137 
138 // ************************************************************
139 // Size
140 // ************************************************************
141 //
142 inline unsigned int
144 {
145  return nchunks*csize;
146 }
147 
148 // ************************************************************
149 // GetNoPages
150 // ************************************************************
151 //
152 inline int
154 {
155  return nchunks;
156 }
157 
158 // ************************************************************
159 // GetPageSize
160 // ************************************************************
161 //
162 inline unsigned int
164 {
165  return csize;
166 }
167 
168 // ************************************************************
169 // GrowPageSize
170 // ************************************************************
171 //
172 inline void
174 {
175  csize = (sz) ? sz*csize : csize;
176 }
177 
178 #endif