The Gaudi Framework  v30r3 (a5ef0a68)
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 {
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;
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 //
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 // ============================================================================
unsigned int Size() const
Return storage size.
std::unique_ptr< char[]> mem
Definition: AllocatorPool.h:94
const unsigned int esize
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: __init__.py:1
const unsigned int csize
#define GAUDI_API
Definition: Kernel.h:104