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 <memory>
52 #include "GaudiKernel/Kernel.h"
53 
54 namespace GaudiUtils
55 {
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;
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*
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
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
150 {
151  return nchunks*csize;
152 }
153 
154 
155 // ============================================================================
156 // The END
157 // ============================================================================
158 #endif
159 // ============================================================================
160 
unsigned int Size() const
Return storage size.
std::unique_ptr< char[]> mem
Definition: AllocatorPool.h:99
const unsigned int esize
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:149
const unsigned int csize
#define GAUDI_API
Definition: Kernel.h:107