| The Gaudi Framework v32r1 (f65d50dc)

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