1*4f1223e8SApple OSS Distributions /* 2*4f1223e8SApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved. 3*4f1223e8SApple OSS Distributions * 4*4f1223e8SApple OSS Distributions * @APPLE_LICENSE_HEADER_START@ 5*4f1223e8SApple OSS Distributions * 6*4f1223e8SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*4f1223e8SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*4f1223e8SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*4f1223e8SApple OSS Distributions * compliance with the License. Please obtain a copy of the License at 10*4f1223e8SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this 11*4f1223e8SApple OSS Distributions * file. 12*4f1223e8SApple OSS Distributions * 13*4f1223e8SApple OSS Distributions * The Original Code and all software distributed under the License are 14*4f1223e8SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15*4f1223e8SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16*4f1223e8SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17*4f1223e8SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18*4f1223e8SApple OSS Distributions * Please see the License for the specific language governing rights and 19*4f1223e8SApple OSS Distributions * limitations under the License. 20*4f1223e8SApple OSS Distributions * 21*4f1223e8SApple OSS Distributions * @APPLE_LICENSE_HEADER_END@ 22*4f1223e8SApple OSS Distributions */ 23*4f1223e8SApple OSS Distributions 24*4f1223e8SApple OSS Distributions #ifndef _OS_ALLOC_UTIL_H 25*4f1223e8SApple OSS Distributions #define _OS_ALLOC_UTIL_H 26*4f1223e8SApple OSS Distributions 27*4f1223e8SApple OSS Distributions #include <sys/cdefs.h> 28*4f1223e8SApple OSS Distributions #if defined(__cplusplus) && __cplusplus >= 201103L 29*4f1223e8SApple OSS Distributions extern "C++" { 30*4f1223e8SApple OSS Distributions #include <os/cpp_util.h> 31*4f1223e8SApple OSS Distributions } 32*4f1223e8SApple OSS Distributions #endif 33*4f1223e8SApple OSS Distributions 34*4f1223e8SApple OSS Distributions /*! 35*4f1223e8SApple OSS Distributions * @macro os_is_ptr_like 36*4f1223e8SApple OSS Distributions * 37*4f1223e8SApple OSS Distributions * @abstract 38*4f1223e8SApple OSS Distributions * Tell whether the given expression resembles a pointer. 39*4f1223e8SApple OSS Distributions * 40*4f1223e8SApple OSS Distributions * @discussion 41*4f1223e8SApple OSS Distributions * When pointer bounds are enabled, only types that are actually classified 42*4f1223e8SApple OSS Distributions * as pointers will be considered pointer-like. Otherwise, any pointer-sized 43*4f1223e8SApple OSS Distributions * type will be considered pointer-like. 44*4f1223e8SApple OSS Distributions * 45*4f1223e8SApple OSS Distributions * @param P the expression to be checked 46*4f1223e8SApple OSS Distributions */ 47*4f1223e8SApple OSS Distributions #if __has_ptrcheck 48*4f1223e8SApple OSS Distributions #define os_is_ptr_like(P) (__builtin_classify_type(P) == 5) 49*4f1223e8SApple OSS Distributions #else /* __has_ptrcheck */ 50*4f1223e8SApple OSS Distributions #define os_is_ptr_like(P) (sizeof(P) == sizeof(void *)) 51*4f1223e8SApple OSS Distributions #endif /* __has_ptrcheck */ 52*4f1223e8SApple OSS Distributions 53*4f1223e8SApple OSS Distributions /*! 54*4f1223e8SApple OSS Distributions * @macro os_ptr_load_and_erase 55*4f1223e8SApple OSS Distributions * 56*4f1223e8SApple OSS Distributions * @abstract 57*4f1223e8SApple OSS Distributions * Load the value of @c elem into a temporary, set @c elem to NULL, and 58*4f1223e8SApple OSS Distributions * return the value. 59*4f1223e8SApple OSS Distributions * 60*4f1223e8SApple OSS Distributions * @param elem the pointer whose value will be taken, and which will 61*4f1223e8SApple OSS Distributions * be set to NULL. 62*4f1223e8SApple OSS Distributions */ 63*4f1223e8SApple OSS Distributions #define os_ptr_load_and_erase(elem) ({ \ 64*4f1223e8SApple OSS Distributions _Static_assert(os_is_ptr_like(elem), \ 65*4f1223e8SApple OSS Distributions "elem isn't pointer sized"); \ 66*4f1223e8SApple OSS Distributions __auto_type *__single __eptr = &(elem); \ 67*4f1223e8SApple OSS Distributions __auto_type __elem = *__eptr; \ 68*4f1223e8SApple OSS Distributions _Pragma("clang diagnostic push") \ 69*4f1223e8SApple OSS Distributions _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \ 70*4f1223e8SApple OSS Distributions *__eptr = (__typeof__(__elem))NULL; \ 71*4f1223e8SApple OSS Distributions _Pragma("clang diagnostic pop") \ 72*4f1223e8SApple OSS Distributions __elem; \ 73*4f1223e8SApple OSS Distributions }) 74*4f1223e8SApple OSS Distributions 75*4f1223e8SApple OSS Distributions /*! 76*4f1223e8SApple OSS Distributions * @macro os_get_pointee_type 77*4f1223e8SApple OSS Distributions * 78*4f1223e8SApple OSS Distributions * @abstract 79*4f1223e8SApple OSS Distributions * Get the type pointed to by @c ptr. 80*4f1223e8SApple OSS Distributions * 81*4f1223e8SApple OSS Distributions * @discussion 82*4f1223e8SApple OSS Distributions * C++ forbids dereferencing a void pointer. 83*4f1223e8SApple OSS Distributions * This macro provides an abstraction that is safe to use with any pointer, 84*4f1223e8SApple OSS Distributions * both from C, and from C++. 85*4f1223e8SApple OSS Distributions * 86*4f1223e8SApple OSS Distributions * @param ptr the pointer we want to get the pointee's type for 87*4f1223e8SApple OSS Distributions */ 88*4f1223e8SApple OSS Distributions 89*4f1223e8SApple OSS Distributions #if defined(__cplusplus) && __has_builtin(__remove_pointer) && \ 90*4f1223e8SApple OSS Distributions __has_builtin(__remove_reference_t) 91*4f1223e8SApple OSS Distributions /* 92*4f1223e8SApple OSS Distributions * The reason we're using the compiler builtins is that those are able to 93*4f1223e8SApple OSS Distributions * properly deal with __ptrauth-qualified pointers, unlike template 94*4f1223e8SApple OSS Distributions * specializations. Moreover, template specializations would require forwarding 95*4f1223e8SApple OSS Distributions * type definitions, and currently __attribute__((xnu_usage_semantics(...))) 96*4f1223e8SApple OSS Distributions * annotations are not carried over across typedefs. 97*4f1223e8SApple OSS Distributions * 98*4f1223e8SApple OSS Distributions * It is safe to assume that if the compiler does not support such builtins, 99*4f1223e8SApple OSS Distributions * it also does not implement D150875, and we can therefore safely dereference 100*4f1223e8SApple OSS Distributions * void pointers. 101*4f1223e8SApple OSS Distributions */ 102*4f1223e8SApple OSS Distributions #define os_get_pointee_type(ptr) \ 103*4f1223e8SApple OSS Distributions __remove_pointer(__remove_reference_t(__typeof__(ptr))) 104*4f1223e8SApple OSS Distributions #else 105*4f1223e8SApple OSS Distributions #define os_get_pointee_type(ptr) \ 106*4f1223e8SApple OSS Distributions _Pragma("clang diagnostic push") \ 107*4f1223e8SApple OSS Distributions _Pragma("clang diagnostic ignored \"-Wvoid-ptr-dereference\"") \ 108*4f1223e8SApple OSS Distributions __typeof__(*(ptr)) \ 109*4f1223e8SApple OSS Distributions _Pragma("clang diagnostic pop") 110*4f1223e8SApple OSS Distributions #endif 111*4f1223e8SApple OSS Distributions 112*4f1223e8SApple OSS Distributions #endif /* _OS_ALLOC_UTIL_H */ 113