source: trunk/source/global/management/include/G4AllocatorPool.hh @ 1340

Last change on this file since 1340 was 1340, checked in by garnier, 14 years ago

update ti head

File size: 5.5 KB
Line 
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.hh,v 1.7 2010/07/14 10:45:46 gcosmo Exp $
28// GEANT4 tag $Name: global-V09-03-22 $
29//
30//
31// -------------------------------------------------------------------
32//      GEANT 4 class header file
33//
34// Class description:
35//
36// Class implementing a memory pool for fast allocation and deallocation
37// of memory chunks.  The size of the chunks for small allocated objects
38// is fixed to 1Kb and takes into account of memory alignment; for large
39// objects it is set to 10 times the object's size.
40// The implementation is derived from: B.Stroustrup, The C++ Programming
41// Language, Third Edition.
42
43//           -------------- G4AllocatorPool ----------------
44//
45// Author: G.Cosmo (CERN), November 2000
46// -------------------------------------------------------------------
47
48#ifndef G4AllocatorPool_h
49#define G4AllocatorPool_h 1
50
51class G4AllocatorPool
52{
53  public:
54
55    explicit G4AllocatorPool( unsigned int n=0 );
56      // Create a pool of elements of size n
57    ~G4AllocatorPool();
58      // Destructor. Return storage to the free store
59
60    inline void* Alloc();
61      // Allocate one element
62    inline void  Free( void* b );
63      // Return an element back to the pool
64
65    inline unsigned int  Size() const;
66      // Return storage size
67    void  Reset();
68      // Return storage to the free store
69
70    inline int  GetNoPages() const;
71      // Return the total number of allocated pages
72    inline unsigned int  GetPageSize() const;
73      // Accessor for default page size
74    inline void GrowPageSize( unsigned int factor );
75      // Increase default page size by a given factor
76
77  private:
78
79    G4AllocatorPool(const G4AllocatorPool& right);
80      // Provate copy constructor
81    G4AllocatorPool& operator= (const G4AllocatorPool& right);
82      // Private equality operator
83
84    struct G4PoolLink
85    {
86      G4PoolLink* next;
87    };
88    class G4PoolChunk
89    {
90      public:
91        explicit G4PoolChunk(unsigned int sz)
92          : size(sz), mem(new char[size]), next(0) {;}
93        ~G4PoolChunk() { delete [] mem; }
94        const unsigned int size;
95        char* mem;
96        G4PoolChunk* next;
97    };
98
99    void Grow();
100      // Make pool larger
101
102  private:
103
104    const unsigned int esize;
105    unsigned int csize;
106    G4PoolChunk* chunks;
107    G4PoolLink* head;
108    int nchunks;
109};
110
111// ------------------------------------------------------------
112// Inline implementation
113// ------------------------------------------------------------
114
115// ************************************************************
116// Alloc
117// ************************************************************
118//
119inline void*
120G4AllocatorPool::Alloc()
121{
122  if (head==0) { Grow(); }
123  G4PoolLink* p = head;  // return first element
124  head = p->next;
125  return p;
126}
127
128// ************************************************************
129// Free
130// ************************************************************
131//
132inline void
133G4AllocatorPool::Free( void* b )
134{
135  G4PoolLink* p = static_cast<G4PoolLink*>(b);
136  p->next = head;        // put b back as first element
137  head = p;
138}
139
140// ************************************************************
141// Size
142// ************************************************************
143//
144inline unsigned int
145G4AllocatorPool::Size() const
146{
147  return nchunks*csize;
148}
149
150// ************************************************************
151// GetNoPages
152// ************************************************************
153//
154inline int
155G4AllocatorPool::GetNoPages() const
156{
157  return nchunks;
158}
159
160// ************************************************************
161// GetPageSize
162// ************************************************************
163//
164inline unsigned int
165G4AllocatorPool::GetPageSize() const
166{
167  return csize;
168}
169
170// ************************************************************
171// GrowPageSize
172// ************************************************************
173//
174inline void
175G4AllocatorPool::GrowPageSize( unsigned int sz )
176{
177  csize = (sz) ? sz*csize : csize; 
178}
179
180#endif
Note: See TracBrowser for help on using the repository browser.