Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
TaskAllocatorPool.hh
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2020 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19//
20// -------------------------------------------------------------------
21// Tasking class header file
22//
23// Class description:
24//
25// Class implementing a memory pool for fast allocation and deallocation
26// of memory chunks. The size of the chunks for small allocated objects
27// is fixed to 1Kb and takes into account of memory alignment; for large
28// objects it is set to 10 times the object's size.
29// The implementation is derived from: B.Stroustrup, The C++ Programming
30// Language, Third Edition.
31
32// -------------- TaskAllocatorPool ----------------
33//
34// Author: G.Cosmo (CERN), November 2000
35// -------------------------------------------------------------------
36
37#pragma once
38
39namespace PTL
40{
42{
43public:
44 explicit TaskAllocatorPool(unsigned int n = 0);
45 // Create a pool of elements of size n
47 // Destructor. Return storage to the free store
48
49 inline void* Alloc();
50 // Allocate one element
51 inline void Free(void* b);
52 // Return an element back to the pool
53
54 inline unsigned int Size() const;
55 // Return storage size
56 void Reset();
57 // Return storage to the free store
58
59 inline int GetNoPages() const;
60 // Return the total number of allocated pages
61 inline unsigned int GetPageSize() const;
62 // Accessor for default page size
63 inline void GrowPageSize(unsigned int factor);
64 // Increase default page size by a given factor
65
66private:
68 // Provate copy constructor
69 TaskAllocatorPool& operator=(const TaskAllocatorPool& right);
70 // Private equality operator
71
72 struct PoolLink
73 {
74 PoolLink* next;
75 };
76
77 class PoolChunk
78 {
79 public:
80 explicit PoolChunk(unsigned int sz)
81 : size(sz)
82 , mem(new char[size])
83 , next(0)
84 {
85 ;
86 }
87 ~PoolChunk() { delete[] mem; }
88 const unsigned int size;
89 char* mem;
90 PoolChunk* next;
91 };
92
93 void Grow();
94 // Make pool larger
95
96private:
97 const unsigned int esize;
98 unsigned int csize;
99 PoolChunk* chunks;
100 PoolLink* head;
101 int nchunks;
102};
103
104//--------------------------------------------------------------------------------------//
105// Inline implementation
106
107// ************************************************************
108// Alloc
109// ************************************************************
110//
111inline void*
113{
114 if(head == nullptr)
115 Grow();
116 PoolLink* p = head; // return first element
117 head = p->next;
118 return p;
119}
120
121// ************************************************************
122// Free
123// ************************************************************
124//
125inline void
127{
128 PoolLink* p = static_cast<PoolLink*>(b);
129 p->next = head; // put b back as first element
130 head = p;
131}
132
133// ************************************************************
134// Size
135// ************************************************************
136//
137inline unsigned int
139{
140 return nchunks * csize;
141}
142
143// ************************************************************
144// GetNoPages
145// ************************************************************
146//
147inline int
149{
150 return nchunks;
151}
152
153// ************************************************************
154// GetPageSize
155// ************************************************************
156//
157inline unsigned int
159{
160 return csize;
161}
162
163// ************************************************************
164// GrowPageSize
165// ************************************************************
166//
167inline void
169{
170 csize = (sz) ? sz * csize : csize;
171}
172
173} // namespace PTL
void GrowPageSize(unsigned int factor)
unsigned int Size() const
unsigned int GetPageSize() const
Definition: AutoLock.hh:254