The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
63namespace GaudiUtils {
72 public:
74 explicit AllocatorPool( unsigned int n = 0 );
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//
132inline 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//
142inline unsigned int GaudiUtils::AllocatorPool::Size() const { return nchunks * csize; }
#define GAUDI_API
Definition Kernel.h:49
void Free(void *b)
Return an element back to the pool.
AllocatorPool & operator=(const AllocatorPool &right)
Private equality operator.
const unsigned int csize
void Reset()
Return storage to the free store.
void Grow()
Make pool larger.
AllocatorPool(unsigned int n=0)
Create a pool of elements of size n.
unsigned int Size() const
Return storage size.
const unsigned int esize
void * Alloc()
Allocate one element.