• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

sst/elements/genericProc/FE/pool.h

00001 // Copyright 2007 Sandia Corporation. Under the terms
00002 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
00003 // Government retains certain rights in this software.
00004 // 
00005 // Copyright (c) 2005-2007, Sandia Corporation
00006 // All rights reserved.
00007 // Copyright (c) 2003-2005, University of Notre Dame
00008 // All rights reserved.
00009 // 
00010 // This file is part of the SST software package. For license
00011 // information, see the LICENSE file in the top level directory of the
00012 // distribution.
00013 
00014 
00015 #ifndef _POOL_H_
00016 #define _POOL_H_
00017 
00018 #include <vector>
00019 using std::vector;
00020 //#include "sst/boost.h"
00021 
00022 //: Abstract Pool class
00023 //
00024 // Templated, and gives access and return pointers to templated class
00025 //!SEC:Level_1
00026 template <class T>
00027 class pool 
00028 {
00029 #if 0 //WANT_CHECKPOINT_SUPPORT    
00030 BOOST_SERIALIZE {
00031     ar & BOOST_SERIALIZATION_NVP(_pool);
00032     ar & BOOST_SERIALIZATION_NVP(num);
00033   }
00034 #endif
00035 public:
00036   pool( int size = 10 );
00037   ~pool( void );
00038   inline T* getItem( void );
00039   inline void returnItem( T* item );
00040   
00041 private:
00042   int num;
00043   //: The actual pool
00044   //
00045   // simple vector
00046   vector<T*> _pool;
00047 };
00048 
00049 //: Constructor
00050 //
00051 //!in: size: The initial size of the pool
00052 template <class T>
00053 pool<T>::pool( const int size )
00054 {
00055   /* Make it a bit larger than probably needed. */
00056   _pool.reserve(size * 2);
00057   /* Fill it up */
00058   for (int i = 0 ; i < size ; ++i ) {
00059     _pool.push_back(new T);
00060   }
00061   num = size;
00062 }
00063 
00064 //: Destructor
00065 //
00066 // Temporarily disabled, because I'm not sure how to handle the
00067 // "looping" - when instrPool tries to delete an instruction, the
00068 // instruction tries to add its singleInstructions to the
00069 // singInstPool, which has already been deleted...
00070 template <class T>
00071 pool<T>::~pool( void )
00072 {
00073   /*for (vector<T*>::iterator i = _pool.begin();
00074        i != _pool.end();
00075        ++i) {
00076        delqete *i;
00077    }*/
00078 }
00079 
00080 //: Returns an item from the pool
00081 //
00082 // If its in the pool, it returns it, otherwise it new() another
00083 template <class T>
00084 T* pool<T>::getItem( void )
00085 {
00086   T* ret;
00087   //if (_pool.empty() == 0) {
00088   if (num > 0) {
00089     num--;
00090     ret = _pool.back();
00091     _pool.pop_back();
00092   } else {
00093     ret = new T;
00094     num++;
00095     _pool.push_back(new T);
00096   }
00097   if (ret == 0) {
00098     fprintf(stderr,"Pool Error (returning 0)\n");
00099     exit(-1);
00100   }
00101   return ret;
00102 }
00103 
00104 //: Returns and item to the pool
00105 //
00106 //!in: item: pointer to item to return
00107 template<class T>
00108 void pool<T>::returnItem( T* item )
00109 {
00110   if (item) {
00111     num++;
00112     _pool.push_back(item);
00113   }
00114 }
00115 
00116 #endif /* _POOL_H_ */

Generated on Fri Oct 22 2010 11:02:21 for SST by  doxygen 1.7.1