diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-23 16:50:12 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-23 16:50:12 +0200 |
| commit | 7e3e8672cdd19b19532de216e3df6fc3bae8ed7a (patch) | |
| tree | ef7b4b106dbb5597a7f418512bbf103d37ad6544 /libs/kstd | |
| parent | 6ea3b5617632e65f3d9fbb919172429b0e52c959 (diff) | |
| download | kernel-7e3e8672cdd19b19532de216e3df6fc3bae8ed7a.tar.xz kernel-7e3e8672cdd19b19532de216e3df6fc3bae8ed7a.zip | |
kstd: fix some shared_ptr bugs
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/kstd/bits/shared_ptr.hpp | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp index 0d5e0285..7241487f 100644 --- a/libs/kstd/kstd/bits/shared_ptr.hpp +++ b/libs/kstd/kstd/bits/shared_ptr.hpp @@ -126,18 +126,18 @@ namespace kstd explicit make_shared_control_block(Args &&... args) : shared_ptr_control_block_base{1, 1} { - std::construct_at(std::bit_cast<ObjectType *>(m_storage.data()), std::forward<Args>(args)...); + std::construct_at(reinterpret_cast<ObjectType *>(m_storage.data()), std::forward<Args>(args)...); } [[nodiscard]] auto get_pointer() noexcept -> ObjectType * { - return std::bit_cast<ObjectType *>(m_storage.data()); + return std::launder(reinterpret_cast<ObjectType *>(m_storage.data())); } protected: constexpr auto destroy_object() -> void override { - std::destroy_at(std::bit_cast<ObjectType *>(m_storage.data())); + std::destroy_at(get_pointer()); } private: @@ -229,7 +229,7 @@ namespace kstd //! Construct a weak pointer which shares ownership of the object managed by other. template<typename Y> requires bits::is_shared_pointer_ctor_compatible<Y *, T *>::value - constexpr weak_ptr(weak_ptr<Y> const & other) + constexpr weak_ptr(weak_ptr<Y> const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -242,7 +242,7 @@ namespace kstd //! Construct a weak pointer which shares ownership of the object managed by other. template<typename Y> requires bits::is_shared_pointer_ctor_compatible<Y *, T *>::value - constexpr weak_ptr(shared_ptr<Y> const & other) + constexpr weak_ptr(shared_ptr<Y> const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -418,7 +418,10 @@ namespace kstd enable_shared_from_this(enable_shared_from_this const &) {} - auto operator=(enable_shared_from_this const &) -> enable_shared_from_this & {} + auto operator=(enable_shared_from_this const &) -> enable_shared_from_this & + { + return *this; + } ~enable_shared_from_this() = default; @@ -542,7 +545,7 @@ namespace kstd * * @param other The shared_ptr to copy from. */ - shared_ptr(shared_ptr const & other) + shared_ptr(shared_ptr const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -560,7 +563,7 @@ namespace kstd */ template<typename U> requires(std::is_convertible_v<U *, T *>) - shared_ptr(shared_ptr<U> const & other) + shared_ptr(shared_ptr<U> const & other) noexcept : m_pointer(other.m_pointer) , m_control_block(other.m_control_block) { @@ -732,7 +735,9 @@ namespace kstd * * @return Returns the object owned by *this, equivalent to *get(). */ - [[nodiscard]] auto operator*() const -> T & + template<typename U = T> + requires(!std::is_void_v<std::remove_cv<U>>) + [[nodiscard]] auto operator*() const -> U & { return *m_pointer; } @@ -841,11 +846,10 @@ namespace kstd }; /** - * @brief Specializes the std::swap algorithm for stl::unique_ptr. Swaps the contents of lhs and rhs. Calls - * lhs.swap(rhs). + * Swap the contents of lhs and rhs. * * @tparam T Type of the managed object. - * @param lhs, rhs Smart pointers whose contents to swap. + * @param lhs, rhs Shared pointers whose contents to swap. */ template<typename T> auto swap(shared_ptr<T> & lhs, shared_ptr<T> & rhs) -> void |
