1 | // |
2 | // Copyright (c) 2019 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_SHARED_PTR_H |
30 | #define XNU_LIBKERN_LIBKERN_CXX_OS_SHARED_PTR_H |
31 | |
32 | #include <libkern/c++/intrusive_shared_ptr.h> |
33 | #include <libkern/c++/OSMetaClass.h> |
34 | |
35 | struct intrusive_osobject_retainer { |
36 | static void |
37 | retain(OSMetaClassBase const& obj) |
38 | { |
39 | obj.retain(); |
40 | } |
41 | static void |
42 | release(OSMetaClassBase const& obj) |
43 | { |
44 | obj.release(); |
45 | } |
46 | }; |
47 | |
48 | template <typename Tag> |
49 | struct intrusive_tagged_osobject_retainer { |
50 | static void |
51 | retain(OSMetaClassBase const& obj) |
52 | { |
53 | obj.taggedRetain(OSTypeID(Tag)); |
54 | } |
55 | static void |
56 | release(OSMetaClassBase const& obj) |
57 | { |
58 | obj.taggedRelease(OSTypeID(Tag)); |
59 | } |
60 | }; |
61 | |
62 | inline constexpr auto OSNoRetain = libkern::no_retain; |
63 | inline constexpr auto OSRetain = libkern::retain; |
64 | |
65 | template <typename T> |
66 | class __attribute__((trivial_abi)) OSSharedPtr: public libkern::intrusive_shared_ptr<T, intrusive_osobject_retainer> { |
67 | using libkern::intrusive_shared_ptr<T, intrusive_osobject_retainer>::intrusive_shared_ptr; |
68 | }; |
69 | |
70 | template <typename T, typename Tag> |
71 | class __attribute__((trivial_abi)) OSTaggedSharedPtr: public libkern::intrusive_shared_ptr<T, intrusive_tagged_osobject_retainer<Tag> > { |
72 | using libkern::intrusive_shared_ptr<T, intrusive_tagged_osobject_retainer<Tag> >::intrusive_shared_ptr; |
73 | }; |
74 | |
75 | template <typename T> |
76 | OSSharedPtr<T> |
77 | OSMakeShared() |
78 | { |
79 | T* memory = OSTypeAlloc(T); |
80 | // OSTypeAlloc returns an object with a refcount of 1, so we must not |
81 | // retain when constructing the shared pointer. |
82 | return OSSharedPtr<T>(memory, OSNoRetain); |
83 | } |
84 | |
85 | template <typename Destination, typename Source> |
86 | OSSharedPtr<Destination> |
87 | OSDynamicPtrCast(OSSharedPtr<Source> const& source) |
88 | { |
89 | Destination* raw = OSDynamicCast(Destination, source.get()); |
90 | if (raw == nullptr) { |
91 | return nullptr; |
92 | } else { |
93 | OSSharedPtr<Destination> dest(raw, OSRetain); |
94 | return dest; |
95 | } |
96 | } |
97 | |
98 | template <typename Destination, typename Source> |
99 | OSSharedPtr<Destination> |
100 | OSDynamicPtrCast(OSSharedPtr<Source> && source) |
101 | { |
102 | Destination* raw = OSDynamicCast(Destination, source.get()); |
103 | if (raw == nullptr) { |
104 | return nullptr; |
105 | } else { |
106 | OSSharedPtr<Destination> dest(raw, OSNoRetain); |
107 | source.detach(); // we stole the retain! |
108 | return dest; |
109 | } |
110 | } |
111 | |
112 | template <typename Destination, typename Tag, typename Source> |
113 | OSTaggedSharedPtr<Destination, Tag> |
114 | OSDynamicPtrCast(OSTaggedSharedPtr<Source, Tag> const& source) |
115 | { |
116 | Destination* raw = OSDynamicCast(Destination, source.get()); |
117 | if (raw == nullptr) { |
118 | return nullptr; |
119 | } else { |
120 | OSTaggedSharedPtr<Destination, Tag> dest(raw, OSRetain); |
121 | return dest; |
122 | } |
123 | } |
124 | |
125 | template <typename To, typename From> |
126 | OSSharedPtr<To> |
127 | OSStaticPtrCast(OSSharedPtr<From> const& ptr) noexcept |
128 | { |
129 | return OSSharedPtr<To>(static_cast<To*>(ptr.get()), OSRetain); |
130 | } |
131 | |
132 | template <typename To, typename From> |
133 | OSSharedPtr<To> |
134 | OSStaticPtrCast(OSSharedPtr<From>&& ptr) noexcept |
135 | { |
136 | return OSSharedPtr<To>(static_cast<To*>(ptr.detach()), OSNoRetain); |
137 | } |
138 | |
139 | template <typename To, typename From> |
140 | OSSharedPtr<To> |
141 | OSConstPtrCast(OSSharedPtr<From> const& ptr) noexcept |
142 | { |
143 | return OSSharedPtr<To>(const_cast<To*>(ptr.get()), OSRetain); |
144 | } |
145 | |
146 | template <typename To, typename From> |
147 | OSSharedPtr<To> |
148 | OSConstPtrCast(OSSharedPtr<From>&& ptr) noexcept |
149 | { |
150 | return OSSharedPtr<To>(const_cast<To*>(ptr.detach()), OSNoRetain); |
151 | } |
152 | |
153 | #endif // !XNU_LIBKERN_LIBKERN_CXX_OS_SHARED_PTR_H |
154 | |