From 524681f3b8b268090386f277f688d3e87a58961f Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 17 Jul 2017 17:17:01 -0600 Subject: [PATCH] Simplify the VectorMemory::Pointer class. Specifically, it looks a lot like std::unique_ptr except that it does something different during destruction. This can easily be modeled using a custom deleter with std::unique_ptr and avoids a couple of dozen lines of code. --- include/deal.II/algorithms/newton.templates.h | 14 +-- .../algorithms/theta_timestepping.templates.h | 4 +- include/deal.II/lac/vector_memory.h | 91 ++++--------------- 3 files changed, 29 insertions(+), 80 deletions(-) diff --git a/include/deal.II/algorithms/newton.templates.h b/include/deal.II/algorithms/newton.templates.h index 88e00e10b4..c3bf3c08d2 100644 --- a/include/deal.II/algorithms/newton.templates.h +++ b/include/deal.II/algorithms/newton.templates.h @@ -117,12 +117,12 @@ namespace Algorithms AnyData src2; src1.add(&u, "Newton iterate"); src1.merge(in); - src2.add(res, "Newton residual"); + src2.add(res.get(), "Newton residual"); src2.merge(src1); AnyData out1; - out1.add(res, "Residual"); + out1.add(res.get(), "Residual"); AnyData out2; - out2.add(Du, "Update"); + out2.add(Du.get(), "Update"); unsigned int step = 0; // fill res with (f(u), v) @@ -135,9 +135,9 @@ namespace Algorithms AnyData tmp; VectorType *p = &u; tmp.add(p, "solution"); - p = Du; + p = Du.get(); tmp.add(p, "update"); - p = res; + p = res.get(); tmp.add(p, "residual"); *data_out << step; *data_out << tmp; @@ -166,9 +166,9 @@ namespace Algorithms AnyData tmp; VectorType *p = &u; tmp.add(p, "solution"); - p = Du; + p = Du.get(); tmp.add(p, "update"); - p = res; + p = res.get(); tmp.add(p, "residual"); *data_out << step; *data_out << tmp; diff --git a/include/deal.II/algorithms/theta_timestepping.templates.h b/include/deal.II/algorithms/theta_timestepping.templates.h index 12b41ea89e..d332393b85 100644 --- a/include/deal.II/algorithms/theta_timestepping.templates.h +++ b/include/deal.II/algorithms/theta_timestepping.templates.h @@ -105,9 +105,9 @@ namespace Algorithms AnyData src2; AnyData out1; - out1.add(aux, "Solution"); + out1.add(aux.get(), "Solution"); // The data provided to the inner solver - src2.add(aux, "Previous time"); + src2.add(aux.get(), "Previous time"); src2.add(&solution, "Previous iterate"); src2.add(&d_implicit.time, "Time"); src2.add(&d_implicit.step, "Timestep"); diff --git a/include/deal.II/lac/vector_memory.h b/include/deal.II/lac/vector_memory.h index a382cf607b..405aeb686e 100644 --- a/include/deal.II/lac/vector_memory.h +++ b/include/deal.II/lac/vector_memory.h @@ -25,6 +25,7 @@ #include #include +#include DEAL_II_NAMESPACE_OPEN @@ -105,12 +106,13 @@ public: "vector has not actually been allocated by the same pool before."); //@} + /** * A class that looks like a pointer for all practical purposes and that * upon construction time allocates a vector from a VectorMemory object - * (or an object of a derived class) that is passed to the constructor. - * The destructor then automatically returns the vector's ownership to - * the same VectorMemory object. + * (or an object of a class derived from VectorMemory) that is passed + * to the constructor of this class. The destructor then automatically + * returns the vector's ownership to the same VectorMemory object. * * Pointers of this type are therefore safe in the sense that they automatically * call VectorMemory::free() when they are destroyed, whether that happens @@ -118,45 +120,27 @@ public: * exception unwinding. These kinds of object thus relieve the user from * using vector management functions explicitly. * + * In many senses, this class acts like std::unique_ptr in that + * it is the unique owner of a chunk of memory that it frees upon destruction. + * The main differences to std::unique_ptr are (i) that it + * allocates memory from a memory pool upon construction, and (ii) that the + * memory is not destroyed using `operator delete` but returned to the + * VectorMemory pool. + * * @author Guido Kanschat, 2009 */ - class Pointer + class Pointer : public std::unique_ptr > { public: /** * Constructor, automatically allocating a vector from @p mem. */ Pointer(VectorMemory &mem); - /** - * Destructor, automatically releasing the vector from the memory #pool. - */ - ~Pointer(); - - /** - * Conversion to regular pointer. - */ - operator VectorType *() const; - - /** - * Dereferencing operator. - */ - VectorType &operator * () const; /** - * Dereferencing operator. - */ - VectorType *operator -> () const; - - private: - /** - * The memory pool used. - */ - SmartPointer,Pointer> pool; - - /** - * The pointer to the vector. + * Destructor, automatically releasing the vector from the memory #pool. */ - VectorType *v; + ~Pointer() = default; }; }; @@ -368,47 +352,12 @@ template inline VectorMemory::Pointer::Pointer(VectorMemory &mem) : - pool(&mem, typeid(*this).name()), v(nullptr) + std::unique_ptr > + (mem.alloc(), [&mem](VectorType *v) { - v = pool->alloc(); -} - - -template -inline -VectorMemory::Pointer::~Pointer() -{ - try - { - pool->free(v); - } - catch (...) - {} -} - - -template -inline -VectorMemory::Pointer::operator VectorType *() const -{ - return v; -} - - -template -inline -VectorType &VectorMemory::Pointer::operator * () const -{ - return *v; -} - - -template -inline -VectorType *VectorMemory::Pointer::operator -> () const -{ - return v; -} + mem.free(v); +}) +{} #endif // DOXYGEN -- 2.39.5