fei_Pool_alloc.hpp

00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #ifndef _fei_Pool_alloc_hpp_
00010 #define _fei_Pool_alloc_hpp_
00011 
00012 #include "fei_macros.hpp"
00013 #include "fei_Pool.hpp"
00014 #include <cstddef>
00015 #include <cstdlib>
00016 #include <new>
00017 #include <stdexcept>
00018 #include <iostream>
00019 
00020 fei_Pool* get_fei_mem_pool(size_t n);
00021 
00032 template<typename T>
00033 class fei_Pool_alloc {
00034  private:
00035   fei_Pool* mem; //pool of elements
00036   size_t n_;
00037 
00038  public:
00039   typedef T value_type;
00040   typedef std::size_t size_type;
00041   typedef T* pointer;
00042   typedef const T* const_pointer;
00043   typedef T& reference;
00044   typedef const T& const_reference;
00045   typedef std::ptrdiff_t difference_type;
00046 
00047   // Boilerplate allocator stuff
00048   template <typename U>
00049   struct rebind
00050   {
00051     typedef fei_Pool_alloc<U> other;
00052   };
00053 
00054   pointer address (reference value) const
00055   {
00056     return &value;
00057   }
00058   const_pointer address (const_reference value) const
00059   {
00060     return &value;
00061   }
00062 
00063   fei_Pool_alloc() throw();
00064   fei_Pool_alloc(const T&) throw();
00065   template<typename U> fei_Pool_alloc(const fei_Pool_alloc<U>&) throw()
00066    : mem(NULL),n_(0) {}
00067 
00068   ~fei_Pool_alloc() throw();
00069 
00070   pointer allocate(size_type n, const void* hint = NULL);
00071   void deallocate(pointer p, size_type n);
00072 
00073   template<typename U> void construct(U* p, const U& val)
00074   { new(p) U(val); }
00075 
00076   void construct(pointer p, const T& val)
00077   { new(p) T(val); }
00078 
00079   template<typename U> void destroy(U* p)
00080   { p->~U(); }
00081 
00082   void destroy(pointer p)
00083   { p->~T(); }
00084 
00085   size_type max_size() const throw() { return n_>0 ? n_ : 1; }
00086 
00087 };
00088 
00089 template<typename T> fei_Pool_alloc<T>::fei_Pool_alloc() throw() : mem(NULL),n_(0) {}
00090 template<typename T> fei_Pool_alloc<T>::fei_Pool_alloc(const T&) throw() : mem(NULL),n_(0) {}
00091 
00092 template<typename T> fei_Pool_alloc<T>::~fei_Pool_alloc() throw() {}
00093 
00094 template<typename T>
00095 T* fei_Pool_alloc<T>::allocate(size_type n, const void*)
00096 {
00097   if (n==0) return NULL;
00098 
00099   if (n_ == 0) {
00100     n_ = n;
00101     mem = get_fei_mem_pool(n_*sizeof(T));
00102   }
00103 
00104   if (n != n_) {
00105     std::cerr << "fei_Pool_alloc ERROR, allocate given bad length ("<<n
00106        <<"), must be " <<n_<<". throwing exception."<<std::endl;
00107     throw std::bad_alloc();
00108   }
00109   return static_cast<T*>(mem->alloc());
00110 }
00111 
00112 template<typename T>
00113 void fei_Pool_alloc<T>::deallocate(pointer p, size_type n)
00114 {
00115   if (p == NULL || n == 0) return;
00116 
00117   if (n == n_) {
00118     mem->free(p);
00119     return;
00120   }
00121 
00122   std::cerr << "fei_Pool_alloc ERROR, deallocate given bad length ("<<n
00123     <<"), must be " <<n_<<". aborting."<<std::endl;
00124   std::abort();
00125 }
00126 
00127 template<typename T>
00128 inline bool operator==(const fei_Pool_alloc<T>&,
00129                        const fei_Pool_alloc<T>&) throw()
00130 { return true; }
00131 template<typename T>
00132 inline bool operator!=(const fei_Pool_alloc<T>&,
00133                        const fei_Pool_alloc<T>&) throw()
00134 { return false; }
00135 
00136 #endif
00137 

Generated on Wed May 12 21:30:41 2010 for FEI by  doxygen 1.4.7