[libc] Switch to using the generic `<gpuintrin.h>` implementations (#121810)
[llvm-project.git] / libcxx / include / __memory / array_cookie.h
blob806a9e99ecafe74f2053c222958c923fdafce90b
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___MEMORY_ARRAY_COOKIE_H
11 #define _LIBCPP___MEMORY_ARRAY_COOKIE_H
13 #include <__config>
14 #include <__configuration/abi.h>
15 #include <__cstddef/size_t.h>
16 #include <__type_traits/integral_constant.h>
17 #include <__type_traits/is_trivially_destructible.h>
18 #include <__type_traits/negation.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
22 #endif
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 // Trait representing whether a type requires an array cookie at the start of its allocation when
27 // allocated as `new T[n]` and deallocated as `delete[] array`.
29 // Under the Itanium C++ ABI [1], we know that an array cookie is available unless `T` is trivially
30 // destructible and the call to `operator delete[]` is not a sized operator delete. Under ABIs other
31 // than the Itanium ABI, we assume there are no array cookies.
33 // [1]: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-cookies
34 #ifdef _LIBCPP_ABI_ITANIUM
35 // TODO: Use a builtin instead
36 // TODO: We should factor in the choice of the usual deallocation function in this determination.
37 template <class _Tp>
38 struct __has_array_cookie : _Not<is_trivially_destructible<_Tp> > {};
39 #else
40 template <class _Tp>
41 struct __has_array_cookie : false_type {};
42 #endif
44 template <class _Tp>
45 // Avoid failures when -fsanitize-address-poison-custom-array-cookie is enabled
46 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") size_t __get_array_cookie(_Tp const* __ptr) {
47 static_assert(
48 __has_array_cookie<_Tp>::value, "Trying to access the array cookie of a type that is not guaranteed to have one");
49 size_t const* __cookie = reinterpret_cast<size_t const*>(__ptr) - 1; // TODO: Use a builtin instead
50 return *__cookie;
53 _LIBCPP_END_NAMESPACE_STD
55 #endif // _LIBCPP___MEMORY_ARRAY_COOKIE_H