Skip to content

Commit 19d57b5

Browse files
committed
[libc++] Refactor allocate_shared to use an allocation guard
This commit is a step towards making it easier to add support for arrays in allocate_shared. Adding support for arrays will require writing multiple functions, and the current complexity of writing allocate_shared is prohibitive for understanding. Differential Revision: https://reviews.llvm.org/D93130
1 parent a29ecca commit 19d57b5

File tree

3 files changed

+101
-19
lines changed

3 files changed

+101
-19
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(files
1414
__memory/allocator_traits.h
1515
__memory/base.h
1616
__memory/pointer_traits.h
17+
__memory/utilities.h
1718
__mutex_base
1819
__node_handle
1920
__nullptr

libcxx/include/__memory/utilities.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___MEMORY_UTILITIES_H
11+
#define _LIBCPP___MEMORY_UTILITIES_H
12+
13+
#include <__config>
14+
#include <__memory/allocator_traits.h>
15+
#include <cstddef>
16+
17+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18+
#pragma GCC system_header
19+
#endif
20+
21+
_LIBCPP_PUSH_MACROS
22+
#include <__undef_macros>
23+
24+
25+
_LIBCPP_BEGIN_NAMESPACE_STD
26+
27+
// Helper class to allocate memory using an Allocator in an exception safe
28+
// manner.
29+
//
30+
// The intended usage of this class is as follows:
31+
//
32+
// 0
33+
// 1 __allocation_guard<SomeAllocator> guard(alloc, 10);
34+
// 2 do_some_initialization_that_may_throw(guard.__get());
35+
// 3 save_allocated_pointer_in_a_noexcept_operation(guard.__release_ptr());
36+
// 4
37+
//
38+
// If line (2) throws an exception during initialization of the memory, the
39+
// guard's destructor will be called, and the memory will be released using
40+
// Allocator deallocation. Otherwise, we release the memory from the guard on
41+
// line (3) in an operation that can't throw -- after that, the guard is not
42+
// responsible for the memory anymore.
43+
//
44+
// This is similar to a unique_ptr, except it's easier to use with a
45+
// custom allocator.
46+
template<class _Alloc>
47+
struct __allocation_guard {
48+
using _Pointer = typename allocator_traits<_Alloc>::pointer;
49+
using _Size = typename allocator_traits<_Alloc>::size_type;
50+
51+
_LIBCPP_HIDE_FROM_ABI
52+
explicit __allocation_guard(_Alloc __alloc, _Size __n)
53+
: __alloc_(_VSTD::move(__alloc))
54+
, __n_(__n)
55+
, __ptr_(allocator_traits<_Alloc>::allocate(__alloc_, __n_)) // initialization order is important
56+
{ }
57+
58+
_LIBCPP_HIDE_FROM_ABI
59+
~__allocation_guard() _NOEXCEPT {
60+
if (__ptr_ != nullptr) {
61+
allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __n_);
62+
}
63+
}
64+
65+
_LIBCPP_HIDE_FROM_ABI
66+
_Pointer __release_ptr() _NOEXCEPT { // not called __release() because it's a keyword in objective-c++
67+
_Pointer __tmp = __ptr_;
68+
__ptr_ = nullptr;
69+
return __tmp;
70+
}
71+
72+
_LIBCPP_HIDE_FROM_ABI
73+
_Pointer __get() const _NOEXCEPT {
74+
return __ptr_;
75+
}
76+
77+
private:
78+
_Alloc __alloc_;
79+
_Size __n_;
80+
_Pointer __ptr_;
81+
};
82+
83+
_LIBCPP_END_NAMESPACE_STD
84+
85+
_LIBCPP_POP_MACROS
86+
87+
#endif // _LIBCPP___MEMORY_UTILITIES_H

libcxx/include/memory

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
682682
#include <__memory/allocator_traits.h>
683683
#include <__memory/base.h>
684684
#include <__memory/pointer_traits.h>
685+
#include <__memory/utilities.h>
685686
#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
686687
# include <atomic>
687688
#endif
@@ -3299,28 +3300,21 @@ shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
32993300
shared_ptr(__p, __d, __a).swap(*this);
33003301
}
33013302

3302-
template<class _Tp, class _Alloc, class ..._Args>
3303-
inline _LIBCPP_INLINE_VISIBILITY
3304-
typename enable_if
3305-
<
3306-
!is_array<_Tp>::value,
3307-
shared_ptr<_Tp>
3308-
>::type
3309-
allocate_shared(const _Alloc& __a, _Args&& ...__args)
3303+
//
3304+
// std::allocate_shared and std::make_shared
3305+
//
3306+
template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3307+
_LIBCPP_HIDE_FROM_ABI
3308+
shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
33103309
{
33113310
static_assert(is_constructible<_Tp, _Args...>::value,
33123311
"allocate_shared/make_shared: the type is not constructible from the provided arguments");
3313-
3314-
typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3315-
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3316-
typedef __allocator_destructor<_A2> _D2;
3317-
3318-
_A2 __a2(__a);
3319-
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3320-
::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
3321-
3322-
typename shared_ptr<_Tp>::element_type *__p = __hold2->__get_elem();
3323-
return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
3312+
using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3313+
using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3314+
__allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3315+
::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3316+
auto __control_block = __guard.__release_ptr();
3317+
return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
33243318
}
33253319

33263320
template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >

0 commit comments

Comments
 (0)