// // ******************************************************************** // * License and Disclaimer * // * * // * The Geant4 software is copyright of the Copyright Holders of * // * the Geant4 Collaboration. It is provided under the terms and * // * conditions of the Geant4 Software License, included in the file * // * LICENSE and available at http://cern.ch/geant4/license . These * // * include a list of copyright holders. * // * * // * Neither the authors of this software system, nor their employing * // * institutes,nor the agencies providing financial support for this * // * work make any representation or warranty, express or implied, * // * regarding this software system or assume any liability for its * // * use. Please see the license in the file LICENSE and URL above * // * for the full disclaimer and the limitation of liability. * // * * // * This code implementation is the result of the scientific and * // * technical work of the GEANT4 collaboration. * // * By using, copying, modifying or distributing the software (or * // * any work based on the software) you agree to acknowledge its * // * use in resulting scientific publications, and indicate your * // * acceptance of all terms of the Geant4 Software license. * // ******************************************************************** // // // $Id: G4Allocator.hh,v 1.18 2006/06/29 19:01:16 gunter Exp $ // GEANT4 tag $Name: geant4-09-02 $ // // // ------------------------------------------------------------ // GEANT 4 class header file // // Class Description: // // A class for fast allocation of objects to the heap through a pool of // chunks organised as linked list. It's meant to be used by associating // it to the object to be allocated and defining for it new and delete // operators via MallocSingle() and FreeSingle() methods. // ---------------- G4Allocator ---------------- // // Author: G.Cosmo (CERN), November 2000 // ------------------------------------------------------------ #ifndef G4Allocator_h #define G4Allocator_h 1 #include #include "G4AllocatorPool.hh" template class G4Allocator { public: // with description G4Allocator() throw(); ~G4Allocator() throw(); // Constructor & destructor inline Type* MallocSingle(); inline void FreeSingle(Type* anElement); // Malloc and Free methods to be used when overloading // new and delete operators in the client object inline void ResetStorage(); // Returns allocated storage to the free store, resets // allocator and page sizes. // Note: contents in memory are lost using this call ! inline size_t GetAllocatedSize() const; // Returns the size of the total memory allocated public: // without description // This public section includes standard methods and types // required if the allocator is to be used as alternative // allocator for STL containers. // NOTE: the code below is a trivial implementation to make // this class an STL compliant allocator. // It is anyhow NOT recommended to use this class as // alternative allocator for STL containers ! typedef Type value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef Type* pointer; typedef const Type* const_pointer; typedef Type& reference; typedef const Type& const_reference; template G4Allocator(const G4Allocator& right) throw() : mem(right.mem) {} // Copy constructor pointer address(reference r) const { return &r; } const_pointer address(const_reference r) const { return &r; } // Returns the address of values pointer allocate(size_type n, void* hint = 0) { // Allocates space for n elements of type Type, but does not initialise // Type* mem_alloc = 0; if (n == 1) mem_alloc = MallocSingle(); else mem_alloc = static_cast(::operator new(n*sizeof(Type))); return mem_alloc; } void deallocate(pointer p, size_type n) { // Deallocates n elements of type Type, but doesn't destroy // if (n == 1) FreeSingle(p); else ::operator delete((void*)p); return; } void construct(pointer p, const Type& val) { new((void*)p) Type(val); } // Initialises *p by val void destroy(pointer p) { p->~Type(); } // Destroy *p but doesn't deallocate size_type max_size() const throw() { // Returns the maximum number of elements that can be allocated // return 2147483647/sizeof(Type); } template struct rebind { typedef G4Allocator other; }; // Rebind allocator to type U private: G4AllocatorPool mem; // Pool of elements of sizeof(Type) }; // ------------------------------------------------------------ // Inline implementation // ------------------------------------------------------------ // Initialization of the static pool // // template G4AllocatorPool G4Allocator::mem(sizeof(Type)); // ************************************************************ // G4Allocator constructor // ************************************************************ // template G4Allocator::G4Allocator() throw() : mem(sizeof(Type)) { } // ************************************************************ // G4Allocator destructor // ************************************************************ // template G4Allocator::~G4Allocator() throw() { } // ************************************************************ // MallocSingle // ************************************************************ // template Type* G4Allocator::MallocSingle() { return static_cast(mem.Alloc()); } // ************************************************************ // FreeSingle // ************************************************************ // template void G4Allocator::FreeSingle(Type* anElement) { mem.Free(anElement); return; } // ************************************************************ // ResetStorage // ************************************************************ // template void G4Allocator::ResetStorage() { // Clear all allocated storage and return it to the free store // mem.Reset(); return; } // ************************************************************ // GetAllocatedSize // ************************************************************ // template size_t G4Allocator::GetAllocatedSize() const { return mem.Size(); } // ************************************************************ // operator== // ************************************************************ // template bool operator== (const G4Allocator&, const G4Allocator&) throw() { return true; } // ************************************************************ // operator!= // ************************************************************ // template bool operator!= (const G4Allocator&, const G4Allocator&) throw() { return false; } #endif