| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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