![]() |
|
|||
File indexing completed on 2025-02-21 10:00:28
0001 /***********************************************************************************\ 0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations * 0003 * * 0004 * This software is distributed under the terms of the Apache version 2 licence, * 0005 * copied verbatim in the file "LICENSE". * 0006 * * 0007 * In applying this licence, CERN does not waive the privileges and immunities * 0008 * granted to it by virtue of its status as an Intergovernmental Organization * 0009 * or submit itself to any jurisdiction. * 0010 \***********************************************************************************/ 0011 // ============================================================================ 0012 /** @file 0013 * Allocator 0014 * The file is copied form Geant4 project 0015 * @date 2006-02-10 0016 */ 0017 // ============================================================================ 0018 0019 // 0020 // ******************************************************************** 0021 // * DISCLAIMER * 0022 // * * 0023 // * The following disclaimer summarizes all the specific disclaimers * 0024 // * of contributors to this software. The specific disclaimers,which * 0025 // * govern, are listed with their locations in: * 0026 // * http://cern.ch/geant4/license * 0027 // * * 0028 // * Neither the authors of this software system, nor their employing * 0029 // * institutes,nor the agencies providing financial support for this * 0030 // * work make any representation or warranty, express or implied, * 0031 // * regarding this software system or assume any liability for its * 0032 // * use. * 0033 // * * 0034 // * This code implementation is the intellectual property of the * 0035 // * GEANT4 collaboration. * 0036 // * By copying, distributing or modifying the Program (or any work * 0037 // * based on the Program) you indicate your acceptance of this * 0038 // * statement, and all its terms. * 0039 // ******************************************************************** 0040 // 0041 // 0042 // ------------------------------------------------------------ 0043 // GEANT 4 class header file 0044 // 0045 // Class Description: 0046 // 0047 // A class for fast allocation of objects to the heap through a pool of 0048 // chunks organised as linked list. It's meant to be used by associating 0049 // it to the object to be allocated and defining for it new and delete 0050 // operators via MallocSingle() and FreeSingle() methods. 0051 0052 // ---------------- G4Allocator ---------------- 0053 // 0054 // Author: G.Cosmo (CERN), November 2000 0055 // ------------------------------------------------------------ 0056 0057 // ============================================================================ 0058 #ifndef GAUDIKERNEL_Allocator_h 0059 #define GAUDIKERNEL_Allocator_h 1 0060 // ============================================================================ 0061 // Include files 0062 // ============================================================================ 0063 // STD & STL 0064 // ============================================================================ 0065 #include <cstddef> 0066 // ============================================================================ 0067 // GaudiKernel 0068 // ============================================================================ 0069 #include "GaudiKernel/AllocatorPool.h" 0070 // ============================================================================ 0071 0072 namespace GaudiUtils { 0073 /** @class Allocator Allocator.h GaudiKernel/Allocator.h 0074 * Allocator. 0075 * The class is imported from Geant4 project 0076 * @date 2006-02-10 0077 */ 0078 template <class Type> 0079 class Allocator { 0080 public: // with description 0081 /// Constructor 0082 Allocator() throw(); 0083 /// destructor 0084 ~Allocator() throw() {} 0085 0086 /** Malloc and Free methods to be used when overloading 0087 * new and delete operators in the client <Type> object 0088 */ 0089 inline Type* MallocSingle(); 0090 inline void FreeSingle( Type* anElement ); 0091 0092 /** Returns allocated storage to the free store, resets 0093 * allocator and page sizes. 0094 * Note: contents in memory are lost using this call ! 0095 */ 0096 inline void ResetStorage(); 0097 0098 /// Returns the size of the total memory allocated 0099 inline size_t GetAllocatedSize() const; 0100 0101 public: // without description 0102 // This public section includes standard methods and types 0103 // required if the allocator is to be used as alternative 0104 // allocator for STL containers. 0105 // NOTE: the code below is a trivial implementation to make 0106 // this class an STL compliant allocator. 0107 // It is anyhow NOT recommended to use this class as 0108 // alternative allocator for STL containers ! 0109 0110 typedef Type value_type; 0111 typedef size_t size_type; 0112 typedef ptrdiff_t difference_type; 0113 typedef Type* pointer; 0114 typedef const Type* const_pointer; 0115 typedef Type& reference; 0116 typedef const Type& const_reference; 0117 0118 /// Copy constructor 0119 template <class U> 0120 Allocator( const Allocator<U>& right ) throw() : mem( right.mem ) {} 0121 0122 /// Returns the address of values 0123 pointer address( reference r ) const { return &r; } 0124 const_pointer address( const_reference r ) const { return &r; } 0125 0126 /// Allocates space for n elements of type Type, but does not initialise 0127 pointer allocate( size_type n, void* /*hint*/ = 0 ) { 0128 // Allocates space for n elements of type Type, but does not initialise 0129 // 0130 Type* mem_alloc = 0; 0131 if ( n == 1 ) 0132 mem_alloc = MallocSingle(); 0133 else 0134 mem_alloc = static_cast<Type*>( ::operator new( n * sizeof( Type ) ) ); 0135 return mem_alloc; 0136 } 0137 0138 /// Deallocates n elements of type Type, but doesn't destroy 0139 void deallocate( pointer p, size_type n ) { 0140 // Deallocates n elements of type Type, but doesn't destroy 0141 // 0142 if ( n == 1 ) 0143 FreeSingle( p ); 0144 else 0145 ::operator delete( (void*)p ); 0146 return; 0147 } 0148 0149 /// Initialises *p by val 0150 void construct( pointer p, const Type& val ) { new ( (void*)p ) Type( val ); } 0151 /// Destroy *p but doesn't deallocate 0152 void destroy( pointer p ) { p->~Type(); } 0153 0154 /// Returns the maximum number of elements that can be allocated 0155 size_type max_size() const throw() { 0156 // Returns the maximum number of elements that can be allocated 0157 // 0158 return 2147483647 / sizeof( Type ); 0159 } 0160 0161 // Rebind allocator to type U 0162 template <class U> 0163 struct rebind { 0164 typedef Allocator<U> other; 0165 }; 0166 0167 private: 0168 GaudiUtils::AllocatorPool mem; 0169 // Pool of elements of sizeof(Type) 0170 }; 0171 0172 } // namespace GaudiUtils 0173 0174 // ------------------------------------------------------------ 0175 // Inline implementation 0176 // ------------------------------------------------------------ 0177 0178 // Initialization of the static pool 0179 // 0180 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type)); 0181 0182 // ************************************************************ 0183 // G4Allocator constructor 0184 // ************************************************************ 0185 // 0186 template <class Type> 0187 GaudiUtils::Allocator<Type>::Allocator() throw() : mem( sizeof( Type ) ) {} 0188 0189 // ************************************************************ 0190 // MallocSingle 0191 // ************************************************************ 0192 // 0193 template <class Type> 0194 Type* GaudiUtils::Allocator<Type>::MallocSingle() { 0195 return static_cast<Type*>( mem.Alloc() ); 0196 } 0197 0198 // ************************************************************ 0199 // FreeSingle 0200 // ************************************************************ 0201 // 0202 template <class Type> 0203 void GaudiUtils::Allocator<Type>::FreeSingle( Type* anElement ) { 0204 mem.Free( anElement ); 0205 return; 0206 } 0207 0208 // ************************************************************ 0209 // ResetStorage 0210 // ************************************************************ 0211 // 0212 template <class Type> 0213 void GaudiUtils::Allocator<Type>::ResetStorage() { 0214 // Clear all allocated storage and return it to the free store 0215 // 0216 mem.Reset(); 0217 return; 0218 } 0219 0220 // ************************************************************ 0221 // GetAllocatedSize 0222 // ************************************************************ 0223 // 0224 template <class Type> 0225 size_t GaudiUtils::Allocator<Type>::GetAllocatedSize() const { 0226 return mem.Size(); 0227 } 0228 0229 // ************************************************************ 0230 // operator== 0231 // ************************************************************ 0232 // 0233 template <class T1, class T2> 0234 bool operator==( const GaudiUtils::Allocator<T1>&, const GaudiUtils::Allocator<T2>& ) throw() { 0235 return true; 0236 } 0237 0238 // ************************************************************ 0239 // operator!= 0240 // ************************************************************ 0241 // 0242 template <class T1, class T2> 0243 bool operator!=( const GaudiUtils::Allocator<T1>&, const GaudiUtils::Allocator<T2>& ) throw() { 0244 return false; 0245 } 0246 0247 // ============================================================================ 0248 // The END 0249 // ============================================================================ 0250 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |