xref: /xnu-12377.81.4/iokit/DriverKit/bounded_ptr.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions //
2*043036a2SApple OSS Distributions // Copyright (c) 2019 Apple, Inc. All rights reserved.
3*043036a2SApple OSS Distributions //
4*043036a2SApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions //
6*043036a2SApple OSS Distributions // This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions // as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions // Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions // compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions // may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions // unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions // circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions // terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions //
15*043036a2SApple OSS Distributions // Please obtain a copy of the License at
16*043036a2SApple OSS Distributions // http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions //
18*043036a2SApple OSS Distributions // The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions // Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions // limitations under the License.
25*043036a2SApple OSS Distributions //
26*043036a2SApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions //
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions #ifndef XNU_LIBKERN_LIBKERN_CXX_BOUNDED_PTR_H
30*043036a2SApple OSS Distributions #define XNU_LIBKERN_LIBKERN_CXX_BOUNDED_PTR_H
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions #if !TAPI
33*043036a2SApple OSS Distributions 
34*043036a2SApple OSS Distributions #include <stddef.h>
35*043036a2SApple OSS Distributions #include <stdint.h>
36*043036a2SApple OSS Distributions #include <os/overflow.h>
37*043036a2SApple OSS Distributions #include <os/base.h>
38*043036a2SApple OSS Distributions 
39*043036a2SApple OSS Distributions #if !defined(__improbable)
40*043036a2SApple OSS Distributions #   define __improbable(...) __builtin_expect((__VA_ARGS__), 0)
41*043036a2SApple OSS Distributions #endif
42*043036a2SApple OSS Distributions 
43*043036a2SApple OSS Distributions namespace libkern {
44*043036a2SApple OSS Distributions namespace detail {
45*043036a2SApple OSS Distributions // Reimplementation of things in <type_traits> because we don't seem
46*043036a2SApple OSS Distributions // to have the right to rely on the C++ Standard Library (based on
47*043036a2SApple OSS Distributions // attempts to compile IOHIDFamily).
48*043036a2SApple OSS Distributions // TODO: Do we really need to re-implement this here?
49*043036a2SApple OSS Distributions template <typename ...> using void_t = void;
50*043036a2SApple OSS Distributions template <typename T> T && declval() noexcept;
51*043036a2SApple OSS Distributions using nullptr_t = decltype(nullptr);
52*043036a2SApple OSS Distributions template <bool Cond, typename T = void> struct enable_if;
53*043036a2SApple OSS Distributions template <typename T> struct enable_if<true, T> { using type = T; };
54*043036a2SApple OSS Distributions template <bool Cond, typename T = void> using enable_if_t = typename enable_if<Cond, T>::type;
55*043036a2SApple OSS Distributions template <typename T1, typename T2>
56*043036a2SApple OSS Distributions constexpr bool is_convertible_v = __is_convertible_to(T1, T2);
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions template <typename T> inline constexpr bool is_void_v = false;
59*043036a2SApple OSS Distributions template <> inline constexpr bool is_void_v<void> = true;
60*043036a2SApple OSS Distributions template <> inline constexpr bool is_void_v<void const> = true;
61*043036a2SApple OSS Distributions 
62*043036a2SApple OSS Distributions template <typename T, typename U> struct copy_const { using type = U; };
63*043036a2SApple OSS Distributions template <typename T, typename U> struct copy_const<T const, U> { using type = U const; };
64*043036a2SApple OSS Distributions template <typename T, typename U> using copy_const_t = typename copy_const<T, U>::type;
65*043036a2SApple OSS Distributions 
66*043036a2SApple OSS Distributions template <typename T, typename U> struct copy_cv { using type = U; };
67*043036a2SApple OSS Distributions template <typename T, typename U> struct copy_cv<T const, U> { using type = U const; };
68*043036a2SApple OSS Distributions template <typename T, typename U> struct copy_cv<T volatile, U> { using type = U volatile; };
69*043036a2SApple OSS Distributions template <typename T, typename U> struct copy_cv<T const volatile, U> { using type = U const volatile; };
70*043036a2SApple OSS Distributions template <typename T, typename U> using copy_cv_t = typename copy_cv<T, U>::type;
71*043036a2SApple OSS Distributions 
72*043036a2SApple OSS Distributions template <typename T, typename U>
73*043036a2SApple OSS Distributions using WhenComparable = void_t<
74*043036a2SApple OSS Distributions 	decltype(declval<T>() == declval<U>()),
75*043036a2SApple OSS Distributions 	decltype(declval<T>() != declval<U>())
76*043036a2SApple OSS Distributions 	>;
77*043036a2SApple OSS Distributions 
78*043036a2SApple OSS Distributions template <typename T, typename U>
79*043036a2SApple OSS Distributions using WhenOrderable = void_t <
80*043036a2SApple OSS Distributions     decltype(declval<T>() < declval<U>()),
81*043036a2SApple OSS Distributions decltype(declval<T>() > declval<U>()),
82*043036a2SApple OSS Distributions decltype(declval<T>() >= declval<U>()),
83*043036a2SApple OSS Distributions decltype(declval<T>() <= declval<U>())
84*043036a2SApple OSS Distributions >;
85*043036a2SApple OSS Distributions 
86*043036a2SApple OSS Distributions // Pretend that sizeof(void) is 1, otherwise the in-bounds check doesn't
87*043036a2SApple OSS Distributions // make sense for `bounded_ptr<void>`.
88*043036a2SApple OSS Distributions template <typename T> constexpr size_t sizeof_v = sizeof(T);
89*043036a2SApple OSS Distributions template <>           inline constexpr size_t sizeof_v<void> = 1;
90*043036a2SApple OSS Distributions template <>           inline constexpr size_t sizeof_v<void const> = 1;
91*043036a2SApple OSS Distributions template <>           inline constexpr size_t sizeof_v<void volatile> = 1;
92*043036a2SApple OSS Distributions template <>           inline constexpr size_t sizeof_v<void const volatile> = 1;
93*043036a2SApple OSS Distributions } // end namespace detail
94*043036a2SApple OSS Distributions 
95*043036a2SApple OSS Distributions // Non-owning pointer to an object (or a range of objects) of type `T`
96*043036a2SApple OSS Distributions // that validates that the address is within some specified bounds on
97*043036a2SApple OSS Distributions // dereference-like operations.
98*043036a2SApple OSS Distributions //
99*043036a2SApple OSS Distributions // Conceptually, a `bounded_ptr` points within a range of memory `[begin, end)`.
100*043036a2SApple OSS Distributions // If accessing any part of the result of dereferencing the pointer would
101*043036a2SApple OSS Distributions // lead to an access outside of the `[begin, end)` range, the pointer is
102*043036a2SApple OSS Distributions // said to be out-of-bounds. Due to representational constraints, the range
103*043036a2SApple OSS Distributions // of in-bounds memory must be no larger than 4GB.
104*043036a2SApple OSS Distributions //
105*043036a2SApple OSS Distributions // Dereference-like operations (dereference, subscript, pointer member access)
106*043036a2SApple OSS Distributions // validate that the pointer is not out-of-bounds. If an out-of-bounds pointer
107*043036a2SApple OSS Distributions // is dereferenced, the `TrappingPolicy` is called as
108*043036a2SApple OSS Distributions // `TrappingPolicy::trap(some-message)`, and the operation is said to "trap".
109*043036a2SApple OSS Distributions // This terminology is used below to describe the behavior of the `TrappingPolicy`.
110*043036a2SApple OSS Distributions //
111*043036a2SApple OSS Distributions // Pointer arithmetic is allowed (and the bounds are not validated), so it is
112*043036a2SApple OSS Distributions // entirely possible to make a `bounded_ptr` point outside of its range.
113*043036a2SApple OSS Distributions // However, overflow checking is performed on arithmetic operations, and
114*043036a2SApple OSS Distributions // any operation resulting in an overflow will also "trap".
115*043036a2SApple OSS Distributions //
116*043036a2SApple OSS Distributions // The behavior of the `TrappingPolicy` can be customized as desired, however
117*043036a2SApple OSS Distributions // a trap should never return, causing the current `bounded_ptr` operation to
118*043036a2SApple OSS Distributions // be aborted. This is important since the trap could signify an integer
119*043036a2SApple OSS Distributions // overflow, a null-pointer dereference or something else that would lead to
120*043036a2SApple OSS Distributions // undefined behavior (UB) if `TrappingPolicy::trap` were to return.
121*043036a2SApple OSS Distributions //
122*043036a2SApple OSS Distributions // Creation of `bounded_ptr`s
123*043036a2SApple OSS Distributions // ==========================
124*043036a2SApple OSS Distributions // `bounded_ptr` provides a single constructor allowing the bounds of the
125*043036a2SApple OSS Distributions // pointer to be specified. When integrating `bounded_ptr` into an existing
126*043036a2SApple OSS Distributions // code base, it is recommended to use `bounded_ptr` as an iterator obtained
127*043036a2SApple OSS Distributions // from other container-like abstractions, instead of manually using the
128*043036a2SApple OSS Distributions // constructor that allows specifying a range. Specifying the range manually
129*043036a2SApple OSS Distributions // on construction is error-prone, and `bounded_ptr` can't help reduce
130*043036a2SApple OSS Distributions // out-of-bounds accesses if the bounds are specified incorrectly.
131*043036a2SApple OSS Distributions //
132*043036a2SApple OSS Distributions // Furthermore, it is a design choice to not provide a constructor that uses
133*043036a2SApple OSS Distributions // relative offsets from the pointer itself to determine the range, because
134*043036a2SApple OSS Distributions // such a constructor is deemed more confusing than helpful. For example, is
135*043036a2SApple OSS Distributions // the offset a number of bytes or a number of objects? Is the offset inclusive
136*043036a2SApple OSS Distributions // or exclusive? Instead, factory functions should be used to create `bounded_ptr`s.
137*043036a2SApple OSS Distributions //
138*043036a2SApple OSS Distributions // Remark on const-ness
139*043036a2SApple OSS Distributions // ====================
140*043036a2SApple OSS Distributions // Like for raw pointers, the const-ness of a `bounded_ptr` has no bearing on
141*043036a2SApple OSS Distributions // whether the pointee is const. Hence, it is possible to obtain a non-const
142*043036a2SApple OSS Distributions // reference to an object from a const `bounded_ptr`. To encode a
143*043036a2SApple OSS Distributions // pointer-to-const, simply create a `bounded_ptr<T const>`.
144*043036a2SApple OSS Distributions template <typename T, typename TrappingPolicy>
145*043036a2SApple OSS Distributions struct __attribute__((trivial_abi)) bounded_ptr {
146*043036a2SApple OSS Distributions private:
147*043036a2SApple OSS Distributions 	using CharType = detail::copy_cv_t<T, char>;
148*043036a2SApple OSS Distributions 
149*043036a2SApple OSS Distributions public:
150*043036a2SApple OSS Distributions 	// Creates a null `bounded_ptr`.
151*043036a2SApple OSS Distributions 	//
152*043036a2SApple OSS Distributions 	// A null `bounded_ptr` does not point to any object and is conceptually
153*043036a2SApple OSS Distributions 	// out of bounds, so dereferencing it will trap. "Observing" operations
154*043036a2SApple OSS Distributions 	// like comparison and check-for-null, along with assignment, are valid
155*043036a2SApple OSS Distributions 	// operations on a null `bounded_ptr`.
156*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE constexpr
157*043036a2SApple OSS Distributions 	bounded_ptr(detail::nullptr_t)
158*043036a2SApple OSS Distributions 		: base_(nullptr), count_(0), offset_(0)
159*043036a2SApple OSS Distributions 	{
160*043036a2SApple OSS Distributions 	}
161*043036a2SApple OSS Distributions 
162*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE constexpr
163*043036a2SApple OSS Distributions 	explicit
164*043036a2SApple OSS Distributions 	bounded_ptr()
165*043036a2SApple OSS Distributions 		: bounded_ptr(nullptr)
166*043036a2SApple OSS Distributions 	{
167*043036a2SApple OSS Distributions 	}
168*043036a2SApple OSS Distributions 
169*043036a2SApple OSS Distributions 	// Creates a `bounded_ptr` pointing to the given object, and whose bounds
170*043036a2SApple OSS Distributions 	// are described by the provided `[begin, end)` range.
171*043036a2SApple OSS Distributions 	//
172*043036a2SApple OSS Distributions 	// This constructor does not check whether the constructed pointer is
173*043036a2SApple OSS Distributions 	// within its bounds. However, it does check that the provided `[begin, end)`
174*043036a2SApple OSS Distributions 	// range is a valid range (that is, `begin <= end`).
175*043036a2SApple OSS Distributions 	//
176*043036a2SApple OSS Distributions 	// Furthermore, the number of bytes in the range of in-bounds memory must be
177*043036a2SApple OSS Distributions 	// representable by a uint32_t, which means that there can be no more than
178*043036a2SApple OSS Distributions 	// 2^32 bytes (i.e. 4GB) in that range. Otherwise, the constructor will trap.
179*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE explicit
180*043036a2SApple OSS Distributions 	bounded_ptr(T* pointer, T const* begin, T const* end)
181*043036a2SApple OSS Distributions 	{
182*043036a2SApple OSS Distributions 		base_ = reinterpret_cast<CharType*>(const_cast<T*>(begin));
183*043036a2SApple OSS Distributions 
184*043036a2SApple OSS Distributions 		// Store (end - begin) into count_, making sure we don't overflow
185*043036a2SApple OSS Distributions 		if (__improbable(os_sub_overflow(reinterpret_cast<uintptr_t>(end),
186*043036a2SApple OSS Distributions 		    reinterpret_cast<uintptr_t>(begin),
187*043036a2SApple OSS Distributions 		    &count_))) {
188*043036a2SApple OSS Distributions 			TrappingPolicy::trap("The range of valid memory is too large to be represented "
189*043036a2SApple OSS Distributions 			    "by this type, or [begin, end) is not a well-formed range");
190*043036a2SApple OSS Distributions 		}
191*043036a2SApple OSS Distributions 
192*043036a2SApple OSS Distributions 		// Store (pointer - begin) into offset_, making sure we don't overflow.
193*043036a2SApple OSS Distributions 		// Note that offset_ can be negative if `pointer` is outside of the
194*043036a2SApple OSS Distributions 		// range delimited by [begin, end), which can be valid if it represents
195*043036a2SApple OSS Distributions 		// e.g. a subrange of an array.
196*043036a2SApple OSS Distributions 		if (__improbable(os_sub_overflow(reinterpret_cast<uintptr_t>(pointer),
197*043036a2SApple OSS Distributions 		    reinterpret_cast<uintptr_t>(begin),
198*043036a2SApple OSS Distributions 		    &offset_))) {
199*043036a2SApple OSS Distributions 			TrappingPolicy::trap("The offset of the pointer inside its valid memory "
200*043036a2SApple OSS Distributions 			    "range can't be represented using int32_t");
201*043036a2SApple OSS Distributions 		}
202*043036a2SApple OSS Distributions 	}
203*043036a2SApple OSS Distributions 
204*043036a2SApple OSS Distributions 	// Creates a `bounded_ptr` to a type `T` from a `bounded_ptr` to a type `U`.
205*043036a2SApple OSS Distributions 	//
206*043036a2SApple OSS Distributions 	// This converting constructor is enabled whenever `U*` is implicitly
207*043036a2SApple OSS Distributions 	// convertible to `T*`. This allows the usual implicit conversions
208*043036a2SApple OSS Distributions 	// between base-and-derived types, and also from any type `U*` to a
209*043036a2SApple OSS Distributions 	// `void*`. If other casts (like between unrelated pointer types) are
210*043036a2SApple OSS Distributions 	// desired, `libkern::reinterpret_pointer_cast` can be used instead.
211*043036a2SApple OSS Distributions 	//
212*043036a2SApple OSS Distributions 	// The bounds on the resulting `bounded_ptr` are inherited from the
213*043036a2SApple OSS Distributions 	// original `bounded_ptr`.
214*043036a2SApple OSS Distributions 	template <typename U, typename Policy, typename = detail::enable_if_t<detail::is_convertible_v<U*, T*> > >
215*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE
216*043036a2SApple OSS Distributions 	bounded_ptr(bounded_ptr<U, Policy> const & other)
217*043036a2SApple OSS Distributions 		: base_(other.base_)
218*043036a2SApple OSS Distributions 		, count_(other.count_)
219*043036a2SApple OSS Distributions 		, offset_(static_cast<int32_t>(reinterpret_cast<CharType*>(static_cast<T*>(other.get_ptr_())) - other.base_))
220*043036a2SApple OSS Distributions 	{
221*043036a2SApple OSS Distributions 	}
222*043036a2SApple OSS Distributions 
223*043036a2SApple OSS Distributions 	// Assigns a `bounded_ptr` to a type `U` to a `bounded_ptr` to a type `T`,
224*043036a2SApple OSS Distributions 	// as long as `U*` is convertible to `T*`.
225*043036a2SApple OSS Distributions 	//
226*043036a2SApple OSS Distributions 	// This is a rebinding operation, like assignment between raw pointers,
227*043036a2SApple OSS Distributions 	// and the destination `bounded_ptr` will inherit the bounds of the
228*043036a2SApple OSS Distributions 	// source `bounded_ptr`.
229*043036a2SApple OSS Distributions 	template <typename U, typename Policy, typename = detail::enable_if_t<detail::is_convertible_v<U*, T*> > >
230*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr&
231*043036a2SApple OSS Distributions 	operator=(bounded_ptr<U, Policy> const& other)
232*043036a2SApple OSS Distributions 	{
233*043036a2SApple OSS Distributions 		base_ = other.base_;
234*043036a2SApple OSS Distributions 		count_ = other.count_;
235*043036a2SApple OSS Distributions 		offset_ = static_cast<int32_t>(reinterpret_cast<CharType*>(static_cast<T*>(other.get_ptr_())) - other.base_);
236*043036a2SApple OSS Distributions 		return *this;
237*043036a2SApple OSS Distributions 	}
238*043036a2SApple OSS Distributions 
239*043036a2SApple OSS Distributions 	// Sets a `bounded_ptr` to null.
240*043036a2SApple OSS Distributions 	//
241*043036a2SApple OSS Distributions 	// This is effectively equivalent to assigning a default-constructed
242*043036a2SApple OSS Distributions 	// `bounded_ptr` to the target. As a result, the original bounds of
243*043036a2SApple OSS Distributions 	// the `bounded_ptr` are discarded, and the resulting `bounded_ptr`
244*043036a2SApple OSS Distributions 	// is both out-of-bounds and also has no bounds assigned to it (like
245*043036a2SApple OSS Distributions 	// a default-constructed `bounded_ptr`).
246*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr&
247*043036a2SApple OSS Distributions 	operator=(detail::nullptr_t)
248*043036a2SApple OSS Distributions 	{
249*043036a2SApple OSS Distributions 		*this = bounded_ptr();
250*043036a2SApple OSS Distributions 		return *this;
251*043036a2SApple OSS Distributions 	}
252*043036a2SApple OSS Distributions 
253*043036a2SApple OSS Distributions 	// Returns a reference to the object pointed-to by the `bounded_ptr`.
254*043036a2SApple OSS Distributions 	//
255*043036a2SApple OSS Distributions 	// Traps if the pointer is pointing outside of its bounds.
256*043036a2SApple OSS Distributions 	//
257*043036a2SApple OSS Distributions 	// Also note that this function will trap when dereferencing a null
258*043036a2SApple OSS Distributions 	// `bounded_ptr`, unless the bounds of the pointer have been set and
259*043036a2SApple OSS Distributions 	// include address 0, in which case there's effectively nothing to
260*043036a2SApple OSS Distributions 	// diagnose.
261*043036a2SApple OSS Distributions 	template <typename T_ = T> // delay instantiation to avoid forming invalid ref for bounded_ptr<void>
262*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE T_&
263*043036a2SApple OSS Distributions 	operator*() const
264*043036a2SApple OSS Distributions 	{
265*043036a2SApple OSS Distributions 		if (__improbable(!in_bounds_())) {
266*043036a2SApple OSS Distributions 			TrappingPolicy::trap("bounded_ptr<T>::operator*: Dereferencing this pointer "
267*043036a2SApple OSS Distributions 			    "would access memory outside of the bounds set originally");
268*043036a2SApple OSS Distributions 		}
269*043036a2SApple OSS Distributions 		return *get_ptr_();
270*043036a2SApple OSS Distributions 	}
271*043036a2SApple OSS Distributions 
272*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE T*
273*043036a2SApple OSS Distributions 	operator->() const
274*043036a2SApple OSS Distributions 	{
275*043036a2SApple OSS Distributions 		if (__improbable(!in_bounds_())) {
276*043036a2SApple OSS Distributions 			TrappingPolicy::trap("bounded_ptr<T>::operator->: Accessing a member through this pointer "
277*043036a2SApple OSS Distributions 			    "would access memory outside of the bounds set originally");
278*043036a2SApple OSS Distributions 		}
279*043036a2SApple OSS Distributions 		return get_ptr_();
280*043036a2SApple OSS Distributions 	}
281*043036a2SApple OSS Distributions 
282*043036a2SApple OSS Distributions 	// Provides access to the n-th element past the given pointer.
283*043036a2SApple OSS Distributions 	//
284*043036a2SApple OSS Distributions 	// The `bounded_ptr` validates whether the provided index is within the
285*043036a2SApple OSS Distributions 	// bounds of the `bounded_ptr`. Like for raw pointers, a negative index
286*043036a2SApple OSS Distributions 	// may be passed, in which case the pointer is accessed at a negative
287*043036a2SApple OSS Distributions 	// offset (which must still be in bounds).
288*043036a2SApple OSS Distributions 	template <typename T_ = T> // delay instantiation to avoid forming invalid ref for bounded_ptr<void>
289*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE T_&
290*043036a2SApple OSS Distributions 	operator[](ptrdiff_t n) const
291*043036a2SApple OSS Distributions 	{
292*043036a2SApple OSS Distributions 		return *(*this + n);
293*043036a2SApple OSS Distributions 	}
294*043036a2SApple OSS Distributions 
295*043036a2SApple OSS Distributions 	// Converts a `bounded_ptr` to a raw pointer, after checking it is within
296*043036a2SApple OSS Distributions 	// its bounds.
297*043036a2SApple OSS Distributions 	//
298*043036a2SApple OSS Distributions 	// The primary intended usage of this function is to aid bridging between
299*043036a2SApple OSS Distributions 	// code that uses `bounded_ptr`s and code that does not.
300*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE T*
301*043036a2SApple OSS Distributions 	discard_bounds() const
302*043036a2SApple OSS Distributions 	{
303*043036a2SApple OSS Distributions 		if (__improbable(!in_bounds_())) {
304*043036a2SApple OSS Distributions 			TrappingPolicy::trap("bounded_ptr<T>::discard_bounds: Discarding the bounds on "
305*043036a2SApple OSS Distributions 			    "this pointer would lose the fact that it is outside of the "
306*043036a2SApple OSS Distributions 			    "bounds set originally");
307*043036a2SApple OSS Distributions 		}
308*043036a2SApple OSS Distributions 		return get_ptr_();
309*043036a2SApple OSS Distributions 	}
310*043036a2SApple OSS Distributions 
311*043036a2SApple OSS Distributions 	// Converts a `bounded_ptr` to a raw pointer, without checking whether the
312*043036a2SApple OSS Distributions 	// pointer is within its bounds.
313*043036a2SApple OSS Distributions 	//
314*043036a2SApple OSS Distributions 	// Like `discard_bounds()`, the primary intended usage of this function
315*043036a2SApple OSS Distributions 	// is to aid bridging between code that uses `bounded_ptr`s and code that
316*043036a2SApple OSS Distributions 	// does not. However, unlike `discard_bounds()`, this function does not
317*043036a2SApple OSS Distributions 	// validate that the returned pointer is in bounds. This functionality is
318*043036a2SApple OSS Distributions 	// necessary when the pointer represents something that can't be
319*043036a2SApple OSS Distributions 	// dereferenced (hence it's OK for it to be out-of-bounds), but that
320*043036a2SApple OSS Distributions 	// is still useful for other purposes like comparing against other
321*043036a2SApple OSS Distributions 	// pointers. An example of that is the `end` pointer in a half-open
322*043036a2SApple OSS Distributions 	// interval `[begin, end)`, where the `end` pointer is out-of-bounds and
323*043036a2SApple OSS Distributions 	// can't be dereferenced, yet it's still useful to delimit the range.
324*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE T*
325*043036a2SApple OSS Distributions 	unsafe_discard_bounds() const
326*043036a2SApple OSS Distributions 	{
327*043036a2SApple OSS Distributions 		return get_ptr_();
328*043036a2SApple OSS Distributions 	}
329*043036a2SApple OSS Distributions 
330*043036a2SApple OSS Distributions 	// Implicit conversion to bool, returning whether the pointer is null.
331*043036a2SApple OSS Distributions 	//
332*043036a2SApple OSS Distributions 	// This operation does not perform any validation of the bounds.
333*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE explicit
334*043036a2SApple OSS Distributions 	operator bool() const
335*043036a2SApple OSS Distributions 	{
336*043036a2SApple OSS Distributions 		return get_ptr_() != nullptr;
337*043036a2SApple OSS Distributions 	}
338*043036a2SApple OSS Distributions 
339*043036a2SApple OSS Distributions 	// Increment/decrement a `bounded_ptr`.
340*043036a2SApple OSS Distributions 	//
341*043036a2SApple OSS Distributions 	// Like for other arithmetic operations, this does not check whether the
342*043036a2SApple OSS Distributions 	// increment or decrement operation results in an out-of-bounds pointer.
343*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr&
344*043036a2SApple OSS Distributions 	operator++()
345*043036a2SApple OSS Distributions 	{
346*043036a2SApple OSS Distributions 		*this += 1;
347*043036a2SApple OSS Distributions 		return *this;
348*043036a2SApple OSS Distributions 	}
349*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr
350*043036a2SApple OSS Distributions 	operator++(int)
351*043036a2SApple OSS Distributions 	{
352*043036a2SApple OSS Distributions 		bounded_ptr old = *this;
353*043036a2SApple OSS Distributions 		++*this;
354*043036a2SApple OSS Distributions 		return old;
355*043036a2SApple OSS Distributions 	}
356*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr&
357*043036a2SApple OSS Distributions 	operator--()
358*043036a2SApple OSS Distributions 	{
359*043036a2SApple OSS Distributions 		*this -= 1;
360*043036a2SApple OSS Distributions 		return *this;
361*043036a2SApple OSS Distributions 	}
362*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr
363*043036a2SApple OSS Distributions 	operator--(int)
364*043036a2SApple OSS Distributions 	{
365*043036a2SApple OSS Distributions 		bounded_ptr old = *this;
366*043036a2SApple OSS Distributions 		--*this;
367*043036a2SApple OSS Distributions 		return old;
368*043036a2SApple OSS Distributions 	}
369*043036a2SApple OSS Distributions 
370*043036a2SApple OSS Distributions 	// Increment or decrement a `bounded_ptr` by a given offset.
371*043036a2SApple OSS Distributions 	//
372*043036a2SApple OSS Distributions 	// This is equivalent to adding the given offset to the underlying raw
373*043036a2SApple OSS Distributions 	// pointer. In particular, the bounds of the `bounded_ptr` are left
374*043036a2SApple OSS Distributions 	// untouched by this operation. Furthermore, like for raw pointers, it
375*043036a2SApple OSS Distributions 	// is possible to provide a negative offset, which will have the effect
376*043036a2SApple OSS Distributions 	// of decrementing the `bounded_ptr` instead of incrementing it.
377*043036a2SApple OSS Distributions 	//
378*043036a2SApple OSS Distributions 	// Also note that the offset is NOT a number of bytes -- just like for
379*043036a2SApple OSS Distributions 	// raw pointers, it is a number of "positions" to move the pointer from,
380*043036a2SApple OSS Distributions 	// which essentially means `n * sizeof(T)` bytes. Again, this works exactly
381*043036a2SApple OSS Distributions 	// the same as a raw pointer to an object of type `T`.
382*043036a2SApple OSS Distributions 	//
383*043036a2SApple OSS Distributions 	// Like other arithmetic operations, this does not check whether the
384*043036a2SApple OSS Distributions 	// increment or decrement operation results in an out-of-bounds pointer.
385*043036a2SApple OSS Distributions 	// However, this does check whether the arithmetic operation would result
386*043036a2SApple OSS Distributions 	// in an overflow, in which case the operation will trap.
387*043036a2SApple OSS Distributions 	template <typename T_ = T>
388*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr&
389*043036a2SApple OSS Distributions 	operator+=(ptrdiff_t n)
390*043036a2SApple OSS Distributions 	{
391*043036a2SApple OSS Distributions 		static_assert(!detail::is_void_v<T_>, "Arithmetic on bounded_ptr<void> is not allowed.");
392*043036a2SApple OSS Distributions 
393*043036a2SApple OSS Distributions 		ptrdiff_t bytes;
394*043036a2SApple OSS Distributions 		if (__improbable(os_mul_overflow(n, sizeof(T), &bytes))) {
395*043036a2SApple OSS Distributions 			TrappingPolicy::trap(
396*043036a2SApple OSS Distributions 				"bounded_ptr<T>::operator+=(n): Calculating the number of bytes to "
397*043036a2SApple OSS Distributions 				"add to the offset (n * sizeof(T)) would trigger an overflow");
398*043036a2SApple OSS Distributions 		}
399*043036a2SApple OSS Distributions 		if (__improbable(os_add_overflow(offset_, bytes, &offset_))) {
400*043036a2SApple OSS Distributions 			TrappingPolicy::trap(
401*043036a2SApple OSS Distributions 				"bounded_ptr<T>::operator+=(n): Adding the specified number of bytes "
402*043036a2SApple OSS Distributions 				"to the offset representing the current position would overflow.");
403*043036a2SApple OSS Distributions 		}
404*043036a2SApple OSS Distributions 		return *this;
405*043036a2SApple OSS Distributions 	}
406*043036a2SApple OSS Distributions 
407*043036a2SApple OSS Distributions 	template <typename T_ = T>
408*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bounded_ptr&
409*043036a2SApple OSS Distributions 	operator-=(ptrdiff_t n)
410*043036a2SApple OSS Distributions 	{
411*043036a2SApple OSS Distributions 		static_assert(!detail::is_void_v<T_>, "Arithmetic on bounded_ptr<void> is not allowed.");
412*043036a2SApple OSS Distributions 
413*043036a2SApple OSS Distributions 		ptrdiff_t bytes;
414*043036a2SApple OSS Distributions 		if (__improbable(os_mul_overflow(n, sizeof(T), &bytes))) {
415*043036a2SApple OSS Distributions 			TrappingPolicy::trap(
416*043036a2SApple OSS Distributions 				"bounded_ptr<T>::operator-=(n): Calculating the number of bytes to "
417*043036a2SApple OSS Distributions 				"subtract from the offset (n * sizeof(T)) would trigger an overflow");
418*043036a2SApple OSS Distributions 		}
419*043036a2SApple OSS Distributions 		if (__improbable(os_sub_overflow(offset_, bytes, &offset_))) {
420*043036a2SApple OSS Distributions 			TrappingPolicy::trap(
421*043036a2SApple OSS Distributions 				"bounded_ptr<T>::operator-=(n): Subtracting the specified number of bytes "
422*043036a2SApple OSS Distributions 				"from the offset representing the current position would overflow.");
423*043036a2SApple OSS Distributions 		}
424*043036a2SApple OSS Distributions 		return *this;
425*043036a2SApple OSS Distributions 	}
426*043036a2SApple OSS Distributions 
427*043036a2SApple OSS Distributions 	friend OS_ALWAYS_INLINE bounded_ptr
428*043036a2SApple OSS Distributions 	operator+(bounded_ptr p, ptrdiff_t n)
429*043036a2SApple OSS Distributions 	{
430*043036a2SApple OSS Distributions 		p += n;
431*043036a2SApple OSS Distributions 		return p;
432*043036a2SApple OSS Distributions 	}
433*043036a2SApple OSS Distributions 	friend OS_ALWAYS_INLINE bounded_ptr
434*043036a2SApple OSS Distributions 	operator+(ptrdiff_t n, bounded_ptr p)
435*043036a2SApple OSS Distributions 	{
436*043036a2SApple OSS Distributions 		p += n;
437*043036a2SApple OSS Distributions 		return p;
438*043036a2SApple OSS Distributions 	}
439*043036a2SApple OSS Distributions 	friend OS_ALWAYS_INLINE bounded_ptr
440*043036a2SApple OSS Distributions 	operator-(bounded_ptr p, ptrdiff_t n)
441*043036a2SApple OSS Distributions 	{
442*043036a2SApple OSS Distributions 		p -= n;
443*043036a2SApple OSS Distributions 		return p;
444*043036a2SApple OSS Distributions 	}
445*043036a2SApple OSS Distributions 
446*043036a2SApple OSS Distributions 	// Returns the difference between two `bounded_ptr`s.
447*043036a2SApple OSS Distributions 	//
448*043036a2SApple OSS Distributions 	// This is semantically equivalent to subtracting the two underlying
449*043036a2SApple OSS Distributions 	// pointers. The bounds of the pointers are not validated by this
450*043036a2SApple OSS Distributions 	// operation.
451*043036a2SApple OSS Distributions 	friend OS_ALWAYS_INLINE ptrdiff_t
452*043036a2SApple OSS Distributions 	operator-(bounded_ptr const& a, bounded_ptr const& b)
453*043036a2SApple OSS Distributions 	{
454*043036a2SApple OSS Distributions 		return a.get_ptr_() - b.get_ptr_();
455*043036a2SApple OSS Distributions 	}
456*043036a2SApple OSS Distributions 
457*043036a2SApple OSS Distributions 	friend OS_ALWAYS_INLINE ptrdiff_t
458*043036a2SApple OSS Distributions 	operator-(bounded_ptr const& a, T const* b)
459*043036a2SApple OSS Distributions 	{
460*043036a2SApple OSS Distributions 		return a.get_ptr_() - b;
461*043036a2SApple OSS Distributions 	}
462*043036a2SApple OSS Distributions 
463*043036a2SApple OSS Distributions 	friend OS_ALWAYS_INLINE ptrdiff_t
464*043036a2SApple OSS Distributions 	operator-(T const* a, bounded_ptr const& b)
465*043036a2SApple OSS Distributions 	{
466*043036a2SApple OSS Distributions 		return a - b.get_ptr_();
467*043036a2SApple OSS Distributions 	}
468*043036a2SApple OSS Distributions 
469*043036a2SApple OSS Distributions private:
470*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE bool
471*043036a2SApple OSS Distributions 	in_bounds_() const
472*043036a2SApple OSS Distributions 	{
473*043036a2SApple OSS Distributions 		static_assert(detail::sizeof_v<T> <= UINT32_MAX - INT32_MAX,
474*043036a2SApple OSS Distributions 		    "The type pointed-to by bounded_ptr is too large, which would defeat "
475*043036a2SApple OSS Distributions 		    "our optimization to check for inboundedness using arithmetic on unsigned");
476*043036a2SApple OSS Distributions 		return offset_ >= 0 && static_cast<uint32_t>(offset_) + static_cast<uint32_t>(detail::sizeof_v<T>) <= count_;
477*043036a2SApple OSS Distributions 	}
478*043036a2SApple OSS Distributions 
479*043036a2SApple OSS Distributions 	OS_ALWAYS_INLINE T*
480*043036a2SApple OSS Distributions 	get_ptr_() const
481*043036a2SApple OSS Distributions 	{
482*043036a2SApple OSS Distributions 		// Compute `base_ + offset_`, catching overflows.
483*043036a2SApple OSS Distributions 		uintptr_t ptr;
484*043036a2SApple OSS Distributions 		if (__improbable(os_add_overflow(reinterpret_cast<uintptr_t>(base_), offset_, &ptr))) {
485*043036a2SApple OSS Distributions 			TrappingPolicy::trap("This bounded_ptr is pointing to memory outside of what can "
486*043036a2SApple OSS Distributions 			    "be represented by a native pointer.");
487*043036a2SApple OSS Distributions 		}
488*043036a2SApple OSS Distributions 		return reinterpret_cast<T*>(ptr);
489*043036a2SApple OSS Distributions 	}
490*043036a2SApple OSS Distributions 
491*043036a2SApple OSS Distributions 	template <typename T_, typename U, typename Policy>
492*043036a2SApple OSS Distributions 	friend bounded_ptr<T_, Policy> reinterpret_pointer_cast(bounded_ptr<U, Policy> const&) noexcept;
493*043036a2SApple OSS Distributions 
494*043036a2SApple OSS Distributions 	template <typename U, typename P> friend struct bounded_ptr; // for cross-type operations and conversions
495*043036a2SApple OSS Distributions 
496*043036a2SApple OSS Distributions 	CharType* base_; // pointer to the beginning of the valid address range
497*043036a2SApple OSS Distributions 	uint32_t count_; // number of bytes considered in-bounds (non-negative)
498*043036a2SApple OSS Distributions 	int32_t offset_; // current offset into the range, in bytes
499*043036a2SApple OSS Distributions };
500*043036a2SApple OSS Distributions 
501*043036a2SApple OSS Distributions // Returns whether two `bounded_ptr`s point to the same object.
502*043036a2SApple OSS Distributions //
503*043036a2SApple OSS Distributions // This comparison is semantically equivalent to comparing the underlying
504*043036a2SApple OSS Distributions // raw pointers. In particular, it doesn't validate the bounds of either
505*043036a2SApple OSS Distributions // `bounded_ptr`, nor does it compare whether the two `bounded_ptr`s have
506*043036a2SApple OSS Distributions // the same bounds.
507*043036a2SApple OSS Distributions //
508*043036a2SApple OSS Distributions // This comparison is enabled between `bounded_ptr`s whenever the two
509*043036a2SApple OSS Distributions // corresponding raw pointer types are comparable. Comparison between a
510*043036a2SApple OSS Distributions // raw pointer and a `bounded_ptr` is also allowed, so long as the
511*043036a2SApple OSS Distributions // two corresponding raw pointer types are comparable.
512*043036a2SApple OSS Distributions template <typename T, typename P1, typename U, typename P2, typename = detail::WhenComparable<T*, U*> >
513*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
514*043036a2SApple OSS Distributions operator==(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b)
515*043036a2SApple OSS Distributions {
516*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() == b.unsafe_discard_bounds();
517*043036a2SApple OSS Distributions }
518*043036a2SApple OSS Distributions 
519*043036a2SApple OSS Distributions template <typename T, typename P1, typename U, typename P2, typename = detail::WhenComparable<T*, U*> >
520*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
521*043036a2SApple OSS Distributions operator!=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b)
522*043036a2SApple OSS Distributions {
523*043036a2SApple OSS Distributions 	return !(a == b);
524*043036a2SApple OSS Distributions }
525*043036a2SApple OSS Distributions 
526*043036a2SApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> >
527*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
528*043036a2SApple OSS Distributions operator==(bounded_ptr<T, P> const& a, U* b)
529*043036a2SApple OSS Distributions {
530*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() == b;
531*043036a2SApple OSS Distributions }
532*043036a2SApple OSS Distributions 
533*043036a2SApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> >
534*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
535*043036a2SApple OSS Distributions operator==(U* a, bounded_ptr<T, P> const& b)
536*043036a2SApple OSS Distributions {
537*043036a2SApple OSS Distributions 	return a == b.unsafe_discard_bounds();
538*043036a2SApple OSS Distributions }
539*043036a2SApple OSS Distributions 
540*043036a2SApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> >
541*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
542*043036a2SApple OSS Distributions operator!=(bounded_ptr<T, P> const& a, U* b)
543*043036a2SApple OSS Distributions {
544*043036a2SApple OSS Distributions 	return !(a == b);
545*043036a2SApple OSS Distributions }
546*043036a2SApple OSS Distributions 
547*043036a2SApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> >
548*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
549*043036a2SApple OSS Distributions operator!=(U* a, bounded_ptr<T, P> const& b)
550*043036a2SApple OSS Distributions {
551*043036a2SApple OSS Distributions 	return !(a == b);
552*043036a2SApple OSS Distributions }
553*043036a2SApple OSS Distributions 
554*043036a2SApple OSS Distributions template <typename T, typename Policy>
555*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
556*043036a2SApple OSS Distributions operator==(detail::nullptr_t, bounded_ptr<T, Policy> const& p)
557*043036a2SApple OSS Distributions {
558*043036a2SApple OSS Distributions 	return p.unsafe_discard_bounds() == nullptr;
559*043036a2SApple OSS Distributions }
560*043036a2SApple OSS Distributions 
561*043036a2SApple OSS Distributions template <typename T, typename Policy>
562*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
563*043036a2SApple OSS Distributions operator!=(detail::nullptr_t, bounded_ptr<T, Policy> const& p)
564*043036a2SApple OSS Distributions {
565*043036a2SApple OSS Distributions 	return p.unsafe_discard_bounds() != nullptr;
566*043036a2SApple OSS Distributions }
567*043036a2SApple OSS Distributions 
568*043036a2SApple OSS Distributions template <typename T, typename Policy>
569*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
570*043036a2SApple OSS Distributions operator==(bounded_ptr<T, Policy> const& p, detail::nullptr_t)
571*043036a2SApple OSS Distributions {
572*043036a2SApple OSS Distributions 	return p.unsafe_discard_bounds() == nullptr;
573*043036a2SApple OSS Distributions }
574*043036a2SApple OSS Distributions 
575*043036a2SApple OSS Distributions template <typename T, typename Policy>
576*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
577*043036a2SApple OSS Distributions operator!=(bounded_ptr<T, Policy> const& p, detail::nullptr_t)
578*043036a2SApple OSS Distributions {
579*043036a2SApple OSS Distributions 	return p.unsafe_discard_bounds() != nullptr;
580*043036a2SApple OSS Distributions }
581*043036a2SApple OSS Distributions 
582*043036a2SApple OSS Distributions // Returns whether a `bounded_ptr` points to an address that is {less-than,
583*043036a2SApple OSS Distributions // less-than-or-equal-to, greater-than, greater-than-or-equal-to} the address
584*043036a2SApple OSS Distributions // held in another `bounded_ptr`.
585*043036a2SApple OSS Distributions //
586*043036a2SApple OSS Distributions // This doesn't validate the bounds of either `bounded_ptr`, nor does it
587*043036a2SApple OSS Distributions // compare those bounds to determine the ordering result. This ordering is
588*043036a2SApple OSS Distributions // semantically equivalent to ordering the result of calling `get()` on both
589*043036a2SApple OSS Distributions // `bounded_ptr`s.
590*043036a2SApple OSS Distributions //
591*043036a2SApple OSS Distributions // This ordering is enabled between `bounded_ptr`s whenever the two
592*043036a2SApple OSS Distributions // corresponding raw pointer types are orderable. Ordering between a
593*043036a2SApple OSS Distributions // raw pointer and a `bounded_ptr` is also allowed, so long as the
594*043036a2SApple OSS Distributions // two corresponding raw pointer types are orderable.
595*043036a2SApple OSS Distributions //
596*043036a2SApple OSS Distributions 
597*043036a2SApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> >
598*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
599*043036a2SApple OSS Distributions operator<(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b)
600*043036a2SApple OSS Distributions {
601*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() < b.unsafe_discard_bounds();
602*043036a2SApple OSS Distributions }
603*043036a2SApple OSS Distributions 
604*043036a2SApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> >
605*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
606*043036a2SApple OSS Distributions operator<=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b)
607*043036a2SApple OSS Distributions {
608*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() <= b.unsafe_discard_bounds();
609*043036a2SApple OSS Distributions }
610*043036a2SApple OSS Distributions 
611*043036a2SApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> >
612*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
613*043036a2SApple OSS Distributions operator>(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b)
614*043036a2SApple OSS Distributions {
615*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() > b.unsafe_discard_bounds();
616*043036a2SApple OSS Distributions }
617*043036a2SApple OSS Distributions 
618*043036a2SApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> >
619*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
620*043036a2SApple OSS Distributions operator>=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b)
621*043036a2SApple OSS Distributions {
622*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() >= b.unsafe_discard_bounds();
623*043036a2SApple OSS Distributions }
624*043036a2SApple OSS Distributions 
625*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
626*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
627*043036a2SApple OSS Distributions operator<(T* a, bounded_ptr<U, P> const& b)
628*043036a2SApple OSS Distributions {
629*043036a2SApple OSS Distributions 	return a < b.unsafe_discard_bounds();
630*043036a2SApple OSS Distributions }
631*043036a2SApple OSS Distributions 
632*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
633*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
634*043036a2SApple OSS Distributions operator<(bounded_ptr<T, P> const& a, U* b)
635*043036a2SApple OSS Distributions {
636*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() < b;
637*043036a2SApple OSS Distributions }
638*043036a2SApple OSS Distributions 
639*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
640*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
641*043036a2SApple OSS Distributions operator<=(T* a, bounded_ptr<U, P> const& b)
642*043036a2SApple OSS Distributions {
643*043036a2SApple OSS Distributions 	return a <= b.unsafe_discard_bounds();
644*043036a2SApple OSS Distributions }
645*043036a2SApple OSS Distributions 
646*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
647*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
648*043036a2SApple OSS Distributions operator<=(bounded_ptr<T, P> const& a, U* b)
649*043036a2SApple OSS Distributions {
650*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() <= b;
651*043036a2SApple OSS Distributions }
652*043036a2SApple OSS Distributions 
653*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
654*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
655*043036a2SApple OSS Distributions operator>(T* a, bounded_ptr<U, P> const& b)
656*043036a2SApple OSS Distributions {
657*043036a2SApple OSS Distributions 	return a > b.unsafe_discard_bounds();
658*043036a2SApple OSS Distributions }
659*043036a2SApple OSS Distributions 
660*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
661*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
662*043036a2SApple OSS Distributions operator>(bounded_ptr<T, P> const& a, U* b)
663*043036a2SApple OSS Distributions {
664*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() > b;
665*043036a2SApple OSS Distributions }
666*043036a2SApple OSS Distributions 
667*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
668*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
669*043036a2SApple OSS Distributions operator>=(T* a, bounded_ptr<U, P> const& b)
670*043036a2SApple OSS Distributions {
671*043036a2SApple OSS Distributions 	return a >= b.unsafe_discard_bounds();
672*043036a2SApple OSS Distributions }
673*043036a2SApple OSS Distributions 
674*043036a2SApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> >
675*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bool
676*043036a2SApple OSS Distributions operator>=(bounded_ptr<T, P> const& a, U* b)
677*043036a2SApple OSS Distributions {
678*043036a2SApple OSS Distributions 	return a.unsafe_discard_bounds() >= b;
679*043036a2SApple OSS Distributions }
680*043036a2SApple OSS Distributions 
681*043036a2SApple OSS Distributions template <typename T, typename U>
682*043036a2SApple OSS Distributions OS_ALWAYS_INLINE T*
683*043036a2SApple OSS Distributions reinterpret_pointer_cast(U* p) noexcept
684*043036a2SApple OSS Distributions {
685*043036a2SApple OSS Distributions 	return reinterpret_cast<T*>(p);
686*043036a2SApple OSS Distributions }
687*043036a2SApple OSS Distributions 
688*043036a2SApple OSS Distributions // Reinterprets a `bounded_ptr` to a type `T` to a `bounded_ptr` to a type `U`.
689*043036a2SApple OSS Distributions //
690*043036a2SApple OSS Distributions // This is equivalent to `reinterpret_cast`ing the underlying pointer as well
691*043036a2SApple OSS Distributions // as the bounds of the original pointer. Like for a raw `reinterpret_cast`,
692*043036a2SApple OSS Distributions // no offset adjustment is performed (even if needed, e.g. for derived-to-base
693*043036a2SApple OSS Distributions // casts with multiple inheritance). Because this is extremely unsafe, it should
694*043036a2SApple OSS Distributions // be used extremely sparingly.
695*043036a2SApple OSS Distributions template <typename T, typename U, typename Policy>
696*043036a2SApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr<T, Policy>
697*043036a2SApple OSS Distributions reinterpret_pointer_cast(bounded_ptr<U, Policy> const& p) noexcept
698*043036a2SApple OSS Distributions {
699*043036a2SApple OSS Distributions 	using CharType = detail::copy_cv_t<T, char>;
700*043036a2SApple OSS Distributions 	CharType* new_begin = reinterpret_cast<CharType*>(p.base_);
701*043036a2SApple OSS Distributions 	CharType* new_end = new_begin + p.count_;
702*043036a2SApple OSS Distributions 	return bounded_ptr<T, Policy>(reinterpret_cast<T*>(p.get_ptr_()),
703*043036a2SApple OSS Distributions 	           reinterpret_cast<T const*>(new_begin),
704*043036a2SApple OSS Distributions 	           reinterpret_cast<T const*>(new_end));
705*043036a2SApple OSS Distributions }
706*043036a2SApple OSS Distributions } // end namespace libkern
707*043036a2SApple OSS Distributions 
708*043036a2SApple OSS Distributions #endif /* !TAPI */
709*043036a2SApple OSS Distributions 
710*043036a2SApple OSS Distributions #endif // !XNU_LIBKERN_LIBKERN_CXX_BOUNDED_PTR_H
711