Skip to content

Commit 00d98c3

Browse files
committed
[libc++] Use std::__{scope,exception}_guard throughout the code base
1 parent bf847a8 commit 00d98c3

File tree

14 files changed

+342
-628
lines changed

14 files changed

+342
-628
lines changed

libcxx/include/__hash_table

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <__utility/forward.h>
4545
#include <__utility/move.h>
4646
#include <__utility/pair.h>
47+
#include <__utility/scope_guard.h>
4748
#include <__utility/swap.h>
4849
#include <__utility/try_key_extraction.h>
4950
#include <limits>
@@ -1317,23 +1318,14 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(__hash_table& __u,
13171318
max_load_factor() = __u.max_load_factor();
13181319
if (bucket_count() != 0) {
13191320
__next_pointer __cache = __detach();
1320-
#if _LIBCPP_HAS_EXCEPTIONS
1321-
try {
1322-
#endif // _LIBCPP_HAS_EXCEPTIONS
1323-
const_iterator __i = __u.begin();
1324-
while (__cache != nullptr && __u.size() != 0) {
1325-
__assign_value(__cache->__upcast()->__get_value(), std::move(__u.remove(__i++)->__get_value()));
1326-
__next_pointer __next = __cache->__next_;
1327-
__node_insert_multi(__cache->__upcast());
1328-
__cache = __next;
1329-
}
1330-
#if _LIBCPP_HAS_EXCEPTIONS
1331-
} catch (...) {
1332-
__deallocate_node(__cache);
1333-
throw;
1321+
auto __guard = std::__make_scope_guard([&] { __deallocate_node(__cache); });
1322+
const_iterator __i = __u.begin();
1323+
while (__cache != nullptr && __u.size() != 0) {
1324+
__assign_value(__cache->__upcast()->__get_value(), std::move(__u.remove(__i++)->__get_value()));
1325+
__next_pointer __next = __cache->__next_;
1326+
__node_insert_multi(__cache->__upcast());
1327+
__cache = __next;
13341328
}
1335-
#endif // _LIBCPP_HAS_EXCEPTIONS
1336-
__deallocate_node(__cache);
13371329
}
13381330
const_iterator __i = __u.begin();
13391331
while (__u.size() != 0)
@@ -1361,22 +1353,13 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __
13611353

13621354
if (bucket_count() != 0) {
13631355
__next_pointer __cache = __detach();
1364-
#if _LIBCPP_HAS_EXCEPTIONS
1365-
try {
1366-
#endif // _LIBCPP_HAS_EXCEPTIONS
1367-
for (; __cache != nullptr && __first != __last; ++__first) {
1368-
__assign_value(__cache->__upcast()->__get_value(), *__first);
1369-
__next_pointer __next = __cache->__next_;
1370-
__node_insert_unique(__cache->__upcast());
1371-
__cache = __next;
1372-
}
1373-
#if _LIBCPP_HAS_EXCEPTIONS
1374-
} catch (...) {
1375-
__deallocate_node(__cache);
1376-
throw;
1356+
auto __guard = std::__make_scope_guard([&] { __deallocate_node(__cache); });
1357+
for (; __cache != nullptr && __first != __last; ++__first) {
1358+
__assign_value(__cache->__upcast()->__get_value(), *__first);
1359+
__next_pointer __next = __cache->__next_;
1360+
__node_insert_unique(__cache->__upcast());
1361+
__cache = __next;
13771362
}
1378-
#endif // _LIBCPP_HAS_EXCEPTIONS
1379-
__deallocate_node(__cache);
13801363
}
13811364
for (; __first != __last; ++__first)
13821365
__emplace_unique(*__first);
@@ -1391,22 +1374,13 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __f
13911374
"__assign_multi may only be called with the containers value type or the nodes value type");
13921375
if (bucket_count() != 0) {
13931376
__next_pointer __cache = __detach();
1394-
#if _LIBCPP_HAS_EXCEPTIONS
1395-
try {
1396-
#endif // _LIBCPP_HAS_EXCEPTIONS
1397-
for (; __cache != nullptr && __first != __last; ++__first) {
1398-
__assign_value(__cache->__upcast()->__get_value(), *__first);
1399-
__next_pointer __next = __cache->__next_;
1400-
__node_insert_multi(__cache->__upcast());
1401-
__cache = __next;
1402-
}
1403-
#if _LIBCPP_HAS_EXCEPTIONS
1404-
} catch (...) {
1405-
__deallocate_node(__cache);
1406-
throw;
1377+
auto __guard = std::__make_scope_guard([&] { __deallocate_node(__cache); });
1378+
for (; __cache != nullptr && __first != __last; ++__first) {
1379+
__assign_value(__cache->__upcast()->__get_value(), *__first);
1380+
__next_pointer __next = __cache->__next_;
1381+
__node_insert_multi(__cache->__upcast());
1382+
__cache = __next;
14071383
}
1408-
#endif // _LIBCPP_HAS_EXCEPTIONS
1409-
__deallocate_node(__cache);
14101384
}
14111385
for (; __first != __last; ++__first)
14121386
__emplace_multi(*__first);

libcxx/include/__memory/shared_ptr.h

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <__type_traits/remove_extent.h>
5555
#include <__type_traits/remove_reference.h>
5656
#include <__utility/declval.h>
57+
#include <__utility/exception_guard.h>
5758
#include <__utility/forward.h>
5859
#include <__utility/move.h>
5960
#include <__utility/swap.h>
@@ -352,52 +353,38 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
352353

353354
template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
354355
_LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) {
355-
#if _LIBCPP_HAS_EXCEPTIONS
356-
try {
357-
#endif // _LIBCPP_HAS_EXCEPTIONS
358-
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
359-
typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
356+
auto __guard = std::__make_exception_guard([&] { __d(__p); });
357+
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
358+
typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
360359
#ifndef _LIBCPP_CXX03_LANG
361-
__cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
360+
__cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
362361
#else
363362
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
364363
#endif // not _LIBCPP_CXX03_LANG
365-
__enable_weak_this(__p, __p);
366-
#if _LIBCPP_HAS_EXCEPTIONS
367-
} catch (...) {
368-
__d(__p);
369-
throw;
370-
}
371-
#endif // _LIBCPP_HAS_EXCEPTIONS
364+
__enable_weak_this(__p, __p);
365+
__guard.__complete();
372366
}
373367

374368
template <class _Yp,
375369
class _Dp,
376370
class _Alloc,
377371
__enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
378372
_LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) {
379-
#if _LIBCPP_HAS_EXCEPTIONS
380-
try {
381-
#endif // _LIBCPP_HAS_EXCEPTIONS
382-
typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
383-
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
384-
typedef __allocator_destructor<_A2> _D2;
385-
_A2 __a2(__a);
386-
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
387-
::new ((void*)std::addressof(*__hold2.get()))
373+
auto __guard = std::__make_exception_guard([&] { __d(__p); });
374+
typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
375+
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
376+
typedef __allocator_destructor<_A2> _D2;
377+
_A2 __a2(__a);
378+
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
379+
::new ((void*)std::addressof(*__hold2.get()))
388380
#ifndef _LIBCPP_CXX03_LANG
389-
_CntrlBlk(__p, std::move(__d), __a);
381+
_CntrlBlk(__p, std::move(__d), __a);
390382
#else
391383
_CntrlBlk(__p, __d, __a);
392384
#endif // not _LIBCPP_CXX03_LANG
393-
__cntrl_ = std::addressof(*__hold2.release());
394-
__enable_weak_this(__p, __p);
395-
#if _LIBCPP_HAS_EXCEPTIONS
396-
} catch (...) {
397-
__d(__p);
398-
throw;
399-
}
400-
#endif // _LIBCPP_HAS_EXCEPTIONS
385+
__cntrl_ = std::addressof(*__hold2.release());
386+
__enable_weak_this(__p, __p);
387+
__guard.__complete();
401388
}
402389

403390
template <class _Dp>
@@ -406,22 +393,15 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
406393
_Dp __d,
407394
__enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
408395
: __ptr_(nullptr) {
409-
#if _LIBCPP_HAS_EXCEPTIONS
410-
try {
411-
#endif // _LIBCPP_HAS_EXCEPTIONS
412-
typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
413-
typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;
396+
auto __guard = std::__make_exception_guard([&] { __d(__p); });
397+
typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
398+
typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;
414399
#ifndef _LIBCPP_CXX03_LANG
415-
__cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
400+
__cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
416401
#else
417402
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
418403
#endif // not _LIBCPP_CXX03_LANG
419-
#if _LIBCPP_HAS_EXCEPTIONS
420-
} catch (...) {
421-
__d(__p);
422-
throw;
423-
}
424-
#endif // _LIBCPP_HAS_EXCEPTIONS
404+
__guard.__complete();
425405
}
426406

427407
template <class _Dp, class _Alloc>
@@ -431,27 +411,20 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
431411
_Alloc __a,
432412
__enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
433413
: __ptr_(nullptr) {
434-
#if _LIBCPP_HAS_EXCEPTIONS
435-
try {
436-
#endif // _LIBCPP_HAS_EXCEPTIONS
437-
typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
438-
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
439-
typedef __allocator_destructor<_A2> _D2;
440-
_A2 __a2(__a);
441-
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
442-
::new ((void*)std::addressof(*__hold2.get()))
414+
auto __guard = std::__make_exception_guard([&] { __d(__p); });
415+
typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
416+
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
417+
typedef __allocator_destructor<_A2> _D2;
418+
_A2 __a2(__a);
419+
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
420+
::new ((void*)std::addressof(*__hold2.get()))
443421
#ifndef _LIBCPP_CXX03_LANG
444-
_CntrlBlk(__p, std::move(__d), __a);
422+
_CntrlBlk(__p, std::move(__d), __a);
445423
#else
446424
_CntrlBlk(__p, __d, __a);
447425
#endif // not _LIBCPP_CXX03_LANG
448-
__cntrl_ = std::addressof(*__hold2.release());
449-
#if _LIBCPP_HAS_EXCEPTIONS
450-
} catch (...) {
451-
__d(__p);
452-
throw;
453-
}
454-
#endif // _LIBCPP_HAS_EXCEPTIONS
426+
__cntrl_ = std::addressof(*__hold2.release());
427+
__guard.__complete();
455428
}
456429

457430
template <class _Yp>

0 commit comments

Comments
 (0)