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