1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___ATOMIC_CLEAR_PADDING_H
10#define _LIBCPP___ATOMIC_CLEAR_PADDING_H
11
12#include <__config>
13#include <__memory/addressof.h>
14#include <__type_traits/enable_if.h>
15#include <__type_traits/is_constant_evaluated.h>
16#include <__type_traits/is_same.h>
17#include <__type_traits/remove_cv.h>
18#include <cstring>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26#if __has_builtin(__builtin_clear_padding)
27
28template <class _Tp>
29inline const bool __needs_clear_padding_v =
30 !__has_unique_object_representations(_Tp) && !is_same<_Tp, float>::value && !is_same<_Tp, double>::value;
31
32template <class _Tp, __enable_if_t<!__needs_clear_padding_v<__remove_cv_t<_Tp> >, int> = 0>
33_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp& __clear_padding_if_needed(_Tp& __obj) _NOEXCEPT {
34 return __obj;
35}
36
37template <class _Tp, __enable_if_t<__needs_clear_padding_v<__remove_cv_t<_Tp> >, int> = 0>
38_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp& __clear_padding_if_needed(_Tp& __obj) _NOEXCEPT {
39 return __libcpp_is_constant_evaluated() ? __obj : (__builtin_clear_padding(std::addressof(__obj)), __obj);
40}
41
42// clang fails to inline the function when the memory order is a constant
43template <class _Tp, class _Up, class _CasFunc, __enable_if_t<!__needs_clear_padding_v<__remove_cv_t<_Tp> >, int> = 0>
44_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE bool
45__atomic_cas_with_clear_padding(_Tp* __expected, _Up __value, _CasFunc&& __cas_func) {
46 return __cas_func(__expected, __value);
47}
48
49template <class _Tp, class _Up, class _CasFunc, __enable_if_t<__needs_clear_padding_v<__remove_cv_t<_Tp> >, int> = 0>
50_LIBCPP_HIDE_FROM_ABI bool __atomic_cas_with_clear_padding(_Tp* __expected, _Up __value, _CasFunc&& __cas_func) {
51 std::__clear_padding_if_needed(__value);
52 __remove_cv_t<_Tp> __expected_copy = *__expected;
53 std::__clear_padding_if_needed(__expected_copy);
54 if (__cas_func(std::addressof(__expected_copy), __value)) {
55 return true;
56 } else {
57 std::memcpy(__expected, std::addressof(__expected_copy), sizeof(__remove_cv_t<_Tp>));
58 return false;
59 }
60}
61
62#else // __has_builtin(__builtin_clear_padding)
63
64template <class _Tp>
65_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp& __clear_padding_if_needed(_Tp& __obj) _NOEXCEPT {
66 return __obj;
67}
68
69template <class _Tp, class _Up, class _CasFunc>
70_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE bool
71__atomic_cas_with_clear_padding(_Tp* __expected, _Up __value, _CasFunc&& __cas_func) {
72 return __cas_func(__expected, __value);
73}
74
75#endif // __has_builtin(__builtin_clear_padding)
76
77_LIBCPP_END_NAMESPACE_STD
78
79#endif // _LIBCPP___ATOMIC_CLEAR_PADDING_H
80