1*4d495c6eSApple OSS Distributions // 2*4d495c6eSApple OSS Distributions // Copyright (c) 2019 Apple, Inc. All rights reserved. 3*4d495c6eSApple OSS Distributions // 4*4d495c6eSApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*4d495c6eSApple OSS Distributions // 6*4d495c6eSApple OSS Distributions // This file contains Original Code and/or Modifications of Original Code 7*4d495c6eSApple OSS Distributions // as defined in and that are subject to the Apple Public Source License 8*4d495c6eSApple OSS Distributions // Version 2.0 (the 'License'). You may not use this file except in 9*4d495c6eSApple OSS Distributions // compliance with the License. The rights granted to you under the License 10*4d495c6eSApple OSS Distributions // may not be used to create, or enable the creation or redistribution of, 11*4d495c6eSApple OSS Distributions // unlawful or unlicensed copies of an Apple operating system, or to 12*4d495c6eSApple OSS Distributions // circumvent, violate, or enable the circumvention or violation of, any 13*4d495c6eSApple OSS Distributions // terms of an Apple operating system software license agreement. 14*4d495c6eSApple OSS Distributions // 15*4d495c6eSApple OSS Distributions // Please obtain a copy of the License at 16*4d495c6eSApple OSS Distributions // http://www.opensource.apple.com/apsl/ and read it before using this file. 17*4d495c6eSApple OSS Distributions // 18*4d495c6eSApple OSS Distributions // The Original Code and all software distributed under the License are 19*4d495c6eSApple OSS Distributions // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*4d495c6eSApple OSS Distributions // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*4d495c6eSApple OSS Distributions // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*4d495c6eSApple OSS Distributions // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*4d495c6eSApple OSS Distributions // Please see the License for the specific language governing rights and 24*4d495c6eSApple OSS Distributions // limitations under the License. 25*4d495c6eSApple OSS Distributions // 26*4d495c6eSApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*4d495c6eSApple OSS Distributions // 28*4d495c6eSApple OSS Distributions 29*4d495c6eSApple OSS Distributions #ifndef XNU_LIBKERN_LIBKERN_CXX_BOUNDED_ARRAY_H 30*4d495c6eSApple OSS Distributions #define XNU_LIBKERN_LIBKERN_CXX_BOUNDED_ARRAY_H 31*4d495c6eSApple OSS Distributions 32*4d495c6eSApple OSS Distributions #if !TAPI 33*4d495c6eSApple OSS Distributions 34*4d495c6eSApple OSS Distributions #if DRIVERKIT_FRAMEWORK_INCLUDE 35*4d495c6eSApple OSS Distributions #include <DriverKit/bounded_ptr.h> 36*4d495c6eSApple OSS Distributions #else 37*4d495c6eSApple OSS Distributions #include <libkern/c++/bounded_ptr.h> 38*4d495c6eSApple OSS Distributions #endif /* DRIVERKIT_FRAMEWORK_INCLUDE */ 39*4d495c6eSApple OSS Distributions #include <stddef.h> 40*4d495c6eSApple OSS Distributions #include <os/base.h> 41*4d495c6eSApple OSS Distributions 42*4d495c6eSApple OSS Distributions namespace libkern { 43*4d495c6eSApple OSS Distributions // `bounded_array` is a simple abstraction for a C-style array. 44*4d495c6eSApple OSS Distributions // 45*4d495c6eSApple OSS Distributions // Unlike C-style arrays, however, it ensures that the array is not accessed 46*4d495c6eSApple OSS Distributions // outside of its bounds. Furthermore, the iterators of the `bounded_array` 47*4d495c6eSApple OSS Distributions // are `bounded_ptr`, which track the range they're allowed to access. 48*4d495c6eSApple OSS Distributions // 49*4d495c6eSApple OSS Distributions // TODO: 50*4d495c6eSApple OSS Distributions // - Should we provide deep comparison operators? 51*4d495c6eSApple OSS Distributions // - Document individual methods 52*4d495c6eSApple OSS Distributions template <typename T, size_t N, typename TrappingPolicy> 53*4d495c6eSApple OSS Distributions struct bounded_array { 54*4d495c6eSApple OSS Distributions // DO NOT USE THIS MEMBER DIRECTLY OR WE WILL BREAK YOUR CODE IN THE FUTURE. 55*4d495c6eSApple OSS Distributions // THIS HAS TO BE PUBLIC FOR THIS TYPE TO SUPPORT AGGREGATE-INITIALIZATION. 56*4d495c6eSApple OSS Distributions T data_[N]; 57*4d495c6eSApple OSS Distributions 58*4d495c6eSApple OSS Distributions using iterator = bounded_ptr<T, TrappingPolicy>; 59*4d495c6eSApple OSS Distributions using const_iterator = bounded_ptr<T const, TrappingPolicy>; 60*4d495c6eSApple OSS Distributions 61*4d495c6eSApple OSS Distributions OS_ALWAYS_INLINE iterator beginbounded_array62*4d495c6eSApple OSS Distributions begin() noexcept 63*4d495c6eSApple OSS Distributions { 64*4d495c6eSApple OSS Distributions return iterator(data_, data_, data_ + N); 65*4d495c6eSApple OSS Distributions } 66*4d495c6eSApple OSS Distributions OS_ALWAYS_INLINE const_iterator beginbounded_array67*4d495c6eSApple OSS Distributions begin() const noexcept 68*4d495c6eSApple OSS Distributions { 69*4d495c6eSApple OSS Distributions return const_iterator(data_, data_, data_ + N); 70*4d495c6eSApple OSS Distributions } 71*4d495c6eSApple OSS Distributions iterator endbounded_array72*4d495c6eSApple OSS Distributions end() noexcept 73*4d495c6eSApple OSS Distributions { 74*4d495c6eSApple OSS Distributions return iterator(data_ + N, data_, data_ + N); 75*4d495c6eSApple OSS Distributions } 76*4d495c6eSApple OSS Distributions const_iterator endbounded_array77*4d495c6eSApple OSS Distributions end() const noexcept 78*4d495c6eSApple OSS Distributions { 79*4d495c6eSApple OSS Distributions return const_iterator(data_ + N, data_, data_ + N); 80*4d495c6eSApple OSS Distributions } 81*4d495c6eSApple OSS Distributions constexpr size_t sizebounded_array82*4d495c6eSApple OSS Distributions size() const noexcept 83*4d495c6eSApple OSS Distributions { 84*4d495c6eSApple OSS Distributions return N; 85*4d495c6eSApple OSS Distributions } 86*4d495c6eSApple OSS Distributions constexpr size_t lengthbounded_array87*4d495c6eSApple OSS Distributions length() const noexcept 88*4d495c6eSApple OSS Distributions { 89*4d495c6eSApple OSS Distributions return N; 90*4d495c6eSApple OSS Distributions } 91*4d495c6eSApple OSS Distributions constexpr T* databounded_array92*4d495c6eSApple OSS Distributions data() noexcept 93*4d495c6eSApple OSS Distributions { 94*4d495c6eSApple OSS Distributions return data_; 95*4d495c6eSApple OSS Distributions } 96*4d495c6eSApple OSS Distributions constexpr T const* databounded_array97*4d495c6eSApple OSS Distributions data() const noexcept 98*4d495c6eSApple OSS Distributions { 99*4d495c6eSApple OSS Distributions return data_; 100*4d495c6eSApple OSS Distributions } 101*4d495c6eSApple OSS Distributions OS_ALWAYS_INLINE T& 102*4d495c6eSApple OSS Distributions operator[](ptrdiff_t n) 103*4d495c6eSApple OSS Distributions { 104*4d495c6eSApple OSS Distributions return begin()[n]; 105*4d495c6eSApple OSS Distributions } 106*4d495c6eSApple OSS Distributions OS_ALWAYS_INLINE T const& 107*4d495c6eSApple OSS Distributions operator[](ptrdiff_t n) const 108*4d495c6eSApple OSS Distributions { 109*4d495c6eSApple OSS Distributions return begin()[n]; 110*4d495c6eSApple OSS Distributions } 111*4d495c6eSApple OSS Distributions }; 112*4d495c6eSApple OSS Distributions } // end namespace libkern 113*4d495c6eSApple OSS Distributions 114*4d495c6eSApple OSS Distributions #endif /* !TAPI */ 115*4d495c6eSApple OSS Distributions 116*4d495c6eSApple OSS Distributions #endif // !XNU_LIBKERN_LIBKERN_CXX_BOUNDED_ARRAY_H 117