FEI Version of the Day
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 <limits>
00017 #include <new>
00018 #include <stdexcept>
00019 #include <iostream>
00020 
00021 fei_Pool* get_fei_mem_pool(size_t n);
00022 
00033 template<typename T>
00034 class fei_Pool_alloc {
00035  private:
00036   fei_Pool* mem; //pool of elements
00037   size_t n_;
00038 
00039  public:
00040   typedef T value_type;
00041   typedef std::size_t size_type;
00042   typedef T* pointer;
00043   typedef const T* const_pointer;
00044   typedef T& reference;
00045   typedef const T& const_reference;
00046   typedef std::ptrdiff_t difference_type;
00047 
00048   // Boilerplate allocator stuff
00049   template <typename U>
00050   struct rebind
00051   {
00052     typedef fei_Pool_alloc<U> other;
00053   };
00054 
00055   pointer address (reference value) const
00056   {
00057     return &value;
00058   }
00059   const_pointer address (const_reference value) const
00060   {
00061     return &value;
00062   }
00063 
00064   fei_Pool_alloc() throw();
00065   fei_Pool_alloc(const T&) throw();
00066   template<typename U> fei_Pool_alloc(const fei_Pool_alloc<U>&) throw()
00067    : mem(NULL),n_(0) {}
00068 
00069   ~fei_Pool_alloc() throw();
00070 
00071   pointer allocate(size_type n, const void* hint = NULL);
00072   void deallocate(pointer p, size_type n);
00073 
00074   template<typename U> void construct(U* p, const U& val)
00075   { new(p) U(val); }
00076 
00077   void construct(pointer p, const T& val)
00078   { new(p) T(val); }
00079 
00080   template<typename U> void destroy(U* p)
00081   { p->~U(); }
00082 
00083   void destroy(pointer p)
00084   { p->~T(); }
00085 
00086   size_type max_size() const throw() { return std::numeric_limits<size_type>::max(); }
00087 
00088 };
00089 
00090 template<typename T> fei_Pool_alloc<T>::fei_Pool_alloc() throw() : mem(NULL),n_(0) {}
00091 template<typename T> fei_Pool_alloc<T>::fei_Pool_alloc(const T&) throw() : mem(NULL),n_(0) {}
00092 
00093 template<typename T> fei_Pool_alloc<T>::~fei_Pool_alloc() throw() {}
00094 
00095 template<typename T>
00096 T* fei_Pool_alloc<T>::allocate(size_type n, const void*)
00097 {
00098   if (n==0) return NULL;
00099 
00100   if (n_ == 0) {
00101     n_ = n;
00102     mem = get_fei_mem_pool(n_*sizeof(T));
00103   }
00104 
00105   if (n != n_) {
00106     std::cerr << "fei_Pool_alloc ERROR, allocate given bad length ("<<n
00107        <<"), must be " <<n_<<". throwing exception."<<std::endl;
00108     throw std::bad_alloc();
00109   }
00110   return static_cast<T*>(mem->alloc());
00111 }
00112 
00113 template<typename T>
00114 void fei_Pool_alloc<T>::deallocate(pointer p, size_type n)
00115 {
00116   if (p == NULL || n == 0) return;
00117 
00118   if (n == n_) {
00119     mem->free(p);
00120     return;
00121   }
00122 
00123   std::cerr << "fei_Pool_alloc ERROR, deallocate given bad length ("<<n
00124     <<"), must be " <<n_<<". aborting."<<std::endl;
00125   std::abort();
00126 }
00127 
00128 template<typename T>
00129 inline bool operator==(const fei_Pool_alloc<T>&,
00130                        const fei_Pool_alloc<T>&) throw()
00131 { return true; }
00132 template<typename T>
00133 inline bool operator!=(const fei_Pool_alloc<T>&,
00134                        const fei_Pool_alloc<T>&) throw()
00135 { return false; }
00136 
00137 #endif
00138 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends