ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AllocatorPool.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AllocatorPool.cc
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 //
27 //
28 //
29 // ----------------------------------------------------------------------
30 // G4AllocatorPool
31 //
32 // Implementation file
33 //
34 // Author: G.Cosmo, November 2000
35 //
36 
37 #include "G4AllocatorPool.hh"
38 
39 // ************************************************************
40 // G4AllocatorPool constructor
41 // ************************************************************
42 //
44  : esize(sz<sizeof(G4PoolLink) ? sizeof(G4PoolLink) : sz),
45  csize(sz<1024/2-16 ? 1024-16 : sz*10-16),
46  chunks(0), head(0), nchunks(0)
47 {
48 }
49 
50 // ************************************************************
51 // G4AllocatorPool copy constructor
52 // ************************************************************
53 //
55  : esize(right.esize), csize(right.csize),
56  chunks(right.chunks), head(right.head), nchunks(right.nchunks)
57 {
58 }
59 
60 // ************************************************************
61 // G4AllocatorPool operator=
62 // ************************************************************
63 //
66 {
67  if (&right == this) { return *this; }
68  chunks = right.chunks;
69  head = right.head;
70  nchunks = right.nchunks;
71  return *this;
72 }
73 
74 // ************************************************************
75 // G4AllocatorPool destructor
76 // ************************************************************
77 //
79 {
80  Reset();
81 }
82 
83 // ************************************************************
84 // Reset
85 // ************************************************************
86 //
88 {
89  // Free all chunks
90  //
91  G4PoolChunk* n = chunks;
92  G4PoolChunk* p = 0;
93  while (n)
94  {
95  p = n;
96  n = n->next;
97  delete p;
98  }
99  head = 0;
100  chunks = 0;
101  nchunks = 0;
102 }
103 
104 // ************************************************************
105 // Grow
106 // ************************************************************
107 //
109 {
110  // Allocate new chunk, organize it as a linked list of
111  // elements of size 'esize'
112  //
113  G4PoolChunk* n = new G4PoolChunk(csize);
114  n->next = chunks;
115  chunks = n;
116  nchunks++;
117 
118  const int nelem = csize/esize;
119  char* start = n->mem;
120  char* last = &start[(nelem-1)*esize];
121  for (char* p=start; p<last; p+=esize)
122  {
123  reinterpret_cast<G4PoolLink*>(p)->next
124  = reinterpret_cast<G4PoolLink*>(p+esize);
125  }
126  reinterpret_cast<G4PoolLink*>(last)->next = 0;
127  head = reinterpret_cast<G4PoolLink*>(start);
128 }