source: trunk/source/global/management/include/G4Allocator.hh @ 1337

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

tag geant4.9.4 beta 1 + modifs locales

File size: 7.4 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: G4Allocator.hh,v 1.20 2010/03/30 08:17:24 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-01 $
29//
30//
31// ------------------------------------------------------------
32// GEANT 4 class header file
33//
34// Class Description:
35//
36// A class for fast allocation of objects to the heap through a pool of
37// chunks organised as linked list. It's meant to be used by associating
38// it to the object to be allocated and defining for it new and delete
39// operators via MallocSingle() and FreeSingle() methods.
40       
41//      ---------------- G4Allocator ----------------
42//
43// Author: G.Cosmo (CERN), November 2000
44// ------------------------------------------------------------
45
46#ifndef G4Allocator_h
47#define G4Allocator_h 1
48
49#include <cstddef>
50
51#include "G4AllocatorPool.hh"
52
53template <class Type>
54class G4Allocator
55{
56  public:  // with description
57
58    G4Allocator() throw();
59    ~G4Allocator() throw();
60      // Constructor & destructor
61
62    inline Type* MallocSingle();
63    inline void FreeSingle(Type* anElement);
64      // Malloc and Free methods to be used when overloading
65      // new and delete operators in the client <Type> object
66
67    inline void ResetStorage();
68      // Returns allocated storage to the free store, resets
69      // allocator and page sizes.
70      // Note: contents in memory are lost using this call !
71
72    inline size_t GetAllocatedSize() const;
73      // Returns the size of the total memory allocated
74
75  public:  // without description
76
77    // This public section includes standard methods and types
78    // required if the allocator is to be used as alternative
79    // allocator for STL containers.
80    // NOTE: the code below is a trivial implementation to make
81    //       this class an STL compliant allocator.
82    //       It is anyhow NOT recommended to use this class as
83    //       alternative allocator for STL containers !
84
85    typedef Type value_type;
86    typedef size_t size_type;
87    typedef ptrdiff_t difference_type;
88    typedef Type* pointer;
89    typedef const Type* const_pointer;
90    typedef Type& reference;
91    typedef const Type& const_reference;
92
93    template <class U> G4Allocator(const G4Allocator<U>& right) throw()
94      : mem(right.mem) {}
95      // Copy constructor
96
97    pointer address(reference r) const { return &r; }
98    const_pointer address(const_reference r) const { return &r; }
99      // Returns the address of values
100
101    pointer allocate(size_type n, void* = 0)
102    {
103      // Allocates space for n elements of type Type, but does not initialise
104      //
105      Type* mem_alloc = 0;
106      if (n == 1)
107        mem_alloc = MallocSingle();
108      else
109        mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type)));
110      return mem_alloc;
111    }
112    void deallocate(pointer p, size_type n)
113    {
114      // Deallocates n elements of type Type, but doesn't destroy
115      //
116      if (n == 1)
117        FreeSingle(p);
118      else
119        ::operator delete((void*)p);
120      return;
121    }
122
123    void construct(pointer p, const Type& val) { new((void*)p) Type(val); }
124      // Initialises *p by val
125    void destroy(pointer p) { p->~Type(); }
126      // Destroy *p but doesn't deallocate
127
128    size_type max_size() const throw()
129    {
130      // Returns the maximum number of elements that can be allocated
131      //
132      return 2147483647/sizeof(Type);
133    }
134
135    template <class U>
136    struct rebind { typedef G4Allocator<U> other; };
137      // Rebind allocator to type U
138
139    G4AllocatorPool mem;
140      // Pool of elements of sizeof(Type)
141};
142
143// ------------------------------------------------------------
144// Inline implementation
145// ------------------------------------------------------------
146
147// Initialization of the static pool
148//
149// template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type));
150
151// ************************************************************
152// G4Allocator constructor
153// ************************************************************
154//
155template <class Type>
156G4Allocator<Type>::G4Allocator() throw()
157  : mem(sizeof(Type))
158{
159}
160
161// ************************************************************
162// G4Allocator destructor
163// ************************************************************
164//
165template <class Type>
166G4Allocator<Type>::~G4Allocator() throw()
167{
168}
169
170// ************************************************************
171// MallocSingle
172// ************************************************************
173//
174template <class Type>
175Type* G4Allocator<Type>::MallocSingle()
176{
177  return static_cast<Type*>(mem.Alloc());
178}
179
180// ************************************************************
181// FreeSingle
182// ************************************************************
183//
184template <class Type>
185void G4Allocator<Type>::FreeSingle(Type* anElement)
186{
187  mem.Free(anElement);
188  return;
189}
190
191// ************************************************************
192// ResetStorage
193// ************************************************************
194//
195template <class Type>
196void G4Allocator<Type>::ResetStorage()
197{
198  // Clear all allocated storage and return it to the free store
199  //
200  mem.Reset();
201  return;
202}
203
204// ************************************************************
205// GetAllocatedSize
206// ************************************************************
207//
208template <class Type>
209size_t G4Allocator<Type>::GetAllocatedSize() const
210{
211  return mem.Size();
212}
213
214// ************************************************************
215// operator==
216// ************************************************************
217//
218template <class T1, class T2>
219bool operator== (const G4Allocator<T1>&, const G4Allocator<T2>&) throw()
220{
221  return true;
222}
223
224// ************************************************************
225// operator!=
226// ************************************************************
227//
228template <class T1, class T2>
229bool operator!= (const G4Allocator<T1>&, const G4Allocator<T2>&) throw()
230{
231  return false;
232}
233
234#endif
Note: See TracBrowser for help on using the repository browser.