| The Gaudi Framework v29r1 (5cd95fa2)

- 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 "GaudiKernel/Kernel.h"
52 #include <memory>
53
54 namespace GaudiUtils
55 {
63 class GAUDI_API AllocatorPool final
64 {
65 public:
67 explicit AllocatorPool( unsigned int n = 0 );
69 ~AllocatorPool();
71 AllocatorPool( const AllocatorPool& right );
73 inline void* Alloc();
75 inline void Free( void* b );
77 inline unsigned int Size() const;
79 void Reset();
80
81 private:
83 AllocatorPool& operator=( const AllocatorPool& right );
84
85 private:
86 struct PoolLink final {
87 PoolLink* next = nullptr;
88 };
89 class PoolChunk final
90 {
91 public:
92 explicit PoolChunk( unsigned int sz ) : size( sz ), mem{new char[size]} {}
93 const unsigned int size;
94 std::unique_ptr<char[]> mem;
95 PoolChunk* next = nullptr;
96 };
97
99 void Grow();
100
101 private:
102 const unsigned int esize;
103 const unsigned int csize;
104 PoolChunk* chunks = nullptr;
105 PoolLink* head = nullptr;
106 int nchunks = 0;
107 };
108
109 } // end of namespace GaudiUtils
110
111 // ************************************************************
112 // Alloc
113 // ************************************************************
114 //
115 inline void* GaudiUtils::AllocatorPool::Alloc()
116 {
117 if ( head == 0 ) {
118 Grow();
119 }
120 PoolLink* p = head; // return first element
121 head = p->next;
122 return p;
123 }
124
125 // ************************************************************
126 // Free
127 // ************************************************************
128 //
129 inline void GaudiUtils::AllocatorPool::Free( void* b )
130 {
131 PoolLink* p = static_cast<PoolLink*>( b );
132 p->next = head; // put b back as first element
133 head = p;
134 }
135
136 // ************************************************************
137 // Size
138 // ************************************************************
139 //
140 inline unsigned int GaudiUtils::AllocatorPool::Size() const { return nchunks * csize; }
141
142 // ============================================================================
143 // The END
144 // ============================================================================
145 #endif
146 // ============================================================================
Kernel.h
GaudiUtils::AllocatorPool::PoolChunk
Definition: AllocatorPool.h:89
GaudiUtils::AllocatorPool::PoolLink
Definition: AllocatorPool.h:86
GaudiUtils::AllocatorPool::Size
unsigned int Size() const
Return storage size.
Definition: AllocatorPool.h:140
GaudiUtils::AllocatorPool::PoolChunk::PoolChunk
PoolChunk(unsigned int sz)
Definition: AllocatorPool.h:92
GaudiUtils::AllocatorPool::PoolChunk::mem
std::unique_ptr< char[]> mem
Definition: AllocatorPool.h:94
GaudiUtils::AllocatorPool::esize
const unsigned int esize
Definition: AllocatorPool.h:102
GaudiUtils::AllocatorPool::PoolChunk::size
const unsigned int size
Definition: AllocatorPool.h:93
operator=
PropertyMgr & operator=(const PropertyMgr &)=delete
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:228
GaudiUtils::AllocatorPool::Alloc
void * Alloc()
Allocate one element.
Definition: AllocatorPool.h:115
GaudiUtils::AllocatorPool::PoolLink::next
PoolLink * next
Definition: AllocatorPool.h:87
GaudiUtils::AllocatorPool::Free
void Free(void *b)
Return an element back to the pool.
Definition: AllocatorPool.h:129
std::unique_ptr< char[]>
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:140
GaudiUtils::AllocatorPool::csize
const unsigned int csize
Definition: AllocatorPool.h:103
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
GaudiUtils::AllocatorPool
Allocator pool.
Definition: AllocatorPool.h:63