xref: /xnu-8020.121.3/libkern/libkern/c++/OSAllocation.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 2019-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef XNU_LIBKERN_LIBKERN_CXX_OS_ALLOCATION_H
30 #define XNU_LIBKERN_LIBKERN_CXX_OS_ALLOCATION_H
31 
32 #if !TAPI
33 
34 #include <stddef.h>
35 #if DRIVERKIT_FRAMEWORK_INCLUDE
36 #include <DriverKit/OSBoundedPtr.h>
37 #include <DriverKit/safe_allocation.h>
38 #include <DriverKit/IOLib.h> // IOMalloc/IOFree
39 #else
40 #include <libkern/c++/OSBoundedPtr.h>
41 #include <libkern/c++/safe_allocation.h>
42 #include <IOKit/IOLib.h> // IOMalloc/IOFree
43 #endif /* DRIVERKIT_FRAMEWORK_INCLUDE */
44 
45 namespace os_detail {
46 struct IOKit_allocator {
47 	static void*
allocateIOKit_allocator48 	allocate(size_t bytes)
49 	{
50 		return IOMalloc(bytes);
51 	}
52 
53 	static void*
allocate_zeroIOKit_allocator54 	allocate_zero(size_t bytes)
55 	{
56 		return IOMallocZero(bytes);
57 	}
58 
59 	static void
deallocateIOKit_allocator60 	deallocate(void* p, size_t bytes)
61 	{
62 		IOFree(p, bytes);
63 	}
64 };
65 
66 #ifdef KERNEL_PRIVATE
67 struct IOKit_data_allocator {
68 	static void*
allocateIOKit_data_allocator69 	allocate(size_t bytes)
70 	{
71 		return IOMallocData(bytes);
72 	}
73 
74 	static void*
allocate_zeroIOKit_data_allocator75 	allocate_zero(size_t bytes)
76 	{
77 		return IOMallocZeroData(bytes);
78 	}
79 
80 	static void
deallocateIOKit_data_allocator81 	deallocate(void* p, size_t bytes)
82 	{
83 		IOFreeData(p, bytes);
84 	}
85 };
86 #endif
87 } // end namespace os_detail
88 
89 template <typename T, typename Allocator = os_detail::IOKit_allocator>
90 using OSAllocation = libkern::safe_allocation<T, Allocator, os_detail::panic_trapping_policy>;
91 
92 #ifdef KERNEL_PRIVATE
93 // uses IOMalloc(Zero)Data as underlying allocator (see IOKit/IOLib.h for advantages and restrictions)
94 template <typename T>
95 using OSDataAllocation = OSAllocation<T, os_detail::IOKit_data_allocator>;
96 #endif
97 
98 inline constexpr auto OSAllocateMemory = libkern::allocate_memory;
99 inline constexpr auto OSAllocateMemoryZero = libkern::allocate_memory_zero;
100 inline constexpr auto OSAdoptMemory = libkern::adopt_memory;
101 
102 #endif /* !TAPI */
103 
104 #endif /* !XNU_LIBKERN_LIBKERN_CXX_OS_ALLOCATION_H */
105