The Gaudi Framework  master (37c0b60a)
AllocatorPool.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
17 // ============================================================================
18 
19 //
20 // ********************************************************************
21 // * DISCLAIMER *
22 // * *
23 // * The following disclaimer summarizes all the specific disclaimers *
24 // * of contributors to this software. The specific disclaimers,which *
25 // * govern, are listed with their locations in: *
26 // * http://cern.ch/geant4/license *
27 // * *
28 // * Neither the authors of this software system, nor their employing *
29 // * institutes,nor the agencies providing financial support for this *
30 // * work make any representation or warranty, express or implied, *
31 // * regarding this software system or assume any liability for its *
32 // * use. *
33 // * *
34 // * This code implementation is the intellectual property of the *
35 // * GEANT4 collaboration. *
36 // * By copying, distributing or modifying the Program (or any work *
37 // * based on the Program) you indicate your acceptance of this *
38 // * statement, and all its terms. *
39 // ********************************************************************
40 //
41 // -------------------------------------------------------------------
42 // GEANT 4 class header file
43 //
44 // Class description:
45 //
46 // Class implementing a memory pool for fast allocation and deallocation
47 // of memory chunks. The size of the chunks for small allocated objects
48 // is fixed to 1Kb and takes into account of memory alignment; for large
49 // objects it is set to 10 times the object's size.
50 // The implementation is derived from: B.Stroustrup, The C++ Programming
51 // Language, Third Edition.
52 
53 // -------------- G4AllocatorPool ----------------
54 //
55 // Author: G.Cosmo (CERN), November 2000
56 // -------------------------------------------------------------------
57 
58 #ifndef GAUDIKERNEL_AllocatorPool_h
59 #define GAUDIKERNEL_AllocatorPool_h 1
60 
61 #include <GaudiKernel/Kernel.h>
62 #include <memory>
63 
64 namespace GaudiUtils {
72  class GAUDI_API AllocatorPool final {
73  public:
75  explicit AllocatorPool( unsigned int n = 0 );
77  ~AllocatorPool();
79  AllocatorPool( const AllocatorPool& right );
81  inline void* Alloc();
83  inline void Free( void* b );
85  inline unsigned int Size() const;
87  void Reset();
88 
89  private:
91  AllocatorPool& operator=( const AllocatorPool& right );
92 
93  private:
94  struct PoolLink final {
95  PoolLink* next = nullptr;
96  };
97  class PoolChunk final {
98  public:
99  explicit PoolChunk( unsigned int sz ) : size( sz ), mem{ new char[size] } {}
100  const unsigned int size;
102  PoolChunk* next = nullptr;
103  };
104 
106  void Grow();
107 
108  private:
109  const unsigned int esize;
110  const unsigned int csize;
111  PoolChunk* chunks = nullptr;
112  PoolLink* head = nullptr;
113  int nchunks = 0;
114  };
115 
116 } // end of namespace GaudiUtils
117 
118 // ************************************************************
119 // Alloc
120 // ************************************************************
121 //
123  if ( head == 0 ) { Grow(); }
124  PoolLink* p = head; // return first element
125  head = p->next;
126  return p;
127 }
128 
129 // ************************************************************
130 // Free
131 // ************************************************************
132 //
133 inline void GaudiUtils::AllocatorPool::Free( void* b ) {
134  PoolLink* p = static_cast<PoolLink*>( b );
135  p->next = head; // put b back as first element
136  head = p;
137 }
138 
139 // ************************************************************
140 // Size
141 // ************************************************************
142 //
143 inline unsigned int GaudiUtils::AllocatorPool::Size() const { return nchunks * csize; }
144 
145 // ============================================================================
146 // The END
147 // ============================================================================
148 #endif
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
GaudiUtils::AllocatorPool::head
PoolLink * head
Definition: AllocatorPool.h:112
GaudiUtils::AllocatorPool::Size
unsigned int Size() const
Return storage size.
Definition: AllocatorPool.h:143
GaudiUtils::AllocatorPool::Free
void Free(void *b)
Return an element back to the pool.
Definition: AllocatorPool.h:133
GaudiUtils::AllocatorPool::PoolChunk::PoolChunk
PoolChunk(unsigned int sz)
Definition: AllocatorPool.h:99
GaudiUtils::AllocatorPool::PoolChunk::size
const unsigned int size
Definition: AllocatorPool.h:100
GaudiUtils::AllocatorPool::Grow
void Grow()
Make pool larger.
Definition: AllocatorPool.cpp:112
cpluginsvc.n
n
Definition: cpluginsvc.py:234
GaudiUtils::AllocatorPool::csize
const unsigned int csize
Definition: AllocatorPool.h:110
GaudiUtils::AllocatorPool::Alloc
void * Alloc()
Allocate one element.
Definition: AllocatorPool.h:122
Kernel.h
GaudiUtils::AllocatorPool::esize
const unsigned int esize
Definition: AllocatorPool.h:109
GaudiUtils::AllocatorPool
Definition: AllocatorPool.h:72
GaudiUtils
Definition: Allocator.h:72
std::unique_ptr< char[]>
GaudiUtils::AllocatorPool::PoolChunk::mem
std::unique_ptr< char[]> mem
Definition: AllocatorPool.h:101
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
GaudiUtils::AllocatorPool::PoolChunk
Definition: AllocatorPool.h:97