All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AllocatorPool.cpp
Go to the documentation of this file.
1 // ============================================================================
7 // ============================================================================
8 //
9 // ********************************************************************
10 // * DISCLAIMER *
11 // * *
12 // * The following disclaimer summarizes all the specific disclaimers *
13 // * of contributors to this software. The specific disclaimers,which *
14 // * govern, are listed with their locations in: *
15 // * http://cern.ch/geant4/license *
16 // * *
17 // * Neither the authors of this software system, nor their employing *
18 // * institutes,nor the agencies providing financial support for this *
19 // * work make any representation or warranty, express or implied, *
20 // * regarding this software system or assume any liability for its *
21 // * use. *
22 // * *
23 // * This code implementation is the intellectual property of the *
24 // * GEANT4 collaboration. *
25 // * By copying, distributing or modifying the Program (or any work *
26 // * based on the Program) you indicate your acceptance of this *
27 // * statement, and all its terms. *
28 // ********************************************************************
29 //
30 // ----------------------------------------------------------------------
31 // G4AllocatorPool
32 //
33 // Implementation file
34 //
35 // Author: G.Cosmo, November 2000
36 //
37 // ============================================================================
38 // Include files
39 // ============================================================================
40 // GaudiKernel
41 // ============================================================================
43 // ============================================================================
44 
45 // ************************************************************
46 // G4AllocatorPool constructor
47 // ************************************************************
48 //
50 ( unsigned int sz )
51  : esize(sz<sizeof(PoolLink) ? sizeof(PoolLink) : sz),
52  csize(sz<1024/2-16 ? 1024-16 : sz*10-16)
53 {}
54 
55 // ************************************************************
56 // G4AllocatorPool copy constructor
57 // ************************************************************
58 //
60  : esize(right.esize), csize(right.csize)
61 {
62  *this = right;
63 }
64 
65 // ************************************************************
66 // G4AllocatorPool operator=
67 // ************************************************************
68 //
70 GaudiUtils::AllocatorPool::operator=
72 {
73  if (&right == this) { return *this; }
74  chunks = right.chunks;
75  head = right.head;
76  nchunks = right.nchunks;
77  return *this;
78 }
79 
80 // ************************************************************
81 // G4AllocatorPool destructor
82 // ************************************************************
83 //
85 {
86  Reset();
87 }
88 
89 // ************************************************************
90 // Reset
91 // ************************************************************
92 //
94 {
95  // Free all chunks
96  //
97  PoolChunk* n = chunks;
98  PoolChunk* p = nullptr;
99  while (n)
100  {
101  p = n;
102  n = n->next;
103  delete p;
104  }
105  head = nullptr;
106  chunks = nullptr;
107  nchunks = 0;
108 }
109 
110 // ************************************************************
111 // Grow
112 // ************************************************************
113 //
115 {
116  // Allocate new chunk, organize it as a linked list of
117  // elements of size 'esize'
118  //
119  auto n = new PoolChunk(csize);
120  n->next = chunks;
121  chunks = n;
122  ++nchunks;
123 
124  const int nelem = csize/esize;
125  char* start = n->mem.get();
126  char* last = &start[(nelem-1)*esize];
127  for (char* p=start; p<last; p+=esize)
128  {
129  reinterpret_cast<PoolLink*>(p)->next
130  = reinterpret_cast<PoolLink*>(p+esize);
131  }
132  reinterpret_cast<PoolLink*>(last)->next = nullptr;
133  head = reinterpret_cast<PoolLink*>(start);
134 }
135 
136 
137 // ============================================================================
138 // The END
139 // ============================================================================
void Grow()
Make pool larger.
~AllocatorPool()
Destructor. Return storage to the free store.
const unsigned int esize
void Reset()
Return storage to the free store.
start
Definition: IOTest.py:88
Allocator pool.
const unsigned int csize
AllocatorPool(unsigned int n=0)
Create a pool of elements of size n.