Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
TaskAllocatorPool.cc
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// Tasking class implementation
21//
22// TaskAllocatorPool
23//
24// Implementation file
25//
26// Author: G.Cosmo, November 2000
27//
28
30
31using namespace PTL;
32
33// ************************************************************
34// TaskAllocatorPool constructor
35// ************************************************************
36//
38: esize((sz < sizeof(PoolLink)) ? sizeof(PoolLink) : sz)
39, csize((sz < 1024 / 2 - 16) ? (1024 - 16) : (sz * 10 - 16))
40, chunks(nullptr)
41, head(nullptr)
42, nchunks(0)
43{}
44
45// ************************************************************
46// TaskAllocatorPool copy constructor
47// ************************************************************
48//
50: esize(right.esize)
51, csize(right.csize)
52, chunks(right.chunks)
53, head(right.head)
54, nchunks(right.nchunks)
55{}
56
57// ************************************************************
58// TaskAllocatorPool operator=
59// ************************************************************
60//
62TaskAllocatorPool::operator=(const TaskAllocatorPool& right)
63{
64 if(&right == this)
65 return *this;
66 chunks = right.chunks;
67 head = right.head;
68 nchunks = right.nchunks;
69 return *this;
70}
71
72// ************************************************************
73// TaskAllocatorPool destructor
74// ************************************************************
75//
77
78// ************************************************************
79// Reset
80// ************************************************************
81//
82void
84{
85 // Free all chunks
86 //
87 PoolChunk* n = chunks;
88 PoolChunk* p = nullptr;
89 while(n)
90 {
91 p = n;
92 n = n->next;
93 delete p;
94 }
95 head = nullptr;
96 chunks = nullptr;
97 nchunks = 0;
98}
99
100// ************************************************************
101// Grow
102// ************************************************************
103//
104void
105TaskAllocatorPool::Grow()
106{
107 // Allocate new chunk, organize it as a linked list of
108 // elements of size 'esize'
109 //
110 PoolChunk* n = new PoolChunk(csize);
111 n->next = chunks;
112 chunks = n;
113 nchunks++;
114
115 const int nelem = csize / esize;
116 char* start = n->mem;
117 char* last = &start[(nelem - 1) * esize];
118 for(char* p = start; p < last; p += esize)
119 {
120 reinterpret_cast<PoolLink*>(p)->next = reinterpret_cast<PoolLink*>(p + esize);
121 }
122 reinterpret_cast<PoolLink*>(last)->next = nullptr;
123 head = reinterpret_cast<PoolLink*>(start);
124}
TaskAllocatorPool(unsigned int n=0)
Definition: AutoLock.hh:254