[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 | // |
---|
| 27 | // $Id: G4Allocator.hh,v 1.18 2006/06/29 19:01:16 gunter Exp $ |
---|
| 28 | // GEANT4 tag $Name: $ |
---|
| 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(); |
---|
| 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* hint = 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 | private: |
---|
| 140 | |
---|
| 141 | G4AllocatorPool mem; |
---|
| 142 | // Pool of elements of sizeof(Type) |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | // ------------------------------------------------------------ |
---|
| 146 | // Inline implementation |
---|
| 147 | // ------------------------------------------------------------ |
---|
| 148 | |
---|
| 149 | // Initialization of the static pool |
---|
| 150 | // |
---|
| 151 | // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type)); |
---|
| 152 | |
---|
| 153 | // ************************************************************ |
---|
| 154 | // G4Allocator constructor |
---|
| 155 | // ************************************************************ |
---|
| 156 | // |
---|
| 157 | template <class Type> |
---|
| 158 | G4Allocator<Type>::G4Allocator() throw() |
---|
| 159 | : mem(sizeof(Type)) |
---|
| 160 | { |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | // ************************************************************ |
---|
| 164 | // G4Allocator destructor |
---|
| 165 | // ************************************************************ |
---|
| 166 | // |
---|
| 167 | template <class Type> |
---|
| 168 | G4Allocator<Type>::~G4Allocator() throw() |
---|
| 169 | { |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | // ************************************************************ |
---|
| 173 | // MallocSingle |
---|
| 174 | // ************************************************************ |
---|
| 175 | // |
---|
| 176 | template <class Type> |
---|
| 177 | Type* G4Allocator<Type>::MallocSingle() |
---|
| 178 | { |
---|
| 179 | return static_cast<Type*>(mem.Alloc()); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | // ************************************************************ |
---|
| 183 | // FreeSingle |
---|
| 184 | // ************************************************************ |
---|
| 185 | // |
---|
| 186 | template <class Type> |
---|
| 187 | void G4Allocator<Type>::FreeSingle(Type* anElement) |
---|
| 188 | { |
---|
| 189 | mem.Free(anElement); |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | // ************************************************************ |
---|
| 194 | // ResetStorage |
---|
| 195 | // ************************************************************ |
---|
| 196 | // |
---|
| 197 | template <class Type> |
---|
| 198 | void G4Allocator<Type>::ResetStorage() |
---|
| 199 | { |
---|
| 200 | // Clear all allocated storage and return it to the free store |
---|
| 201 | // |
---|
| 202 | mem.Reset(); |
---|
| 203 | return; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | // ************************************************************ |
---|
| 207 | // GetAllocatedSize |
---|
| 208 | // ************************************************************ |
---|
| 209 | // |
---|
| 210 | template <class Type> |
---|
| 211 | size_t G4Allocator<Type>::GetAllocatedSize() const |
---|
| 212 | { |
---|
| 213 | return mem.Size(); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | // ************************************************************ |
---|
| 217 | // operator== |
---|
| 218 | // ************************************************************ |
---|
| 219 | // |
---|
| 220 | template <class T1, class T2> |
---|
| 221 | bool operator== (const G4Allocator<T1>&, const G4Allocator<T2>&) throw() |
---|
| 222 | { |
---|
| 223 | return true; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | // ************************************************************ |
---|
| 227 | // operator!= |
---|
| 228 | // ************************************************************ |
---|
| 229 | // |
---|
| 230 | template <class T1, class T2> |
---|
| 231 | bool operator!= (const G4Allocator<T1>&, const G4Allocator<T2>&) throw() |
---|
| 232 | { |
---|
| 233 | return false; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | #endif |
---|