From 9f211d6de8711e4427d0b446582fb63d6a15e2f9 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 10:40:33 +0200 Subject: [PATCH 01/16] shared ptr impl --- homework/shared_ptr.hpp | 104 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 homework/shared_ptr.hpp diff --git a/homework/shared_ptr.hpp b/homework/shared_ptr.hpp new file mode 100644 index 00000000..e2f70b19 --- /dev/null +++ b/homework/shared_ptr.hpp @@ -0,0 +1,104 @@ +#ifndef SHARED_PRT_H_ +#define SHARED_PRT_H_ + +#include +#include + +namespace my { + +template +struct ControlBlock { + std::atomic shared_refs; + std::atomic weak_refs; + std::function deleter; +}; + +template +auto default_deleter = [](Type1* p) { delete p; }; + +template +class shared_ptr { +private: + Type* ptr_; + ControlBlock* ctrl_block_; + +public: + shared_ptr(Type* ptr, std::function d = default_deleter) + : ptr_(ptr), ctrl_block_(new ControlBlock{0, 0, d}) { + // ctrl_block_ = new ControlBlock{0,0,nullptr}; + ctrl_block_->shared_refs++; + } + + shared_ptr(const shared_ptr& other) + : ptr_(other.ptr_), ctrl_block_(other.ctrl_block_) { + ctrl_block_->shared_refs++; + } + + shared_ptr(shared_ptr&& other) + : ptr_(other.ptr_), ctrl_block_(other.ctrl_block_) { + other.ptr_ = nullptr; + other.ctrl_block_ = nullptr; + } + + ~shared_ptr() { + if (ctrl_block_) { + ctrl_block_->shared_refs--; + if (ctrl_block_->shared_refs == 0) { + ctrl_block_->deleter(ptr_); + + if (ctrl_block_->weak_refs == 0) { + delete ctrl_block_; + } + } + } + } + + Type* operator->() { + return ptr_; + } + + Type& operator*() { + return *ptr_; + } + + explicit operator bool() const { + return (ptr_ != nullptr); + } + + size_t use_count() { + return ctrl_block_->shared_refs; + } + + Type* get() { + return ptr_; + } + + void reset(Type* ptr, std::function d = default_deleter) + { + if(ptr_ && ctrl_block_->shared_refs > 0) + { + ctrl_block_->deleter(ptr_); + ptr_ = ptr; + ctrl_block_->deleter = d; + } + else if(!ptr_) + { + if(ctrl_block_) + { + ptr_ = ptr; + ctrl_block_->shared_refs++; + } + else + { + ptr_ = ptr; + ctrl_block_ = new ControlBlock{0, 0, d}; + ctrl_block_->shared_refs++; + } + + } + + } +}; + +} // namespace my +#endif /* SHARED_PRT_H_*/ From b31d0bab6e35d512310e674d3f379ad968216ad7 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 10:46:14 +0200 Subject: [PATCH 02/16] change location --- homework/{ => shared_ptr}/shared_ptr.hpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename homework/{ => shared_ptr}/shared_ptr.hpp (100%) diff --git a/homework/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp similarity index 100% rename from homework/shared_ptr.hpp rename to homework/shared_ptr/shared_ptr.hpp From 5324bc46a0ca8c6081915258ed710d4f3b282744 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 10:53:46 +0200 Subject: [PATCH 03/16] add test file --- homework/shared_ptr/shared_ptr_tests.cpp | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 homework/shared_ptr/shared_ptr_tests.cpp diff --git a/homework/shared_ptr/shared_ptr_tests.cpp b/homework/shared_ptr/shared_ptr_tests.cpp new file mode 100644 index 00000000..aa28650a --- /dev/null +++ b/homework/shared_ptr/shared_ptr_tests.cpp @@ -0,0 +1,3 @@ +#include "shared_ptr.hpp" + +template class my::shared_ptr; \ No newline at end of file From ca0ad80181707d251323b944818ca01bb2a63651 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 11:11:24 +0200 Subject: [PATCH 04/16] Add 1st test --- homework/shared_ptr/shared_ptr_tests.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/homework/shared_ptr/shared_ptr_tests.cpp b/homework/shared_ptr/shared_ptr_tests.cpp index aa28650a..7b68cad6 100644 --- a/homework/shared_ptr/shared_ptr_tests.cpp +++ b/homework/shared_ptr/shared_ptr_tests.cpp @@ -1,3 +1,12 @@ +#include #include "shared_ptr.hpp" -template class my::shared_ptr; \ No newline at end of file +template class my::shared_ptr; + + +TEST(SharedPtr, shouldCreateSharedPtrWithInitialValue) +{ + my::shared_ptr p {new int(5)}; + ASSERT_EQ(*p, 5); + +} \ No newline at end of file From bd37041397a949c5f0c16e25d64bac22801f2810 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 11:37:59 +0200 Subject: [PATCH 05/16] fix --- homework/shared_ptr/shared_ptr.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index e2f70b19..0ba5eb27 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -73,7 +73,7 @@ class shared_ptr { return ptr_; } - void reset(Type* ptr, std::function d = default_deleter) + void reset(Type* ptr = nullptr, std::function d = default_deleter) { if(ptr_ && ctrl_block_->shared_refs > 0) { @@ -102,3 +102,4 @@ class shared_ptr { } // namespace my #endif /* SHARED_PRT_H_*/ + From f9c339a5cd07139f7d9fffc7b8c0d8e19553ec2f Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 12:15:50 +0200 Subject: [PATCH 06/16] fix --- homework/shared_ptr/shared_ptr.hpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index 0ba5eb27..592479d1 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -53,6 +53,20 @@ class shared_ptr { } } + shared_ptr& operator=(const shared_ptr& other) + { + ptr_ = other.ptr_; + ctrl_block_ = other.ctrl_block_; + } + + shared_ptr& operator=(const shared_ptr&& other) + { + ptr_ = other.ptr_; + ctrl_block_ = other.ctrl_block_; + other.ptr_ = nullptr; + other.ctrl_block_ = nullptr; + } + Type* operator->() { return ptr_; } From 95346060f9775b42b87e78aa1aead837b65e82fc Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 12:18:34 +0200 Subject: [PATCH 07/16] fix --- homework/shared_ptr/shared_ptr.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index 592479d1..3930342b 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -23,10 +23,8 @@ class shared_ptr { ControlBlock* ctrl_block_; public: - shared_ptr(Type* ptr, std::function d = default_deleter) - : ptr_(ptr), ctrl_block_(new ControlBlock{0, 0, d}) { - // ctrl_block_ = new ControlBlock{0,0,nullptr}; - ctrl_block_->shared_refs++; + shared_ptr(Type* ptr = nullptr, std::function d = default_deleter) + : ptr_(ptr), ctrl_block_(new ControlBlock{1, 0, d}) { } shared_ptr(const shared_ptr& other) From 93d9fe3bf723a32cee9d4da9dc8843af80605181 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 12:20:38 +0200 Subject: [PATCH 08/16] fix --- homework/shared_ptr/shared_ptr.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index 3930342b..ea3bda3d 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -55,6 +55,7 @@ class shared_ptr { { ptr_ = other.ptr_; ctrl_block_ = other.ctrl_block_; + return *this; } shared_ptr& operator=(const shared_ptr&& other) @@ -63,6 +64,7 @@ class shared_ptr { ctrl_block_ = other.ctrl_block_; other.ptr_ = nullptr; other.ctrl_block_ = nullptr; + return *this; } Type* operator->() { From faadfe5b2e78a10c8f32a4b70b9834b23c242eae Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 12:22:01 +0200 Subject: [PATCH 09/16] fix --- homework/shared_ptr/shared_ptr.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index ea3bda3d..20ebfff0 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -58,7 +58,7 @@ class shared_ptr { return *this; } - shared_ptr& operator=(const shared_ptr&& other) + shared_ptr& operator=(shared_ptr&& other) { ptr_ = other.ptr_; ctrl_block_ = other.ctrl_block_; From 59cae8a92f83079da54b36abf340d1115f067117 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 13:45:02 +0200 Subject: [PATCH 10/16] Fix --- homework/shared_ptr/shared_ptr.hpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index 20ebfff0..d81b56ec 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -51,10 +51,23 @@ class shared_ptr { } } - shared_ptr& operator=(const shared_ptr& other) + shared_ptr& operator=(shared_ptr& other) { + if(ptr_ != other.ptr_) + { + if(ctrl_block_->shared_refs == 1u) + { + ctrl_block_->deleter(ptr_); + delete ctrl_block_; + } + else + { + ctrl_block_->shared_refs--; + } + } ptr_ = other.ptr_; ctrl_block_ = other.ctrl_block_; + ctrl_block_->shared_refs++; return *this; } From 0d3dcc33d55efa1f24b191787ac6e6e22b24fcd3 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 13:47:29 +0200 Subject: [PATCH 11/16] Fix formatting --- homework/shared_ptr/shared_ptr.hpp | 36 +++++++----------------- homework/shared_ptr/shared_ptr_tests.cpp | 7 ++--- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index d81b56ec..ee4c7bee 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -51,17 +51,12 @@ class shared_ptr { } } - shared_ptr& operator=(shared_ptr& other) - { - if(ptr_ != other.ptr_) - { - if(ctrl_block_->shared_refs == 1u) - { + shared_ptr& operator=(shared_ptr& other) { + if (ptr_ != other.ptr_) { + if (ctrl_block_->shared_refs == 1u) { ctrl_block_->deleter(ptr_); delete ctrl_block_; - } - else - { + } else { ctrl_block_->shared_refs--; } } @@ -71,8 +66,7 @@ class shared_ptr { return *this; } - shared_ptr& operator=(shared_ptr&& other) - { + shared_ptr& operator=(shared_ptr&& other) { ptr_ = other.ptr_; ctrl_block_ = other.ctrl_block_; other.ptr_ = nullptr; @@ -100,33 +94,23 @@ class shared_ptr { return ptr_; } - void reset(Type* ptr = nullptr, std::function d = default_deleter) - { - if(ptr_ && ctrl_block_->shared_refs > 0) - { + void reset(Type* ptr = nullptr, std::function d = default_deleter) { + if (ptr_ && ctrl_block_->shared_refs > 0) { ctrl_block_->deleter(ptr_); ptr_ = ptr; ctrl_block_->deleter = d; - } - else if(!ptr_) - { - if(ctrl_block_) - { + } else if (!ptr_) { + if (ctrl_block_) { ptr_ = ptr; ctrl_block_->shared_refs++; - } - else - { + } else { ptr_ = ptr; ctrl_block_ = new ControlBlock{0, 0, d}; ctrl_block_->shared_refs++; } - } - } }; } // namespace my #endif /* SHARED_PRT_H_*/ - diff --git a/homework/shared_ptr/shared_ptr_tests.cpp b/homework/shared_ptr/shared_ptr_tests.cpp index 7b68cad6..e8a415b5 100644 --- a/homework/shared_ptr/shared_ptr_tests.cpp +++ b/homework/shared_ptr/shared_ptr_tests.cpp @@ -3,10 +3,7 @@ template class my::shared_ptr; - -TEST(SharedPtr, shouldCreateSharedPtrWithInitialValue) -{ - my::shared_ptr p {new int(5)}; +TEST(SharedPtr, shouldCreateSharedPtrWithInitialValue) { + my::shared_ptr p{new int(5)}; ASSERT_EQ(*p, 5); - } \ No newline at end of file From 3d747cc55c1d1c182f6e0dadaa46b6fe2fd52a38 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 14:00:09 +0200 Subject: [PATCH 12/16] Fix formatting --- homework/shared_ptr/shared_ptr.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index ee4c7bee..a361cb8e 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -95,19 +95,16 @@ class shared_ptr { } void reset(Type* ptr = nullptr, std::function d = default_deleter) { - if (ptr_ && ctrl_block_->shared_refs > 0) { + if (ptr_ && ctrl_block_->shared_refs == 1) { ctrl_block_->deleter(ptr_); ptr_ = ptr; ctrl_block_->deleter = d; } else if (!ptr_) { if (ctrl_block_) { ptr_ = ptr; - ctrl_block_->shared_refs++; - } else { - ptr_ = ptr; - ctrl_block_ = new ControlBlock{0, 0, d}; - ctrl_block_->shared_refs++; + ctrl_block_->shared_refs = 1; } + } else { } } }; From d8235180bd8debe68c9aae863e1708a15664c10e Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Wed, 30 Apr 2025 14:38:41 +0200 Subject: [PATCH 13/16] Fix --- homework/shared_ptr/shared_ptr.hpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/homework/shared_ptr/shared_ptr.hpp b/homework/shared_ptr/shared_ptr.hpp index a361cb8e..c74f9bd0 100644 --- a/homework/shared_ptr/shared_ptr.hpp +++ b/homework/shared_ptr/shared_ptr.hpp @@ -67,6 +67,14 @@ class shared_ptr { } shared_ptr& operator=(shared_ptr&& other) { + if (ptr_ != other.ptr_) { + if (ctrl_block_->shared_refs == 1u) { + ctrl_block_->deleter(ptr_); + delete ctrl_block_; + } else { + ctrl_block_->shared_refs--; + } + } ptr_ = other.ptr_; ctrl_block_ = other.ctrl_block_; other.ptr_ = nullptr; @@ -103,6 +111,9 @@ class shared_ptr { if (ctrl_block_) { ptr_ = ptr; ctrl_block_->shared_refs = 1; + } else { + ctrl_block_ = new ControlBlock{1, 0, d}; + ptr_ = ptr; } } else { } From 5423f03dc542896b04b6700ce11a14013379d756 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Fri, 2 May 2025 13:39:39 +0200 Subject: [PATCH 14/16] Add tests --- homework/shared_ptr/shared_ptr_tests.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/homework/shared_ptr/shared_ptr_tests.cpp b/homework/shared_ptr/shared_ptr_tests.cpp index e8a415b5..e7c7e751 100644 --- a/homework/shared_ptr/shared_ptr_tests.cpp +++ b/homework/shared_ptr/shared_ptr_tests.cpp @@ -6,4 +6,26 @@ template class my::shared_ptr; TEST(SharedPtr, shouldCreateSharedPtrWithInitialValue) { my::shared_ptr p{new int(5)}; ASSERT_EQ(*p, 5); +} + +TEST(SharedPtr, shouldCreateWithCopyConstructor) { + my::shared_ptr p {new int(5)}; + my::shared_ptr p2 (p); + + ASSERT_EQ(p.get(), p2.get()); + ASSERT_EQ(p.operator->(), p2.get()); + ASSERT_EQ(p.operator->(), p2.operator->()); + ASSERT_EQ(p.use_count(), 2); +} + +TEST(SharedPtr, shouldCreateWithMoveConstructor) { + my::shared_ptr p {new int(5)}; + my::shared_ptr p2 (std::move(p)); + + ASSERT_EQ(p.get(), nullptr); + ASSERT_EQ(p2.use_count(), 1); + p2.reset(); + ASSERT_EQ(p2.get(), nullptr); + p2.reset(new int(6)); + ASSERT_EQ(*p2, 5); } \ No newline at end of file From 857dfdc98de9110256c1188662ed9c3e59db11e0 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Fri, 2 May 2025 13:43:38 +0200 Subject: [PATCH 15/16] Add tests --- homework/shared_ptr/shared_ptr_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/shared_ptr/shared_ptr_tests.cpp b/homework/shared_ptr/shared_ptr_tests.cpp index e7c7e751..1ca2e848 100644 --- a/homework/shared_ptr/shared_ptr_tests.cpp +++ b/homework/shared_ptr/shared_ptr_tests.cpp @@ -27,5 +27,5 @@ TEST(SharedPtr, shouldCreateWithMoveConstructor) { p2.reset(); ASSERT_EQ(p2.get(), nullptr); p2.reset(new int(6)); - ASSERT_EQ(*p2, 5); + ASSERT_EQ(*p2, 6); } \ No newline at end of file From 2213e1545ae1f5169e714ef93db0bd55536f3091 Mon Sep 17 00:00:00 2001 From: adrianow795 Date: Fri, 2 May 2025 13:44:25 +0200 Subject: [PATCH 16/16] Update formatting' --- homework/shared_ptr/shared_ptr_tests.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homework/shared_ptr/shared_ptr_tests.cpp b/homework/shared_ptr/shared_ptr_tests.cpp index 1ca2e848..5e97ba16 100644 --- a/homework/shared_ptr/shared_ptr_tests.cpp +++ b/homework/shared_ptr/shared_ptr_tests.cpp @@ -9,8 +9,8 @@ TEST(SharedPtr, shouldCreateSharedPtrWithInitialValue) { } TEST(SharedPtr, shouldCreateWithCopyConstructor) { - my::shared_ptr p {new int(5)}; - my::shared_ptr p2 (p); + my::shared_ptr p{new int(5)}; + my::shared_ptr p2(p); ASSERT_EQ(p.get(), p2.get()); ASSERT_EQ(p.operator->(), p2.get()); @@ -19,8 +19,8 @@ TEST(SharedPtr, shouldCreateWithCopyConstructor) { } TEST(SharedPtr, shouldCreateWithMoveConstructor) { - my::shared_ptr p {new int(5)}; - my::shared_ptr p2 (std::move(p)); + my::shared_ptr p{new int(5)}; + my::shared_ptr p2(std::move(p)); ASSERT_EQ(p.get(), nullptr); ASSERT_EQ(p2.use_count(), 1);