xref: /freebsd-src/contrib/llvm-project/libcxx/include/__utility/is_pointer_in_range.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
106c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric 
906c3fb27SDimitry Andric #ifndef _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H
1006c3fb27SDimitry Andric #define _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H
1106c3fb27SDimitry Andric 
1206c3fb27SDimitry Andric #include <__algorithm/comp.h>
1306c3fb27SDimitry Andric #include <__assert>
1406c3fb27SDimitry Andric #include <__config>
1506c3fb27SDimitry Andric #include <__type_traits/enable_if.h>
1606c3fb27SDimitry Andric #include <__type_traits/integral_constant.h>
1706c3fb27SDimitry Andric #include <__type_traits/is_constant_evaluated.h>
1806c3fb27SDimitry Andric #include <__type_traits/void_t.h>
1906c3fb27SDimitry Andric #include <__utility/declval.h>
20*0fca6ea1SDimitry Andric #include <__utility/is_valid_range.h>
2106c3fb27SDimitry Andric 
2206c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2306c3fb27SDimitry Andric #  pragma GCC system_header
2406c3fb27SDimitry Andric #endif
2506c3fb27SDimitry Andric 
2606c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2706c3fb27SDimitry Andric 
2806c3fb27SDimitry Andric template <class _Tp, class _Up, class = void>
2906c3fb27SDimitry Andric struct __is_less_than_comparable : false_type {};
3006c3fb27SDimitry Andric 
3106c3fb27SDimitry Andric template <class _Tp, class _Up>
3206c3fb27SDimitry Andric struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type {
3306c3fb27SDimitry Andric };
3406c3fb27SDimitry Andric 
3506c3fb27SDimitry Andric template <class _Tp, class _Up, __enable_if_t<__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
36*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool
37*0fca6ea1SDimitry Andric __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
38*0fca6ea1SDimitry Andric   _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__begin, __end), "[__begin, __end) is not a valid range");
3906c3fb27SDimitry Andric 
40*0fca6ea1SDimitry Andric   if (__libcpp_is_constant_evaluated()) {
4106c3fb27SDimitry Andric     // If this is not a constant during constant evaluation we know that __ptr is not part of the allocation where
4206c3fb27SDimitry Andric     // [__begin, __end) is.
4306c3fb27SDimitry Andric     if (!__builtin_constant_p(__begin <= __ptr && __ptr < __end))
4406c3fb27SDimitry Andric       return false;
4506c3fb27SDimitry Andric   }
4606c3fb27SDimitry Andric 
4706c3fb27SDimitry Andric   return !__less<>()(__ptr, __begin) && __less<>()(__ptr, __end);
4806c3fb27SDimitry Andric }
4906c3fb27SDimitry Andric 
5006c3fb27SDimitry Andric template <class _Tp, class _Up, __enable_if_t<!__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
51*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool
52*0fca6ea1SDimitry Andric __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
5306c3fb27SDimitry Andric   if (__libcpp_is_constant_evaluated())
5406c3fb27SDimitry Andric     return false;
5506c3fb27SDimitry Andric 
5606c3fb27SDimitry Andric   return reinterpret_cast<const char*>(__begin) <= reinterpret_cast<const char*>(__ptr) &&
5706c3fb27SDimitry Andric          reinterpret_cast<const char*>(__ptr) < reinterpret_cast<const char*>(__end);
5806c3fb27SDimitry Andric }
5906c3fb27SDimitry Andric 
6006c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
6106c3fb27SDimitry Andric 
6206c3fb27SDimitry Andric #endif // _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H
63