Geant4-11
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// G4AllocatorPool
27//
28// Class description:
29//
30// Class implementing a memory pool for fast allocation and deallocation
31// of memory chunks. The size of the chunks for small allocated objects
32// is fixed to 1Kb and takes into account of memory alignment; for large
33// objects it is set to 10 times the object's size.
34// The implementation is derived from: B.Stroustrup, The C++ Programming
35// Language, Third Edition.
36
37// -------------- G4AllocatorPool ----------------
38//
39// Author: G.Cosmo (CERN), November 2000
40// --------------------------------------------------------------------
41#ifndef G4AllocatorPool_hh
42#define G4AllocatorPool_hh 1
43
45{
46 public:
47 explicit G4AllocatorPool(unsigned int n = 0);
48 // Create a pool of elements of size n
50 // Destructor. Return storage to the free store
51
52 G4AllocatorPool(const G4AllocatorPool& right);
53 // Copy constructor
55 // Equality operator
56
57 inline void* Alloc();
58 // Allocate one element
59 inline void Free(void* b);
60 // Return an element back to the pool
61
62 inline unsigned int Size() const;
63 // Return storage size
64 void Reset();
65 // Return storage to the free store
66
67 inline int GetNoPages() const;
68 // Return the total number of allocated pages
69 inline unsigned int GetPageSize() const;
70 // Accessor for default page size
71 inline void GrowPageSize(unsigned int factor);
72 // Increase default page size by a given factor
73
74 private:
76 {
78 };
80 {
81 public:
82 explicit G4PoolChunk(unsigned int sz)
83 : size(sz)
84 , mem(new char[size])
85 , next(0)
86 {
87 ;
88 }
89 ~G4PoolChunk() { delete[] mem; }
90 const unsigned int size;
91 char* mem;
93 };
94
95 void Grow();
96 // Make pool larger
97
98 private:
99 const unsigned int esize;
100 unsigned int csize;
101 G4PoolChunk* chunks = nullptr;
102 G4PoolLink* head = nullptr;
103 int nchunks = 0;
104};
105
106// ------------------------------------------------------------
107// Inline implementation
108// ------------------------------------------------------------
109
110// ************************************************************
111// Alloc
112// ************************************************************
113//
115{
116 if(head == 0)
117 {
118 Grow();
119 }
120 G4PoolLink* p = head; // return first element
121 head = p->next;
122 return p;
123}
124
125// ************************************************************
126// Free
127// ************************************************************
128//
129inline void G4AllocatorPool::Free(void* b)
130{
131 G4PoolLink* p = static_cast<G4PoolLink*>(b);
132 p->next = head; // put b back as first element
133 head = p;
134}
135
136// ************************************************************
137// Size
138// ************************************************************
139//
140inline unsigned int G4AllocatorPool::Size() const { return nchunks * csize; }
141
142// ************************************************************
143// GetNoPages
144// ************************************************************
145//
146inline int G4AllocatorPool::GetNoPages() const { return nchunks; }
147
148// ************************************************************
149// GetPageSize
150// ************************************************************
151//
152inline unsigned int G4AllocatorPool::GetPageSize() const { return csize; }
153
154// ************************************************************
155// GrowPageSize
156// ************************************************************
157//
158inline void G4AllocatorPool::GrowPageSize(unsigned int sz)
159{
160 csize = (sz) ? sz * csize : csize;
161}
162
163#endif
const unsigned int esize
G4PoolLink * head
G4AllocatorPool & operator=(const G4AllocatorPool &right)
unsigned int csize
G4AllocatorPool(unsigned int n=0)
unsigned int Size() const
int GetNoPages() const
G4PoolChunk * chunks
void Free(void *b)
void GrowPageSize(unsigned int factor)
unsigned int GetPageSize() const