Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:25

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // INCL++ intra-nuclear cascade model
0027 // Alain Boudard, CEA-Saclay, France
0028 // Joseph Cugnon, University of Liege, Belgium
0029 // Jean-Christophe David, CEA-Saclay, France
0030 // Pekka Kaitaniemi, CEA-Saclay, France, and Helsinki Institute of Physics, Finland
0031 // Sylvie Leray, CEA-Saclay, France
0032 // Davide Mancusi, CEA-Saclay, France
0033 //
0034 #define INCLXX_IN_GEANT4_MODE 1
0035 
0036 #include "globals.hh"
0037 
0038 /** \file G4INCLAllocationPool.hh
0039  * \brief Singleton for recycling allocation of instances of a given class
0040  *
0041  * \date 2nd October 2014
0042  * \author Davide Mancusi
0043  */
0044 
0045 #ifndef G4INCLALLOCATIONPOOL_HH
0046 #define G4INCLALLOCATIONPOOL_HH
0047 
0048 #if defined(INCL_USE_ALLOCATION_POOL) || defined(INCLXX_IN_GEANT4_MODE)
0049 
0050 #include <stack>
0051 #include <new>
0052 #include <cstddef>
0053 
0054 namespace G4INCL {
0055 
0056   template<typename T>
0057     class AllocationPool {
0058       public:
0059         static AllocationPool &getInstance() {
0060           if(!theInstance)
0061             theInstance = new AllocationPool<T>;
0062           return *theInstance;
0063         }
0064 
0065         T *getObject() {
0066           if(theStack.empty())
0067             return static_cast<T*>(::operator new(sizeof(T)));
0068           else {
0069             T *t = theStack.top();
0070             theStack.pop();
0071             return t;
0072           }
0073         }
0074 
0075         void recycleObject(T *t) {
0076           theStack.push(t);
0077         }
0078 
0079         void clear() {
0080           while(!theStack.empty()) { /* Loop checking, 10.07.2015, D.Mancusi */
0081             ::operator delete(theStack.top());
0082             theStack.pop();
0083           }
0084         }
0085 
0086       protected:
0087         AllocationPool() {}
0088         virtual ~AllocationPool() {
0089           clear();
0090         }
0091 
0092         static G4ThreadLocal AllocationPool *theInstance;
0093 
0094         std::stack<T*> theStack;
0095 
0096     };
0097 
0098   template<typename T>
0099     G4ThreadLocal AllocationPool<T> *AllocationPool<T>::theInstance = 0;
0100 
0101 }
0102 
0103 #define INCL_DECLARE_ALLOCATION_POOL(T) \
0104   public: \
0105     static void *operator new(size_t /* s */) { \
0106       ::G4INCL::AllocationPool<T> &allocator = ::G4INCL::AllocationPool<T>::getInstance(); \
0107       return allocator.getObject(); \
0108     } \
0109     static void operator delete(void *a, size_t /* s */) { \
0110       ::G4INCL::AllocationPool<T> &allocator = ::G4INCL::AllocationPool<T>::getInstance(); \
0111       allocator.recycleObject(static_cast<T *>(a)); \
0112     }
0113 
0114 #else // defined(INCL_USE_ALLOCATION_POOL) || defined(INCLXX_IN_GEANT4_MODE)
0115 #define INCL_DECLARE_ALLOCATION_POOL(T)
0116 #endif
0117 
0118 #endif // G4INCLALLOCATIONPOOL_HH