FEI Version of the Day
fei_Pool_alloc.hpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 
00044 #ifndef _fei_Pool_alloc_hpp_
00045 #define _fei_Pool_alloc_hpp_
00046 
00047 #include "fei_macros.hpp"
00048 #include "fei_Pool.hpp"
00049 #include <cstddef>
00050 #include <cstdlib>
00051 #include <limits>
00052 #include <new>
00053 #include <stdexcept>
00054 #include <iostream>
00055 
00056 fei_Pool* get_fei_mem_pool(size_t n);
00057 
00068 template<typename T>
00069 class fei_Pool_alloc {
00070  private:
00071   fei_Pool* mem; //pool of elements
00072   size_t n_;
00073 
00074  public:
00075   typedef T value_type;
00076   typedef std::size_t size_type;
00077   typedef T* pointer;
00078   typedef const T* const_pointer;
00079   typedef T& reference;
00080   typedef const T& const_reference;
00081   typedef std::ptrdiff_t difference_type;
00082 
00083   // Boilerplate allocator stuff
00084   template <typename U>
00085   struct rebind
00086   {
00087     typedef fei_Pool_alloc<U> other;
00088   };
00089 
00090   pointer address (reference value) const
00091   {
00092     return &value;
00093   }
00094   const_pointer address (const_reference value) const
00095   {
00096     return &value;
00097   }
00098 
00099   fei_Pool_alloc() throw();
00100   fei_Pool_alloc(const T&) throw();
00101   template<typename U> fei_Pool_alloc(const fei_Pool_alloc<U>&) throw()
00102    : mem(NULL),n_(0) {}
00103 
00104   ~fei_Pool_alloc() throw();
00105 
00106   pointer allocate(size_type n, const void* hint = NULL);
00107   void deallocate(pointer p, size_type n);
00108 
00109   template<typename U> void construct(U* p, const U& val)
00110   { new(p) U(val); }
00111 
00112   void construct(pointer p, const T& val)
00113   { new(p) T(val); }
00114 
00115   template<typename U> void destroy(U* p)
00116   { p->~U(); }
00117 
00118   void destroy(pointer p)
00119   { p->~T(); }
00120 
00121   size_type max_size() const throw() { return std::numeric_limits<size_type>::max(); }
00122 
00123 };
00124 
00125 template<typename T> fei_Pool_alloc<T>::fei_Pool_alloc() throw() : mem(NULL),n_(0) {}
00126 template<typename T> fei_Pool_alloc<T>::fei_Pool_alloc(const T&) throw() : mem(NULL),n_(0) {}
00127 
00128 template<typename T> fei_Pool_alloc<T>::~fei_Pool_alloc() throw() {}
00129 
00130 template<typename T>
00131 T* fei_Pool_alloc<T>::allocate(size_type n, const void*)
00132 {
00133   if (n==0) return NULL;
00134 
00135   if (n_ == 0) {
00136     n_ = n;
00137     mem = get_fei_mem_pool(n_*sizeof(T));
00138   }
00139 
00140   if (n != n_) {
00141     std::cerr << "fei_Pool_alloc ERROR, allocate given bad length ("<<n
00142        <<"), must be " <<n_<<". throwing exception."<<std::endl;
00143     throw std::bad_alloc();
00144   }
00145   return static_cast<T*>(mem->alloc());
00146 }
00147 
00148 template<typename T>
00149 void fei_Pool_alloc<T>::deallocate(pointer p, size_type n)
00150 {
00151   if (p == NULL || n == 0) return;
00152 
00153   if (n == n_) {
00154     mem->free(p);
00155     return;
00156   }
00157 
00158   std::cerr << "fei_Pool_alloc ERROR, deallocate given bad length ("<<n
00159     <<"), must be " <<n_<<". aborting."<<std::endl;
00160   std::abort();
00161 }
00162 
00163 template<typename T>
00164 inline bool operator==(const fei_Pool_alloc<T>&,
00165                        const fei_Pool_alloc<T>&) throw()
00166 { return true; }
00167 template<typename T>
00168 inline bool operator!=(const fei_Pool_alloc<T>&,
00169                        const fei_Pool_alloc<T>&) throw()
00170 { return false; }
00171 
00172 #endif
00173 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends