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   if (sz > 0)
00143     std::memset(dest,0,sz*sizeof(T));
00144       }
00145 
00147       static inline void destroy_and_release(T* m, int sz, MemPool* pool) {
00148   pool->free((void*) m);
00149       }
00150     };
00151 
00153     template <typename T> 
00154     class MemPoolStorage {
00155 
00156     public:
00157 
00159       MemPoolStorage(const T & x) : 
00160   val_(x), sz_(0), len_(0), dx_(NULL), myPool_(defaultPool_) {}
00161 
00163 
00166       MemPoolStorage(const int sz, const T & x) : 
00167   val_(x), sz_(sz), len_(sz), myPool_(defaultPool_) {
00168   dx_ = mp_array<T>::get_and_fill(sz_, myPool_);
00169       }
00170 
00172       MemPoolStorage(const MemPoolStorage& x) : 
00173   val_(x.val_), sz_(x.sz_), len_(x.sz_), myPool_(x.myPool_) {
00174   dx_ = mp_array<T>::get_and_fill(x.dx_, sz_, myPool_);
00175       }
00176       
00178       ~MemPoolStorage() {
00179   if (len_ != 0)
00180     mp_array<T>::destroy_and_release(dx_, len_, myPool_);
00181       }
00182 
00184       MemPoolStorage& operator=(const MemPoolStorage& x) { 
00185   val_ = x.val_;
00186   if (sz_ != x.sz_) {
00187     sz_ = x.sz_;
00188     if (x.sz_ > len_) {
00189       if (len_ != 0)
00190         mp_array<T>::destroy_and_release(dx_, len_, myPool_);
00191       len_ = x.sz_;
00192       myPool_ = x.myPool_;
00193       dx_ = mp_array<T>::get_and_fill(x.dx_, sz_, myPool_);
00194     }
00195     else 
00196       mp_array<T>::copy(x.dx_, dx_, sz_);
00197   }
00198   else 
00199     mp_array<T>::copy(x.dx_, dx_, sz_);
00200 
00201   return *this; 
00202       } 
00203 
00205       int size() const { return sz_;}
00206 
00208       void resize(int sz) { 
00209   if (sz > len_) {
00210      if (len_ != 0)
00211       mp_array<T>::destroy_and_release(dx_, len_, myPool_);
00212     myPool_ = defaultPool_;
00213     dx_ = mp_array<T>::get_and_fill(sz, myPool_);
00214     len_ = sz;
00215   }
00216   sz_ = sz;
00217       }
00218 
00220       void zero() { 
00221   mp_array<T>::zero(dx_, sz_);
00222       }
00223 
00225       const T& val() const { return val_; }
00226 
00228       T& val() { return val_; }
00229 
00231       const T* dx() const { return dx_;}
00232 
00234       T dx(int i) const { return sz_ ? dx_[i] : T(0.); }
00235     
00237       T& fastAccessDx(int i) { return dx_[i];}
00238 
00240       const T& fastAccessDx(int i) const { return dx_[i];}
00241 
00242     private:
00243 
00245       T val_;
00246 
00248       int sz_;
00249 
00251       int len_;
00252 
00254       T* dx_;
00255 
00256     public:
00257 
00259       static MemPool* defaultPool_;
00260 
00261     protected:
00262 
00264       MemPool* myPool_;
00265 
00266     }; // class MemPoolStorage
00267 
00268   } // namespace Fad
00269 
00270 } // namespace Sacado
00271 
00272 #endif // SACADO_FAD_MEMPOOLSTORAGE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:32 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3