Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4AllocatorPool.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4AllocatorPool class implementation
27//
28// Author: G.Cosmo, November 2000
29// --------------------------------------------------------------------
30
31#include "G4AllocatorPool.hh"
32
33// ************************************************************
34// G4AllocatorPool constructor
35// ************************************************************
36//
38 : esize(sz < sizeof(G4PoolLink) ? sizeof(G4PoolLink) : sz)
39 , csize(sz < 1024 / 2 - 16 ? 1024 - 16 : sz * 10 - 16)
40{}
41
42// ************************************************************
43// G4AllocatorPool copy constructor
44// ************************************************************
45//
47 : esize(right.esize)
48 , csize(right.csize)
49 , chunks(right.chunks)
50 , head(right.head)
51 , nchunks(right.nchunks)
52{}
53
54// ************************************************************
55// G4AllocatorPool operator=
56// ************************************************************
57//
59{
60 if(&right == this)
61 {
62 return *this;
63 }
64 chunks = right.chunks;
65 head = right.head;
66 nchunks = right.nchunks;
67 return *this;
68}
69
70// ************************************************************
71// G4AllocatorPool destructor
72// ************************************************************
73//
75
76// ************************************************************
77// Reset
78// ************************************************************
79//
81{
82 // Free all chunks
83 //
84 G4PoolChunk* n = chunks;
85 G4PoolChunk* p = nullptr;
86 while(n)
87 {
88 p = n;
89 n = n->next;
90 delete p;
91 }
92 head = nullptr;
93 chunks = nullptr;
94 nchunks = 0;
95}
96
97// ************************************************************
98// Grow
99// ************************************************************
100//
101void G4AllocatorPool::Grow()
102{
103 // Allocate new chunk, organize it as a linked list of
104 // elements of size 'esize'
105 //
106 G4PoolChunk* n = new G4PoolChunk(csize);
107 n->next = chunks;
108 chunks = n;
109 ++nchunks;
110
111 const int nelem = csize / esize;
112 char* start = n->mem;
113 char* last = &start[(nelem - 1) * esize];
114 for(char* p = start; p < last; p += esize)
115 {
116 reinterpret_cast<G4PoolLink*>(p)->next =
117 reinterpret_cast<G4PoolLink*>(p + esize);
118 }
119 reinterpret_cast<G4PoolLink*>(last)->next = nullptr;
120 head = reinterpret_cast<G4PoolLink*>(start);
121}
G4AllocatorPool & operator=(const G4AllocatorPool &right)
G4AllocatorPool(unsigned int n=0)