Sacado_Fad_MemPoolStorage.hpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #ifndef SACADO_FAD_MEMPOOLSTORAGE_HPP
00033 #define SACADO_FAD_MEMPOOLSTORAGE_HPP
00034 
00035 #include <new>
00036 #include <cstring>
00037 
00038 #include "Sacado_Traits.hpp"
00039 #include "Sacado_Fad_MemPool.hpp"
00040 
00041 namespace Sacado {
00042 
00043   namespace Fad {
00044 
00048     template <typename T, bool isScalar = IsScalarType<T>::value>
00049     struct mp_array {
00050 
00052       static inline T* get_and_fill(int sz, MemPool* pool) {
00053   if (sz) {
00054     T* m = static_cast<T*>(pool->alloc());
00055     T* p = m;
00056     for (int i=0; i<sz; ++i)
00057       new (p++) T(0.);
00058     return m;
00059   }
00060   else
00061     return NULL;
00062       }
00063 
00068       static inline T* get_and_fill(const T* src, int sz, MemPool* pool) {
00069   if (sz) {
00070     T* m = static_cast<T*>(pool->alloc());
00071     T* p = m; 
00072     for (int i=0; i<sz; ++i)
00073       new (p++) T(*(src++));
00074     return m;
00075   }
00076   else
00077     return NULL;
00078       }
00079 
00081       static inline void copy(const T* src, T*  dest, int sz) {
00082   for (int i=0; i<sz; ++i)
00083     *(dest++) = *(src++);
00084       }
00085 
00087       static inline void zero(T* dest, int sz) {
00088   for (int i=0; i<sz; ++i)
00089     *(dest++) = T(0.);
00090       }
00091 
00093       static inline void destroy_and_release(T* m, int sz, MemPool* pool) {
00094   T* e = m+sz;
00095   for (T* b = m; b!=e; b++)
00096     b->~T();
00097   pool->free((void*) m);
00098       }
00099     };
00100 
00105     template <typename T>
00106     struct mp_array<T,true> {
00107 
00109       static inline T* get_and_fill(int sz, MemPool* pool) {
00110   if (sz) {
00111     T* m = static_cast<T*>(pool->alloc());
00112     std::memset(m,0,sz*sizeof(T));
00113     return m;
00114   }
00115   else
00116     return NULL;
00117       }
00118 
00123       static inline T* get_and_fill(const T* src, int sz, MemPool* pool) {
00124   if (sz) {
00125     T* m = static_cast<T*>(pool->alloc());
00126     T* p = m; 
00127     for (int i=0; i<sz; ++i)
00128       new (p++) T(*(src++));
00129     return m;
00130   }
00131   else
00132     return NULL;
00133       }
00134 
00136       static inline void copy(const T* src, T* dest, int sz) {
00137   std::memcpy(dest,src,sz*sizeof(T));
00138       }
00139 
00141       static inline void zero(T* dest, int sz) {
00142   std::memset(dest,0,sz*sizeof(T));
00143       }
00144 
00146       static inline void destroy_and_release(T* m, int sz, MemPool* pool) {
00147   pool->free((void*) m);
00148       }
00149     };
00150 
00152     template <typename T> 
00153     class MemPoolStorage {
00154 
00155     public:
00156 
00158       MemPoolStorage(const T & x) : 
00159   val_(x), sz_(0), len_(0), dx_(NULL), myPool_(defaultPool_) {}
00160 
00162 
00165       MemPoolStorage(const int sz, const T & x) : 
00166   val_(x), sz_(sz), len_(sz), myPool_(defaultPool_) {
00167   dx_ = mp_array<T>::get_and_fill(sz_, myPool_);
00168       }
00169 
00171       MemPoolStorage(const MemPoolStorage& x) : 
00172   val_(x.val_), sz_(x.sz_), len_(x.sz_), myPool_(x.myPool_) {
00173   dx_ = mp_array<T>::get_and_fill(x.dx_, sz_, myPool_);
00174       }
00175       
00177       ~MemPoolStorage() {
00178   if (len_ != 0)
00179     mp_array<T>::destroy_and_release(dx_, len_, myPool_);
00180       }
00181 
00183       MemPoolStorage& operator=(const MemPoolStorage& x) { 
00184   val_ = x.val_;
00185   if (sz_ != x.sz_) {
00186     sz_ = x.sz_;
00187     if (x.sz_ > len_) {
00188       if (len_ != 0)
00189         mp_array<T>::destroy_and_release(dx_, len_, myPool_);
00190       len_ = x.sz_;
00191       myPool_ = x.myPool_;
00192       dx_ = mp_array<T>::get_and_fill(x.dx_, sz_, myPool_);
00193     }
00194     else 
00195       mp_array<T>::copy(x.dx_, dx_, sz_);
00196   }
00197   else 
00198     mp_array<T>::copy(x.dx_, dx_, sz_);
00199 
00200   return *this; 
00201       } 
00202 
00204       int size() const { return sz_;}
00205 
00207       void resize(int sz) { 
00208   if (sz > len_) {
00209      if (len_ != 0)
00210       mp_array<T>::destroy_and_release(dx_, len_, myPool_);
00211     myPool_ = defaultPool_;
00212     dx_ = mp_array<T>::get_and_fill(sz, myPool_);
00213     len_ = sz;
00214   }
00215   sz_ = sz;
00216       }
00217 
00219       void zero() { 
00220   mp_array<T>::zero(dx_, sz_);
00221       }
00222 
00224       const T& val() const { return val_; }
00225 
00227       T& val() { return val_; }
00228 
00230       const T* dx() const { return dx_;}
00231 
00233       T dx(int i) const { return sz_ ? dx_[i] : T(0.); }
00234     
00236       T& fastAccessDx(int i) { return dx_[i];}
00237 
00239       const T& fastAccessDx(int i) const { return dx_[i];}
00240 
00241     private:
00242 
00244       T val_;
00245 
00247       int sz_;
00248 
00250       int len_;
00251 
00253       T* dx_;
00254 
00255     public:
00256 
00258       static MemPool* defaultPool_;
00259 
00260     protected:
00261 
00263       MemPool* myPool_;
00264 
00265     }; // class MemPoolStorage
00266 
00267   } // namespace Fad
00268 
00269 } // namespace Sacado
00270 
00271 #endif // SACADO_FAD_MEMPOOLSTORAGE_HPP

Generated on Wed May 12 21:39:34 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7