diff --git a/include/gp/utils/pointers.hpp b/include/gp/utils/pointers.hpp index a2ae00c..9e77c65 100644 --- a/include/gp/utils/pointers.hpp +++ b/include/gp/utils/pointers.hpp @@ -4,6 +4,7 @@ #include "gp/algorithms/move.hpp" #include "gp/containers/buffer.hpp" #include "gp/functional/function.hpp" +#include "gp/utils/allocators/dummy.hpp" #include #include @@ -78,7 +79,7 @@ namespace gp { class shared_ptr { T* data; std::atomic_int* refcounter; - gp::allocator& owner; + gp::optional> owner; shared_ptr(T* _data, gp::allocator& _owner) : data(_data) @@ -101,8 +102,8 @@ namespace gp { if(refcounter->fetch_sub(1, std::memory_order::acq_rel) == 0) { if(data) { data->~T(); - owner.deallocate(refcounter); - owner.deallocate(data); + owner.value().get().deallocate(refcounter); + owner.value().get().deallocate(data); } } } @@ -130,6 +131,12 @@ namespace gp { return data != nullptr; } + shared_ptr() + : data{nullptr} + , owner{gp::nullopt} + , refcounter(nullptr) + {} + shared_ptr(shared_ptr& oth) { oth.refcounter->fetch_add(1, std::memory_order::acquire); @@ -152,13 +159,18 @@ namespace gp { data = oth.data; refcounter = oth.refcounter; - owner = oth.owner; + owner = oth.owner; + return *this; } shared_ptr& operator=(shared_ptr&& oth) { dirty_clear(); + (*oth.refcounter)++; + data = oth.data; + refcounter = oth.refcounter; owner = oth.owner; + return *this; } ~shared_ptr() {