[libcxx-commits] [PATCH] D143791: [libc++] Fix bug in allocate_shared_for_overwrite

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Feb 10 15:47:32 PST 2023


ldionne added a comment.

The intent would be to cherry-pick to LLVM 16 since this is effectively a bugfix.

Thanks a lot @tcanens for bringing this to our attention.



================
Comment at: libcxx/include/__memory/shared_ptr.h:266
+// initialization/destruction based on that.
+struct __for_overwrite_tag {};
 
----------------
@huixie90 I know this name is what you had started with and I asked to change it, but I think it now makes more sense since we do more than only default-constructing.


================
Comment at: libcxx/include/__memory/shared_ptr.h:968
 {
-    using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
-    using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
-    __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
-    ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__default_initialize_tag{}, __a);
-    auto __control_block = __guard.__release_ptr();
-    return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
+    using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
+    _ForOverwriteAllocator __alloc(__a);
----------------
This is a fly-by refactoring.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143791/new/

https://reviews.llvm.org/D143791



More information about the libcxx-commits mailing list