diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-03-29 15:59:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 15:59:29 +0200 |
commit | 55a68fe347b0b551cd3421d014fabcf3be4f525d (patch) | |
tree | e8a015d2755d5fb666ab593941373090a9481809 /vespalib | |
parent | d826275fe2b951ae5cdf26bb5e295d17e2fbaf70 (diff) | |
parent | da37d75e5dad5d7f819329bac3fa17f9bbe370f8 (diff) |
Merge pull request #21868 from vespa-engine/balder/avoid-fieldpathupdate-cp
CloneablePtr -> std::unique_ptr
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/memory/memory_test.cpp | 31 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/memory.h | 146 |
2 files changed, 11 insertions, 166 deletions
diff --git a/vespalib/src/tests/memory/memory_test.cpp b/vespalib/src/tests/memory/memory_test.cpp index d39fbd672ea..4d7c0b1c4d4 100644 --- a/vespalib/src/tests/memory/memory_test.cpp +++ b/vespalib/src/tests/memory/memory_test.cpp @@ -25,50 +25,25 @@ public: int Main() override; }; -B* fn(auto_arr<B> param) { return param.get(); } -auto_arr<B> fn(B *param) { auto_arr<B> bb(param); return bb; } - int Test::Main() { TEST_INIT("memory_test"); { - B* p = new B[5]; - auto_arr<B> apb(p); - EXPECT_TRUE(apb.get() == p); - EXPECT_TRUE(fn(apb) == p); - EXPECT_TRUE(apb.get() == nullptr); - } - { - A* p = new A[5]; - auto_arr<A> apa(p); - EXPECT_TRUE(apa.get() == p); - auto_arr<A> apb = apa; - EXPECT_TRUE(apa.get() == nullptr); - EXPECT_TRUE(apb.get() == p); - A& ref = apb[2]; - EXPECT_TRUE(&ref == (p+2)); - } - { - B* p = new B[5]; - auto_arr<B> apb = fn(p); - EXPECT_TRUE(apb.get() == p); - } - { MallocAutoPtr a(malloc(30)); EXPECT_TRUE(a.get() != nullptr); void * tmp = a.get(); - MallocAutoPtr b(a); + MallocAutoPtr b(std::move(a)); EXPECT_TRUE(tmp == b.get()); EXPECT_TRUE(a.get() == nullptr); MallocAutoPtr c; - c = b; + c = std::move(b); EXPECT_TRUE(b.get() == nullptr); EXPECT_TRUE(tmp == c.get()); MallocAutoPtr d(malloc(30)); EXPECT_TRUE(d.get() != nullptr); tmp = c.get(); - d = c; + d = std::move(c); EXPECT_TRUE(tmp == d.get()); EXPECT_TRUE(c.get() == nullptr); } diff --git a/vespalib/src/vespa/vespalib/util/memory.h b/vespalib/src/vespa/vespalib/util/memory.h index 0391a7730f5..da164eba0a6 100644 --- a/vespalib/src/vespa/vespalib/util/memory.h +++ b/vespalib/src/vespa/vespalib/util/memory.h @@ -12,110 +12,6 @@ namespace vespalib { /** - * @brief Helper class - * - * Helper to enable auto_arr instances as parameters and return values. - * You should not use this class directly. - **/ -template<class OtherArray> struct auto_arr_ref { - OtherArray* _array; - auto_arr_ref(OtherArray* a) : _array(a) {} -}; - -/** - * @brief std::unique_ptr for arrays - * - * This class behaves just like unique_ptr, but wraps a pointer allocated - * with new[]; so it will call delete[] when doing cleanup. - */ -template <class Array> class auto_arr { -private: - Array* _array; // actual owned array (if any) - -public: - /** - * @brief constructor from pointer - * - * Note: the pointer must have been allocated with new[] - **/ - explicit auto_arr(Array* a = 0) throw() : _array(a) {} - - /** - * @brief "copy" contructor - * - * Note: non-const parameter; transfers ownership - * instead of copying. - **/ - auto_arr(auto_arr& a) throw() : _array(a.release()) {} - - /** - * @brief assignment operator - * - * Note: non-const parameter; transfers ownership - * instead of copying. - **/ - auto_arr& operator=(auto_arr& a) throw() { - reset(a.release()); - return *this; - } - - /** @brief destructor, calls delete[] on owned pointer */ - ~auto_arr() throw() { delete[] _array; } - - /** @brief value access */ - Array& operator [] (size_t i) const throw() { return _array[i]; } - - /** @brief access underlying array */ - Array* get() const throw() { return _array; } - - /** - * @brief release ownership - * - * The caller of release() must take responsibility for eventually calling delete[]. - * @return previously owned pointer - **/ - Array* release() throw() { - Array* tmp = _array; - _array = 0; - return tmp; - } - - /** - * @brief reset value - * - * Behaves like destruct then construct. - **/ - void reset(Array* a = 0) throw() { - delete[] _array; - _array = a; - } - - /** - * @brief special implicit conversion from auxiliary type - * to enable parameter / return value passing - **/ - auto_arr(auto_arr_ref<Array> ref) throw() - : _array(ref._array) {} - - /** - * @brief special assignment from auxiliary type - * to enable parameter / return value passing - **/ - auto_arr& operator=(auto_arr_ref<Array> ref) throw() { - reset(ref._array); - return *this; - } - - /** - * @brief special implicit conversion to auxiliary type - * to enable parameter / return value passing - **/ - operator auto_arr_ref<Array>() throw() { - return auto_arr_ref<Array>(this->release()); - } -}; - -/** * @brief Keep ownership of memory allocated via malloc() * * A MallocAutoPtr does for c type alloced objects as std::unique_ptr does @@ -130,51 +26,25 @@ public: * * Note: the pointer must have been allocated with malloc() **/ - MallocAutoPtr(void *p=nullptr) : _p(p) { } + MallocAutoPtr(void *p=nullptr) noexcept : _p(p) { } /** @brief destructor, calls free() on owned pointer */ ~MallocAutoPtr() { cleanup(); } - MallocAutoPtr(MallocAutoPtr && rhs) : _p(rhs._p) { rhs._p = nullptr; } - MallocAutoPtr & operator = (MallocAutoPtr && rhs) { + MallocAutoPtr(MallocAutoPtr && rhs) noexcept : _p(rhs._p) { rhs._p = nullptr; } + MallocAutoPtr & operator = (MallocAutoPtr && rhs) noexcept { cleanup(); std::swap(_p, rhs._p); return *this; } - /** - * @brief "copy" contructor - * - * Note: non-const parameter; transfers ownership - * instead of copying. - **/ - MallocAutoPtr(const MallocAutoPtr & rhs) - : _p(rhs._p) { const_cast<MallocAutoPtr &>(rhs)._p = nullptr; } + MallocAutoPtr(const MallocAutoPtr & rhs) = delete; + MallocAutoPtr & operator = (const MallocAutoPtr & rhs) = delete; - /** - * @brief assignment operator - * - * Note: non-const parameter; transfers ownership - * instead of copying. - **/ - MallocAutoPtr & operator = (const MallocAutoPtr & rhs) { - if (this != &rhs) { - MallocAutoPtr tmp(rhs); - swap(tmp); - } - return *this; - } - - /** @brief swap contents */ - void swap(MallocAutoPtr & rhs) { std::swap(_p, rhs._p); } - - /** @brief value access */ - const void * get() const { return _p; } - - /** @brief value access */ - void * get() { return _p; } + const void * get() const noexcept { return _p; } + void * get() noexcept { return _p; } private: - void cleanup() { + void cleanup() noexcept { if (_p) { free(_p); _p = nullptr; |