mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-01 15:30:29 +00:00
Document refcount-saving hack
This commit is contained in:
parent
e409c773e0
commit
1122f6a9d0
1 changed files with 6 additions and 1 deletions
|
@ -536,9 +536,14 @@ template<typename T, typename... Args>
|
|||
shared_ptr<T>
|
||||
make_shared(Args&&... args)
|
||||
{
|
||||
auto rc = __::shared_emplace<T>::make();
|
||||
unique_ptr rc = __::shared_emplace<T>::make();
|
||||
if constexpr (is_base_of_v<weak_self_base, T> &&
|
||||
is_constructible_v<T, const weak_ptr<T>&, Args...>) {
|
||||
// A __::shared_ref has a virtual weak reference that is owned by all of
|
||||
// the shared references. We can avoid some unnecessary refcount changes
|
||||
// by "borrowing" that reference and passing it to the constructor, then
|
||||
// promoting it to a shared reference by swapping it with the shared_ptr
|
||||
// that we return.
|
||||
weak_ptr<T> w;
|
||||
w.p = &rc->t;
|
||||
w.rc = rc.get();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue