Geant4
10.02.p03
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
50
class
G4AllocatorPool
51
{
52
public
:
53
54
explicit
G4AllocatorPool
(
unsigned
int
n
=0 );
55
// Create a pool of elements of size n
56
~G4AllocatorPool
();
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
78
G4AllocatorPool
(
const
G4AllocatorPool
&
right
);
79
// Provate copy constructor
80
G4AllocatorPool
&
operator=
(
const
G4AllocatorPool
& right);
81
// Private equality operator
82
83
struct
G4PoolLink
84
{
85
G4PoolLink
*
next
;
86
};
87
class
G4PoolChunk
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
;
95
G4PoolChunk
*
next
;
96
};
97
98
void
Grow
();
99
// Make pool larger
100
101
private
:
102
103
const
unsigned
int
esize
;
104
unsigned
int
csize
;
105
G4PoolChunk
*
chunks
;
106
G4PoolLink
*
head
;
107
int
nchunks
;
108
};
109
110
// ------------------------------------------------------------
111
// Inline implementation
112
// ------------------------------------------------------------
113
114
// ************************************************************
115
// Alloc
116
// ************************************************************
117
//
118
inline
void
*
119
G4AllocatorPool::Alloc
()
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
132
G4AllocatorPool::Free
(
void
*
b
)
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
144
G4AllocatorPool::Size
()
const
145
{
146
return
nchunks
*
csize
;
147
}
148
149
// ************************************************************
150
// GetNoPages
151
// ************************************************************
152
//
153
inline
int
154
G4AllocatorPool::GetNoPages
()
const
155
{
156
return
nchunks
;
157
}
158
159
// ************************************************************
160
// GetPageSize
161
// ************************************************************
162
//
163
inline
unsigned
int
164
G4AllocatorPool::GetPageSize
()
const
165
{
166
return
csize
;
167
}
168
169
// ************************************************************
170
// GrowPageSize
171
// ************************************************************
172
//
173
inline
void
174
G4AllocatorPool::GrowPageSize
(
unsigned
int
sz )
175
{
176
csize
= (sz) ? sz*
csize
:
csize
;
177
}
178
179
#endif
G4AllocatorPool::GrowPageSize
void GrowPageSize(unsigned int factor)
Definition:
G4AllocatorPool.hh:174
G4AllocatorPool::esize
const unsigned int esize
Definition:
G4AllocatorPool.hh:103
G4AllocatorPool::G4PoolChunk::size
const unsigned int size
Definition:
G4AllocatorPool.hh:93
right
Definition:
F04UserTrackInformation.hh:37
G4AllocatorPool::G4PoolChunk
Definition:
G4AllocatorPool.hh:87
G4AllocatorPool::G4PoolChunk::mem
char * mem
Definition:
G4AllocatorPool.hh:94
G4AllocatorPool::Alloc
void * Alloc()
Definition:
G4AllocatorPool.hh:119
G4AllocatorPool::chunks
G4PoolChunk * chunks
Definition:
G4AllocatorPool.hh:105
G4AllocatorPool::Grow
void Grow()
Definition:
G4AllocatorPool.cc:109
G4AllocatorPool::Reset
void Reset()
Definition:
G4AllocatorPool.cc:88
G4AllocatorPool::G4PoolChunk::G4PoolChunk
G4PoolChunk(unsigned int sz)
Definition:
G4AllocatorPool.hh:90
G4AllocatorPool::operator=
G4AllocatorPool & operator=(const G4AllocatorPool &right)
Definition:
G4AllocatorPool.cc:66
G4AllocatorPool::GetNoPages
int GetNoPages() const
Definition:
G4AllocatorPool.hh:154
G4AllocatorPool::G4AllocatorPool
G4AllocatorPool(unsigned int n=0)
Definition:
G4AllocatorPool.cc:44
n
Char_t n[5]
Definition:
comparison_ascii.C:55
G4AllocatorPool::G4PoolChunk::~G4PoolChunk
~G4PoolChunk()
Definition:
G4AllocatorPool.hh:92
G4AllocatorPool::G4PoolLink::next
G4PoolLink * next
Definition:
G4AllocatorPool.hh:85
G4AllocatorPool::GetPageSize
unsigned int GetPageSize() const
Definition:
G4AllocatorPool.hh:164
G4AllocatorPool::~G4AllocatorPool
~G4AllocatorPool()
Definition:
G4AllocatorPool.cc:79
G4AllocatorPool::G4PoolLink
Definition:
G4AllocatorPool.hh:83
factor
static const G4double factor
Definition:
G4ContinuumGammaTransition.cc:63
G4AllocatorPool::Free
void Free(void *b)
Definition:
G4AllocatorPool.hh:132
G4AllocatorPool::G4PoolChunk::next
G4PoolChunk * next
Definition:
G4AllocatorPool.hh:95
G4AllocatorPool::nchunks
int nchunks
Definition:
G4AllocatorPool.hh:107
G4AllocatorPool::head
G4PoolLink * head
Definition:
G4AllocatorPool.hh:106
G4AllocatorPool::csize
unsigned int csize
Definition:
G4AllocatorPool.hh:104
G4AllocatorPool
Definition:
G4AllocatorPool.hh:50
test.b
b
Definition:
tests/test01/test.py:12
G4AllocatorPool::Size
unsigned int Size() const
Definition:
G4AllocatorPool.hh:144
Geant4
Geant4.10.02.p03
source
global
management
include
G4AllocatorPool.hh
Generated by
1.8.13