Warning: This file is not a C or C++ file. It does not have highlighting.
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___UTILITY_IS_POINTER_IN_RANGE_H |
10 | #define _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H |
11 | |
12 | #include <__algorithm/comp.h> |
13 | #include <__assert> |
14 | #include <__config> |
15 | #include <__type_traits/enable_if.h> |
16 | #include <__type_traits/integral_constant.h> |
17 | #include <__type_traits/is_constant_evaluated.h> |
18 | #include <__type_traits/void_t.h> |
19 | #include <__utility/declval.h> |
20 | #include <__utility/is_valid_range.h> |
21 | |
22 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
23 | # pragma GCC system_header |
24 | #endif |
25 | |
26 | _LIBCPP_BEGIN_NAMESPACE_STD |
27 | |
28 | template <class _Tp, class _Up, class = void> |
29 | struct __is_less_than_comparable : false_type {}; |
30 | |
31 | template <class _Tp, class _Up> |
32 | struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type { |
33 | }; |
34 | |
35 | template <class _Tp, class _Up, __enable_if_t<__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0> |
36 | _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool |
37 | __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) { |
38 | _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__begin, __end), "[__begin, __end) is not a valid range"); |
39 | |
40 | if (__libcpp_is_constant_evaluated()) { |
41 | // If this is not a constant during constant evaluation we know that __ptr is not part of the allocation where |
42 | // [__begin, __end) is. |
43 | if (!__builtin_constant_p(__begin <= __ptr && __ptr < __end)) |
44 | return false; |
45 | } |
46 | |
47 | return !__less<>()(__ptr, __begin) && __less<>()(__ptr, __end); |
48 | } |
49 | |
50 | template <class _Tp, class _Up, __enable_if_t<!__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0> |
51 | _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool |
52 | __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) { |
53 | if (__libcpp_is_constant_evaluated()) |
54 | return false; |
55 | |
56 | return reinterpret_cast<const char*>(__begin) <= reinterpret_cast<const char*>(__ptr) && |
57 | reinterpret_cast<const char*>(__ptr) < reinterpret_cast<const char*>(__end); |
58 | } |
59 | |
60 | template <class _Tp, class _Up> |
61 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool |
62 | __is_overlapping_range(const _Tp* __begin, const _Tp* __end, const _Up* __begin2) { |
63 | auto __size = __end - __begin; |
64 | auto __end2 = __begin2 + __size; |
65 | return std::__is_pointer_in_range(__begin, __end, __begin2) || std::__is_pointer_in_range(__begin2, __end2, __begin); |
66 | } |
67 | |
68 | _LIBCPP_END_NAMESPACE_STD |
69 | |
70 | #endif // _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H |
71 |
Warning: This file is not a C or C++ file. It does not have highlighting.