| [833] | 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 | //
|
|---|
| [1340] | 27 | // $Id: G4Allocator.hh,v 1.21 2010/04/01 12:43:12 gcosmo Exp $
|
|---|
| 28 | // GEANT4 tag $Name: global-V09-03-22 $
|
|---|
| [833] | 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 |
|
|---|
| 53 | template <class Type>
|
|---|
| 54 | class 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();
|
|---|
| [1340] | 68 | // Returns allocated storage to the free store, resets allocator.
|
|---|
| [833] | 69 | // Note: contents in memory are lost using this call !
|
|---|
| 70 |
|
|---|
| 71 | inline size_t GetAllocatedSize() const;
|
|---|
| 72 | // Returns the size of the total memory allocated
|
|---|
| [1340] | 73 | inline int GetNoPages() const;
|
|---|
| 74 | // Returns the total number of allocated pages
|
|---|
| 75 | inline size_t GetPageSize() const;
|
|---|
| 76 | // Returns the current size of a page
|
|---|
| 77 | inline void IncreasePageSize( unsigned int sz );
|
|---|
| 78 | // Resets allocator and increases default page size of a given factor
|
|---|
| [833] | 79 |
|
|---|
| 80 | public: // without description
|
|---|
| 81 |
|
|---|
| 82 | // This public section includes standard methods and types
|
|---|
| 83 | // required if the allocator is to be used as alternative
|
|---|
| 84 | // allocator for STL containers.
|
|---|
| 85 | // NOTE: the code below is a trivial implementation to make
|
|---|
| 86 | // this class an STL compliant allocator.
|
|---|
| 87 | // It is anyhow NOT recommended to use this class as
|
|---|
| 88 | // alternative allocator for STL containers !
|
|---|
| 89 |
|
|---|
| 90 | typedef Type value_type;
|
|---|
| 91 | typedef size_t size_type;
|
|---|
| 92 | typedef ptrdiff_t difference_type;
|
|---|
| 93 | typedef Type* pointer;
|
|---|
| 94 | typedef const Type* const_pointer;
|
|---|
| 95 | typedef Type& reference;
|
|---|
| 96 | typedef const Type& const_reference;
|
|---|
| 97 |
|
|---|
| 98 | template <class U> G4Allocator(const G4Allocator<U>& right) throw()
|
|---|
| 99 | : mem(right.mem) {}
|
|---|
| 100 | // Copy constructor
|
|---|
| 101 |
|
|---|
| 102 | pointer address(reference r) const { return &r; }
|
|---|
| 103 | const_pointer address(const_reference r) const { return &r; }
|
|---|
| 104 | // Returns the address of values
|
|---|
| 105 |
|
|---|
| [1193] | 106 | pointer allocate(size_type n, void* = 0)
|
|---|
| [833] | 107 | {
|
|---|
| 108 | // Allocates space for n elements of type Type, but does not initialise
|
|---|
| 109 | //
|
|---|
| 110 | Type* mem_alloc = 0;
|
|---|
| 111 | if (n == 1)
|
|---|
| 112 | mem_alloc = MallocSingle();
|
|---|
| 113 | else
|
|---|
| 114 | mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type)));
|
|---|
| 115 | return mem_alloc;
|
|---|
| 116 | }
|
|---|
| 117 | void deallocate(pointer p, size_type n)
|
|---|
| 118 | {
|
|---|
| 119 | // Deallocates n elements of type Type, but doesn't destroy
|
|---|
| 120 | //
|
|---|
| 121 | if (n == 1)
|
|---|
| 122 | FreeSingle(p);
|
|---|
| 123 | else
|
|---|
| 124 | ::operator delete((void*)p);
|
|---|
| 125 | return;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | void construct(pointer p, const Type& val) { new((void*)p) Type(val); }
|
|---|
| 129 | // Initialises *p by val
|
|---|
| 130 | void destroy(pointer p) { p->~Type(); }
|
|---|
| 131 | // Destroy *p but doesn't deallocate
|
|---|
| 132 |
|
|---|
| 133 | size_type max_size() const throw()
|
|---|
| 134 | {
|
|---|
| 135 | // Returns the maximum number of elements that can be allocated
|
|---|
| 136 | //
|
|---|
| 137 | return 2147483647/sizeof(Type);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | template <class U>
|
|---|
| 141 | struct rebind { typedef G4Allocator<U> other; };
|
|---|
| 142 | // Rebind allocator to type U
|
|---|
| 143 |
|
|---|
| 144 | G4AllocatorPool mem;
|
|---|
| 145 | // Pool of elements of sizeof(Type)
|
|---|
| 146 | };
|
|---|
| 147 |
|
|---|
| 148 | // ------------------------------------------------------------
|
|---|
| 149 | // Inline implementation
|
|---|
| 150 | // ------------------------------------------------------------
|
|---|
| 151 |
|
|---|
| 152 | // Initialization of the static pool
|
|---|
| 153 | //
|
|---|
| 154 | // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type));
|
|---|
| 155 |
|
|---|
| 156 | // ************************************************************
|
|---|
| 157 | // G4Allocator constructor
|
|---|
| 158 | // ************************************************************
|
|---|
| 159 | //
|
|---|
| 160 | template <class Type>
|
|---|
| 161 | G4Allocator<Type>::G4Allocator() throw()
|
|---|
| 162 | : mem(sizeof(Type))
|
|---|
| 163 | {
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | // ************************************************************
|
|---|
| 167 | // G4Allocator destructor
|
|---|
| 168 | // ************************************************************
|
|---|
| 169 | //
|
|---|
| 170 | template <class Type>
|
|---|
| 171 | G4Allocator<Type>::~G4Allocator() throw()
|
|---|
| 172 | {
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | // ************************************************************
|
|---|
| 176 | // MallocSingle
|
|---|
| 177 | // ************************************************************
|
|---|
| 178 | //
|
|---|
| 179 | template <class Type>
|
|---|
| 180 | Type* G4Allocator<Type>::MallocSingle()
|
|---|
| 181 | {
|
|---|
| 182 | return static_cast<Type*>(mem.Alloc());
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | // ************************************************************
|
|---|
| 186 | // FreeSingle
|
|---|
| 187 | // ************************************************************
|
|---|
| 188 | //
|
|---|
| 189 | template <class Type>
|
|---|
| 190 | void G4Allocator<Type>::FreeSingle(Type* anElement)
|
|---|
| 191 | {
|
|---|
| 192 | mem.Free(anElement);
|
|---|
| 193 | return;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | // ************************************************************
|
|---|
| 197 | // ResetStorage
|
|---|
| 198 | // ************************************************************
|
|---|
| 199 | //
|
|---|
| 200 | template <class Type>
|
|---|
| 201 | void G4Allocator<Type>::ResetStorage()
|
|---|
| 202 | {
|
|---|
| 203 | // Clear all allocated storage and return it to the free store
|
|---|
| 204 | //
|
|---|
| 205 | mem.Reset();
|
|---|
| 206 | return;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | // ************************************************************
|
|---|
| 210 | // GetAllocatedSize
|
|---|
| 211 | // ************************************************************
|
|---|
| 212 | //
|
|---|
| 213 | template <class Type>
|
|---|
| 214 | size_t G4Allocator<Type>::GetAllocatedSize() const
|
|---|
| 215 | {
|
|---|
| 216 | return mem.Size();
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | // ************************************************************
|
|---|
| [1340] | 220 | // GetNoPages
|
|---|
| 221 | // ************************************************************
|
|---|
| 222 | //
|
|---|
| 223 | template <class Type>
|
|---|
| 224 | int G4Allocator<Type>::GetNoPages() const
|
|---|
| 225 | {
|
|---|
| 226 | return mem.GetNoPages();
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | // ************************************************************
|
|---|
| 230 | // GetPageSize
|
|---|
| 231 | // ************************************************************
|
|---|
| 232 | //
|
|---|
| 233 | template <class Type>
|
|---|
| 234 | size_t G4Allocator<Type>::GetPageSize() const
|
|---|
| 235 | {
|
|---|
| 236 | return mem.GetPageSize();
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | // ************************************************************
|
|---|
| 240 | // IncreasePageSize
|
|---|
| 241 | // ************************************************************
|
|---|
| 242 | //
|
|---|
| 243 | template <class Type>
|
|---|
| 244 | void G4Allocator<Type>::IncreasePageSize( unsigned int sz )
|
|---|
| 245 | {
|
|---|
| 246 | ResetStorage();
|
|---|
| 247 | mem.GrowPageSize(sz);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | // ************************************************************
|
|---|
| [833] | 251 | // operator==
|
|---|
| 252 | // ************************************************************
|
|---|
| 253 | //
|
|---|
| 254 | template <class T1, class T2>
|
|---|
| 255 | bool operator== (const G4Allocator<T1>&, const G4Allocator<T2>&) throw()
|
|---|
| 256 | {
|
|---|
| 257 | return true;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | // ************************************************************
|
|---|
| 261 | // operator!=
|
|---|
| 262 | // ************************************************************
|
|---|
| 263 | //
|
|---|
| 264 | template <class T1, class T2>
|
|---|
| 265 | bool operator!= (const G4Allocator<T1>&, const G4Allocator<T2>&) throw()
|
|---|
| 266 | {
|
|---|
| 267 | return false;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | #endif
|
|---|