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