mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-03 08:20:28 +00:00
Try moving the unique_ptr into shared_pointer
__::shared_pointer::make takes ownership of p the same way as shared_ptr does: either the allocation succeeds and the returned control block owns it, or the allocation throws and the unique_ptr frees it. Note that this construction is safe since C++17, in which the evaluation of constructor arguments is sequenced after a new-expression allocation.
This commit is contained in:
parent
d8ee0130e2
commit
8794cdc99d
1 changed files with 4 additions and 5 deletions
|
@ -48,7 +48,9 @@ struct shared_pointer : shared_control
|
|||
|
||||
static shared_pointer* make(T* p)
|
||||
{
|
||||
return new shared_pointer(p);
|
||||
auto p2 = unique_ptr<T>(p);
|
||||
auto r = new shared_pointer(p2.release());
|
||||
return r;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -85,11 +87,8 @@ class shared_ptr
|
|||
: p(nullptr), ctl(nullptr)
|
||||
{
|
||||
}
|
||||
shared_ptr(T* const p2)
|
||||
shared_ptr(T* const p) : p(p), ctl(__::shared_pointer<T>::make(p))
|
||||
{
|
||||
auto hold = ctl::unique_ptr(p2);
|
||||
ctl = __::shared_pointer<T>::make(p2);
|
||||
p = hold.release();
|
||||
}
|
||||
|
||||
~shared_ptr()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue