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