Geant4  10.00.p01
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 //
27 // $Id: G4AllocatorPool.cc 67970 2013-03-13 10:10:06Z gcosmo $
28 //
29 //
30 // ----------------------------------------------------------------------
31 // G4AllocatorPool
32 //
33 // Implementation file
34 //
35 // Author: G.Cosmo, November 2000
36 //
37 
38 #include "G4AllocatorPool.hh"
39 
40 // ************************************************************
41 // G4AllocatorPool constructor
42 // ************************************************************
43 //
45  : esize(sz<sizeof(G4PoolLink) ? sizeof(G4PoolLink) : sz),
46  csize(sz<1024/2-16 ? 1024-16 : sz*10-16),
47  chunks(0), head(0), nchunks(0)
48 {
49 }
50 
51 // ************************************************************
52 // G4AllocatorPool copy constructor
53 // ************************************************************
54 //
56  : esize(right.esize), csize(right.csize),
57  chunks(right.chunks), head(right.head), nchunks(right.nchunks)
58 {
59 }
60 
61 // ************************************************************
62 // G4AllocatorPool operator=
63 // ************************************************************
64 //
67 {
68  if (&right == this) { return *this; }
69  chunks = right.chunks;
70  head = right.head;
71  nchunks = right.nchunks;
72  return *this;
73 }
74 
75 // ************************************************************
76 // G4AllocatorPool destructor
77 // ************************************************************
78 //
80 {
81  Reset();
82 }
83 
84 // ************************************************************
85 // Reset
86 // ************************************************************
87 //
89 {
90  // Free all chunks
91  //
92  G4PoolChunk* n = chunks;
93  G4PoolChunk* p = 0;
94  while (n)
95  {
96  p = n;
97  n = n->next;
98  delete p;
99  }
100  head = 0;
101  chunks = 0;
102  nchunks = 0;
103 }
104 
105 // ************************************************************
106 // Grow
107 // ************************************************************
108 //
110 {
111  // Allocate new chunk, organize it as a linked list of
112  // elements of size 'esize'
113  //
114  G4PoolChunk* n = new G4PoolChunk(csize);
115  n->next = chunks;
116  chunks = n;
117  nchunks++;
118 
119  const int nelem = csize/esize;
120  char* start = n->mem;
121  char* last = &start[(nelem-1)*esize];
122  for (char* p=start; p<last; p+=esize)
123  {
124  reinterpret_cast<G4PoolLink*>(p)->next
125  = reinterpret_cast<G4PoolLink*>(p+esize);
126  }
127  reinterpret_cast<G4PoolLink*>(last)->next = 0;
128  head = reinterpret_cast<G4PoolLink*>(start);
129 }
const unsigned int esize
G4PoolChunk * chunks
G4AllocatorPool & operator=(const G4AllocatorPool &right)
G4AllocatorPool(unsigned int n=0)
const G4int n
G4PoolLink * head
unsigned int csize