Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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;
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 //
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
unsigned int Size() const
Return storage size.
std::unique_ptr< char[]> mem
Definition: AllocatorPool.h:91
const unsigned int esize
Definition: AllocatorPool.h:99
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
PropertyMgr & operator=(const PropertyMgr &)=delete
void * Alloc()
Allocate one element.
void Free(void *b)
Return an element back to the pool.
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
const unsigned int csize
#define GAUDI_API
Definition: Kernel.h:71