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