16#include "pw_allocator/config.h"
19#if PW_ALLOCATOR_HAS_ATOMICS
25#include "pw_allocator/deallocator.h"
26#include "pw_allocator/internal/control_block.h"
27#include "pw_allocator/internal/managed_ptr.h"
28#include "pw_allocator/layout.h"
59class SharedPtr final : public ::pw::allocator::internal::ManagedPtr<T> {
61 using Base = ::pw::allocator::internal::ManagedPtr<T>;
62 using ControlBlock = ::pw::allocator::internal::ControlBlock;
65 using element_type =
typename Base::element_type;
102 template <
typename U>
104 *
this = std::move(other);
111 operator= <T>(other);
121 template <
typename U>
130 template <
typename U>
137 SharedPtr& operator=(std::nullptr_t)
noexcept;
143 static_assert(std::is_array_v<T>,
144 "size() cannot be called on non-array types");
145 return control_block_ ==
nullptr ? 0 : control_block_->size();
151 return control_block_ ==
nullptr ? 0 : control_block_->num_shared();
159 void reset() noexcept;
166 template <typename PtrType>
167 bool owner_before(const PtrType& other) const noexcept {
168 return control_block_ < other.control_block();
174 static constexpr bool is_unbounded_array_v =
175 allocator::internal::is_unbounded_array_v<T>;
202 template <
typename... Args>
222 SharedPtr(element_type* value, ControlBlock* control_block)
223 : Base(value), control_block_(control_block) {}
225 ControlBlock* control_block()
const {
return control_block_; }
228 template <
typename U>
229 void CopyFrom(
const SharedPtr<U>& other);
236 template <
typename U>
237 constexpr void CheckArrayTypes();
239 ControlBlock* control_block_ =
nullptr;
247template <
typename... Args>
248SharedPtr<T> SharedPtr<T>::Create(Allocator* allocator, Args&&... args) {
249 static_assert(!std::is_array_v<T>);
250 auto* control_block = ControlBlock::Create(allocator, Layout::Of<T>(), 1);
251 if (control_block ==
nullptr) {
254 auto* t =
new (control_block->data()) T(std::forward<Args>(args)...);
255 return SharedPtr<T>(t, control_block);
259SharedPtr<T> SharedPtr<T>::Create(Allocator* allocator,
262 static_assert(allocator::internal::is_unbounded_array_v<T>);
263 Layout layout = Layout::Of<T>(count).Align(alignment);
264 auto* control_block = ControlBlock::Create(allocator, layout, count);
265 if (control_block ==
nullptr) {
268 auto* t =
new (control_block->data()) std::remove_extent_t<T>[count];
269 return SharedPtr<T>(t, control_block);
275 const SharedPtr<U>& other)
noexcept {
276 CheckArrayTypes<U>();
278 if (control_block_ !=
nullptr) {
279 control_block_->IncrementShared();
287 CheckArrayTypes<U>();
302 if (*
this ==
nullptr) {
305 auto action = control_block_->DecrementShared();
306 if (action == ControlBlock::Action::kNone) {
313 Allocator* allocator = control_block_->allocator();
314 if (!Base::HasCapability(allocator, allocator::kSkipsDestroy)) {
315 if constexpr (std::is_array_v<T>) {
316 Base::Destroy(control_block_->size());
322 if (action == ControlBlock::Action::kExpire) {
325 Base::Resize(allocator, control_block_,
sizeof(ControlBlock));
328 Base::Deallocate(allocator, control_block_);
337 std::swap(control_block_, other.control_block_);
343 CheckArrayTypes<U>();
344 Base::CopyFrom(other);
345 control_block_ = other.control_block_;
349void SharedPtr<T>::Release() {
351 control_block_ =
nullptr;
356constexpr void SharedPtr<T>::CheckArrayTypes() {
357 if constexpr (std::is_array_v<T>) {
358 static_assert(std::is_array_v<U>,
359 "non-array type used with SharedPtr<T[]>");
361 static_assert(!std::is_array_v<U>,
"array type used with SharedPtr<T>");
Definition: allocator.h:36
Definition: multibuf_v2.h:192
Definition: shared_ptr.h:59
SharedPtr(SharedPtr< U > &&other) noexcept
Definition: shared_ptr.h:103
SharedPtr & operator=(SharedPtr< U > &&other) noexcept
size_t size() const
Definition: shared_ptr.h:142
void swap(SharedPtr &other) noexcept
Swaps the managed pointer and deallocator of this and another object.
Definition: shared_ptr.h:335
~SharedPtr()
Releases any currently-held value.
Definition: shared_ptr.h:69
constexpr SharedPtr & operator=(const SharedPtr< U > &other) noexcept
constexpr SharedPtr() noexcept=default
constexpr SharedPtr(const SharedPtr &other) noexcept
Copy-constructs a SharedPtr<T> from a SharedPtr<T>.
Definition: shared_ptr.h:84
int32_t use_count() const
Definition: shared_ptr.h:150
constexpr SharedPtr & operator=(const SharedPtr &other) noexcept
Definition: shared_ptr.h:110
constexpr SharedPtr(const SharedPtr< U > &other) noexcept
Definition: shared_ptr.h:93
void reset() noexcept
Definition: shared_ptr.h:301
Definition: weak_ptr.h:37
MultiBufProperty
Basic properties of a MultiBuf.
Definition: properties.h:24
The Pigweed namespace.
Definition: alignment.h:27