Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AllocatorPool.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
59 
60 #include <GaudiKernel/Kernel.h>
61 #include <memory>
62 
63 namespace GaudiUtils {
71  class GAUDI_API AllocatorPool final {
72  public:
74  explicit AllocatorPool( unsigned int n = 0 );
76  ~AllocatorPool();
78  AllocatorPool( const AllocatorPool& right );
80  inline void* Alloc();
82  inline void Free( void* b );
84  inline unsigned int Size() const;
86  void Reset();
87 
88  private:
90  AllocatorPool& operator=( const AllocatorPool& right );
91 
92  private:
93  struct PoolLink final {
94  PoolLink* next = nullptr;
95  };
96  class PoolChunk final {
97  public:
98  explicit PoolChunk( unsigned int sz ) : size( sz ), mem{ new char[size] } {}
99  const unsigned int size;
100  std::unique_ptr<char[]> mem;
101  PoolChunk* next = nullptr;
102  };
103 
105  void Grow();
106 
107  private:
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 } // namespace GaudiUtils
116 
117 // ************************************************************
118 // Alloc
119 // ************************************************************
120 //
122  if ( head == 0 ) { Grow(); }
123  PoolLink* p = head; // return first element
124  head = p->next;
125  return p;
126 }
127 
128 // ************************************************************
129 // Free
130 // ************************************************************
131 //
132 inline void GaudiUtils::AllocatorPool::Free( void* b ) {
133  PoolLink* p = static_cast<PoolLink*>( b );
134  p->next = head; // put b back as first element
135  head = p;
136 }
137 
138 // ************************************************************
139 // Size
140 // ************************************************************
141 //
142 inline unsigned int GaudiUtils::AllocatorPool::Size() const { return nchunks * csize; }
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
GaudiUtils::AllocatorPool::head
PoolLink * head
Definition: AllocatorPool.h:111
GaudiUtils::AllocatorPool::Size
unsigned int Size() const
Return storage size.
Definition: AllocatorPool.h:142
GaudiUtils::AllocatorPool::Free
void Free(void *b)
Return an element back to the pool.
Definition: AllocatorPool.h:132
GaudiUtils::AllocatorPool::PoolChunk::PoolChunk
PoolChunk(unsigned int sz)
Definition: AllocatorPool.h:98
GaudiUtils::AllocatorPool::PoolChunk::size
const unsigned int size
Definition: AllocatorPool.h:99
GaudiUtils::AllocatorPool::Grow
void Grow()
Make pool larger.
Definition: AllocatorPool.cpp:107
cpluginsvc.n
n
Definition: cpluginsvc.py:234
GaudiUtils::AllocatorPool::csize
const unsigned int csize
Definition: AllocatorPool.h:109
GaudiUtils::AllocatorPool::Alloc
void * Alloc()
Allocate one element.
Definition: AllocatorPool.h:121
Kernel.h
GaudiUtils::AllocatorPool::esize
const unsigned int esize
Definition: AllocatorPool.h:108
GaudiUtils::AllocatorPool
Definition: AllocatorPool.h:71
GaudiUtils
Definition: Allocator.h:62
GaudiUtils::AllocatorPool::PoolChunk::mem
std::unique_ptr< char[]> mem
Definition: AllocatorPool.h:100
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
GaudiUtils::AllocatorPool::PoolChunk
Definition: AllocatorPool.h:96