Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:06

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H
0010 #define _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H
0011 
0012 #include <__algorithm/comp.h>
0013 #include <__assert>
0014 #include <__config>
0015 #include <__type_traits/enable_if.h>
0016 #include <__type_traits/integral_constant.h>
0017 #include <__type_traits/is_constant_evaluated.h>
0018 #include <__type_traits/void_t.h>
0019 #include <__utility/declval.h>
0020 #include <__utility/is_valid_range.h>
0021 
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 #  pragma GCC system_header
0024 #endif
0025 
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027 
0028 template <class _Tp, class _Up, class = void>
0029 struct __is_less_than_comparable : false_type {};
0030 
0031 template <class _Tp, class _Up>
0032 struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type {
0033 };
0034 
0035 template <class _Tp, class _Up, __enable_if_t<__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
0036 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool
0037 __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
0038   _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__begin, __end), "[__begin, __end) is not a valid range");
0039 
0040   if (__libcpp_is_constant_evaluated()) {
0041     // If this is not a constant during constant evaluation we know that __ptr is not part of the allocation where
0042     // [__begin, __end) is.
0043     if (!__builtin_constant_p(__begin <= __ptr && __ptr < __end))
0044       return false;
0045   }
0046 
0047   return !__less<>()(__ptr, __begin) && __less<>()(__ptr, __end);
0048 }
0049 
0050 template <class _Tp, class _Up, __enable_if_t<!__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
0051 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool
0052 __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
0053   if (__libcpp_is_constant_evaluated())
0054     return false;
0055 
0056   return reinterpret_cast<const char*>(__begin) <= reinterpret_cast<const char*>(__ptr) &&
0057          reinterpret_cast<const char*>(__ptr) < reinterpret_cast<const char*>(__end);
0058 }
0059 
0060 template <class _Tp, class _Up>
0061 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0062 __is_overlapping_range(const _Tp* __begin, const _Tp* __end, const _Up* __begin2) {
0063   auto __size = __end - __begin;
0064   auto __end2 = __begin2 + __size;
0065   return std::__is_pointer_in_range(__begin, __end, __begin2) || std::__is_pointer_in_range(__begin2, __end2, __begin);
0066 }
0067 
0068 _LIBCPP_END_NAMESPACE_STD
0069 
0070 #endif // _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H