00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00223 public:
00224
00226 T val_;
00227
00229 int sz_;
00230
00232 int len_;
00233
00235 T* dx_;
00236
00238 static MemPool* defaultPool_;
00239
00240 protected:
00241
00243 MemPool* myPool_;
00244
00245 };
00246
00247 }
00248
00249 }
00250
00251 #endif // SACADO_FAD_MEMPOOLSTORAGE_HPP