Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57420 - sandbox/stm/branches/vbe/boost/stm/non_tx/detail
From: vicente.botet_at_[hidden]
Date: 2009-11-05 16:18:02


Author: viboes
Date: 2009-11-05 16:18:02 EST (Thu, 05 Nov 2009)
New Revision: 57420
URL: http://svn.boost.org/trac/boost/changeset/57420

Log:
TBoost.STM vbe: Improved cache_map exception safety and address reuse by a different type.
Removal of test_ from test names

Text files modified:
   sandbox/stm/branches/vbe/boost/stm/non_tx/detail/cache_map.hpp | 51 +++++++++++++++++++++++++++++----------
   1 files changed, 38 insertions(+), 13 deletions(-)

Modified: sandbox/stm/branches/vbe/boost/stm/non_tx/detail/cache_map.hpp
==============================================================================
--- sandbox/stm/branches/vbe/boost/stm/non_tx/detail/cache_map.hpp (original)
+++ sandbox/stm/branches/vbe/boost/stm/non_tx/detail/cache_map.hpp 2009-11-05 16:18:02 EST (Thu, 05 Nov 2009)
@@ -16,6 +16,7 @@
 
 //-----------------------------------------------------------------------------
 #include <map>
+#include <memory>
 //-----------------------------------------------------------------------------
 #include <boost/synchro.hpp>
 //-----------------------------------------------------------------------------
@@ -60,7 +61,7 @@
         : base_type()
         , value_(ptr), ptr_(0) {}
 
- inline ~cache() {
+ ~cache() {
         delete ptr_;
     }
 
@@ -106,6 +107,7 @@
         delete this;
     }
 #endif
+
     virtual void copy_state(base_transaction_object const * const rhs) {
         if (value_==0) return;
         *value_= *(static_cast<cache<T> const * const>(rhs)->ptr_);
@@ -130,7 +132,7 @@
       base_memory_manager::return_mem(mem, sizeof(cache<T>));
    }
 #endif
-
+
 private:
     //cache(cache<T> const & r);
 
@@ -158,27 +160,50 @@
     static cache<T>* get(T* ptr) {
         synchro::lock_guard<Mutex> lk(mtx_);
         map_type::iterator it = map_.find(ptr);
- cache<T>* res=0;
+ std::auto_ptr<cache<T> > res;
         if (it == map_.end()) {
- res= new cache<T>(ptr);
- map_.insert(std::make_pair(ptr, res));
+ // When the cache do not exists yet create a new one
+ res.reset(new cache<T>(ptr));
+ map_.insert(std::make_pair(ptr, res.get()));
         } else {
- res=static_cast<cache<T>*>(it->second);
+ if (typeid(*it->second)==typeid(cache<T>)) {
+ res.reset(static_cast<cache<T>*>(it->second));
+ } else {
+ // When cached value do not corresponds to the type we need
+ // remove the cached one and create a new one.
+ base_transaction_object* tmp= it->second;
+ map_.erase(it);
+ delete tmp;
+ res.reset(new cache<T>(ptr));
+ map_.insert(std::make_pair(ptr, res.get()));
+ }
+
         }
- return res;
+ return res.release();
     }
     template <typename T>
- static cache<T>* get(T const* ptr) {
+ static cache<T> * get(T const* ptr) {
         synchro::lock_guard<Mutex> lk(mtx_);
         map_type::iterator it = map_.find(const_cast<T*>(ptr));
- cache<T>* res=0;
+ std::auto_ptr<cache<T> > res;
         if (it == map_.end()) {
- res= new cache<T>(const_cast<T*>(ptr));
- map_.insert(std::make_pair(const_cast<T*>(ptr), res));
+ // When the cache do not exists yet create a new one
+ res.reset(new cache<T>(const_cast<T*>(ptr)));
+ map_.insert(std::make_pair(const_cast<T*>(ptr), res.get()));
         } else {
- res=static_cast<cache<T>*>(it->second);
+ if (typeid(it->second)==typeid(cache<T>*)) {
+ res.reset(static_cast<cache<T>*>(it->second));
+ } else {
+ // When cached value do not corresponds to the type we need
+ // remove the cached one and create a new one.
+ base_transaction_object* tmp= it->second;
+ map_.erase(it);
+ delete tmp;
+ res.reset(new cache<T>(const_cast<T*>(ptr)));
+ map_.insert(std::make_pair(const_cast<T*>(ptr), res.get()));
+ }
         }
- return res;
+ return res.release();
     }
 };
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk