Mercurial > hg > sv-dependency-builds
comparison osx/include/kj/memory.h @ 134:41e769c91eca
Add Capnp and KJ builds for OSX
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Tue, 25 Oct 2016 14:48:23 +0100 |
parents | |
children | 0994c39f1e94 |
comparison
equal
deleted
inserted
replaced
133:1ac99bfc383d | 134:41e769c91eca |
---|---|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors | |
2 // Licensed under the MIT License: | |
3 // | |
4 // Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 // of this software and associated documentation files (the "Software"), to deal | |
6 // in the Software without restriction, including without limitation the rights | |
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 // copies of the Software, and to permit persons to whom the Software is | |
9 // furnished to do so, subject to the following conditions: | |
10 // | |
11 // The above copyright notice and this permission notice shall be included in | |
12 // all copies or substantial portions of the Software. | |
13 // | |
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 // THE SOFTWARE. | |
21 | |
22 #ifndef KJ_MEMORY_H_ | |
23 #define KJ_MEMORY_H_ | |
24 | |
25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS | |
26 #pragma GCC system_header | |
27 #endif | |
28 | |
29 #include "common.h" | |
30 | |
31 namespace kj { | |
32 | |
33 // ======================================================================================= | |
34 // Disposer -- Implementation details. | |
35 | |
36 class Disposer { | |
37 // Abstract interface for a thing that "disposes" of objects, where "disposing" usually means | |
38 // calling the destructor followed by freeing the underlying memory. `Own<T>` encapsulates an | |
39 // object pointer with corresponding Disposer. | |
40 // | |
41 // Few developers will ever touch this interface. It is primarily useful for those implementing | |
42 // custom memory allocators. | |
43 | |
44 protected: | |
45 // Do not declare a destructor, as doing so will force a global initializer for each HeapDisposer | |
46 // instance. Eww! | |
47 | |
48 virtual void disposeImpl(void* pointer) const = 0; | |
49 // Disposes of the object, given a pointer to the beginning of the object. If the object is | |
50 // polymorphic, this pointer is determined by dynamic_cast<void*>(). For non-polymorphic types, | |
51 // Own<T> does not allow any casting, so the pointer exactly matches the original one given to | |
52 // Own<T>. | |
53 | |
54 public: | |
55 | |
56 template <typename T> | |
57 void dispose(T* object) const; | |
58 // Helper wrapper around disposeImpl(). | |
59 // | |
60 // If T is polymorphic, calls `disposeImpl(dynamic_cast<void*>(object))`, otherwise calls | |
61 // `disposeImpl(implicitCast<void*>(object))`. | |
62 // | |
63 // Callers must not call dispose() on the same pointer twice, even if the first call throws | |
64 // an exception. | |
65 | |
66 private: | |
67 template <typename T, bool polymorphic = __is_polymorphic(T)> | |
68 struct Dispose_; | |
69 }; | |
70 | |
71 template <typename T> | |
72 class DestructorOnlyDisposer: public Disposer { | |
73 // A disposer that merely calls the type's destructor and nothing else. | |
74 | |
75 public: | |
76 static const DestructorOnlyDisposer instance; | |
77 | |
78 void disposeImpl(void* pointer) const override { | |
79 reinterpret_cast<T*>(pointer)->~T(); | |
80 } | |
81 }; | |
82 | |
83 template <typename T> | |
84 const DestructorOnlyDisposer<T> DestructorOnlyDisposer<T>::instance = DestructorOnlyDisposer<T>(); | |
85 | |
86 class NullDisposer: public Disposer { | |
87 // A disposer that does nothing. | |
88 | |
89 public: | |
90 static const NullDisposer instance; | |
91 | |
92 void disposeImpl(void* pointer) const override {} | |
93 }; | |
94 | |
95 // ======================================================================================= | |
96 // Own<T> -- An owned pointer. | |
97 | |
98 template <typename T> | |
99 class Own { | |
100 // A transferrable title to a T. When an Own<T> goes out of scope, the object's Disposer is | |
101 // called to dispose of it. An Own<T> can be efficiently passed by move, without relocating the | |
102 // underlying object; this transfers ownership. | |
103 // | |
104 // This is much like std::unique_ptr, except: | |
105 // - You cannot release(). An owned object is not necessarily allocated with new (see next | |
106 // point), so it would be hard to use release() correctly. | |
107 // - The deleter is made polymorphic by virtual call rather than by template. This is much | |
108 // more powerful -- it allows the use of custom allocators, freelists, etc. This could | |
109 // _almost_ be accomplished with unique_ptr by forcing everyone to use something like | |
110 // std::unique_ptr<T, kj::Deleter>, except that things get hairy in the presence of multiple | |
111 // inheritance and upcasting, and anyway if you force everyone to use a custom deleter | |
112 // then you've lost any benefit to interoperating with the "standard" unique_ptr. | |
113 | |
114 public: | |
115 KJ_DISALLOW_COPY(Own); | |
116 inline Own(): disposer(nullptr), ptr(nullptr) {} | |
117 inline Own(Own&& other) noexcept | |
118 : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; } | |
119 inline Own(Own<RemoveConstOrDisable<T>>&& other) noexcept | |
120 : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; } | |
121 template <typename U, typename = EnableIf<canConvert<U*, T*>()>> | |
122 inline Own(Own<U>&& other) noexcept | |
123 : disposer(other.disposer), ptr(other.ptr) { | |
124 static_assert(__is_polymorphic(T), | |
125 "Casting owned pointers requires that the target type is polymorphic."); | |
126 other.ptr = nullptr; | |
127 } | |
128 inline Own(T* ptr, const Disposer& disposer) noexcept: disposer(&disposer), ptr(ptr) {} | |
129 | |
130 ~Own() noexcept(false) { dispose(); } | |
131 | |
132 inline Own& operator=(Own&& other) { | |
133 // Move-assingnment operator. | |
134 | |
135 // Careful, this might own `other`. Therefore we have to transfer the pointers first, then | |
136 // dispose. | |
137 const Disposer* disposerCopy = disposer; | |
138 T* ptrCopy = ptr; | |
139 disposer = other.disposer; | |
140 ptr = other.ptr; | |
141 other.ptr = nullptr; | |
142 if (ptrCopy != nullptr) { | |
143 disposerCopy->dispose(const_cast<RemoveConst<T>*>(ptrCopy)); | |
144 } | |
145 return *this; | |
146 } | |
147 | |
148 inline Own& operator=(decltype(nullptr)) { | |
149 dispose(); | |
150 return *this; | |
151 } | |
152 | |
153 template <typename U> | |
154 Own<U> downcast() { | |
155 // Downcast the pointer to Own<U>, destroying the original pointer. If this pointer does not | |
156 // actually point at an instance of U, the results are undefined (throws an exception in debug | |
157 // mode if RTTI is enabled, otherwise you're on your own). | |
158 | |
159 Own<U> result; | |
160 if (ptr != nullptr) { | |
161 result.ptr = &kj::downcast<U>(*ptr); | |
162 result.disposer = disposer; | |
163 ptr = nullptr; | |
164 } | |
165 return result; | |
166 } | |
167 | |
168 #define NULLCHECK KJ_IREQUIRE(ptr != nullptr, "null Own<> dereference") | |
169 inline T* operator->() { NULLCHECK; return ptr; } | |
170 inline const T* operator->() const { NULLCHECK; return ptr; } | |
171 inline T& operator*() { NULLCHECK; return *ptr; } | |
172 inline const T& operator*() const { NULLCHECK; return *ptr; } | |
173 #undef NULLCHECK | |
174 inline T* get() { return ptr; } | |
175 inline const T* get() const { return ptr; } | |
176 inline operator T*() { return ptr; } | |
177 inline operator const T*() const { return ptr; } | |
178 | |
179 private: | |
180 const Disposer* disposer; // Only valid if ptr != nullptr. | |
181 T* ptr; | |
182 | |
183 inline explicit Own(decltype(nullptr)): disposer(nullptr), ptr(nullptr) {} | |
184 | |
185 inline bool operator==(decltype(nullptr)) { return ptr == nullptr; } | |
186 inline bool operator!=(decltype(nullptr)) { return ptr != nullptr; } | |
187 // Only called by Maybe<Own<T>>. | |
188 | |
189 inline void dispose() { | |
190 // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly | |
191 // dispose again. | |
192 T* ptrCopy = ptr; | |
193 if (ptrCopy != nullptr) { | |
194 ptr = nullptr; | |
195 disposer->dispose(const_cast<RemoveConst<T>*>(ptrCopy)); | |
196 } | |
197 } | |
198 | |
199 template <typename U> | |
200 friend class Own; | |
201 friend class Maybe<Own<T>>; | |
202 }; | |
203 | |
204 namespace _ { // private | |
205 | |
206 template <typename T> | |
207 class OwnOwn { | |
208 public: | |
209 inline OwnOwn(Own<T>&& value) noexcept: value(kj::mv(value)) {} | |
210 | |
211 inline Own<T>& operator*() & { return value; } | |
212 inline const Own<T>& operator*() const & { return value; } | |
213 inline Own<T>&& operator*() && { return kj::mv(value); } | |
214 inline const Own<T>&& operator*() const && { return kj::mv(value); } | |
215 inline Own<T>* operator->() { return &value; } | |
216 inline const Own<T>* operator->() const { return &value; } | |
217 inline operator Own<T>*() { return value ? &value : nullptr; } | |
218 inline operator const Own<T>*() const { return value ? &value : nullptr; } | |
219 | |
220 private: | |
221 Own<T> value; | |
222 }; | |
223 | |
224 template <typename T> | |
225 OwnOwn<T> readMaybe(Maybe<Own<T>>&& maybe) { return OwnOwn<T>(kj::mv(maybe.ptr)); } | |
226 template <typename T> | |
227 Own<T>* readMaybe(Maybe<Own<T>>& maybe) { return maybe.ptr ? &maybe.ptr : nullptr; } | |
228 template <typename T> | |
229 const Own<T>* readMaybe(const Maybe<Own<T>>& maybe) { return maybe.ptr ? &maybe.ptr : nullptr; } | |
230 | |
231 } // namespace _ (private) | |
232 | |
233 template <typename T> | |
234 class Maybe<Own<T>> { | |
235 public: | |
236 inline Maybe(): ptr(nullptr) {} | |
237 inline Maybe(Own<T>&& t) noexcept: ptr(kj::mv(t)) {} | |
238 inline Maybe(Maybe&& other) noexcept: ptr(kj::mv(other.ptr)) {} | |
239 | |
240 template <typename U> | |
241 inline Maybe(Maybe<Own<U>>&& other): ptr(mv(other.ptr)) {} | |
242 | |
243 inline Maybe(decltype(nullptr)) noexcept: ptr(nullptr) {} | |
244 | |
245 inline operator Maybe<T&>() { return ptr.get(); } | |
246 inline operator Maybe<const T&>() const { return ptr.get(); } | |
247 | |
248 inline Maybe& operator=(Maybe&& other) { ptr = kj::mv(other.ptr); return *this; } | |
249 | |
250 inline bool operator==(decltype(nullptr)) const { return ptr == nullptr; } | |
251 inline bool operator!=(decltype(nullptr)) const { return ptr != nullptr; } | |
252 | |
253 Own<T>& orDefault(Own<T>& defaultValue) { | |
254 if (ptr == nullptr) { | |
255 return defaultValue; | |
256 } else { | |
257 return ptr; | |
258 } | |
259 } | |
260 const Own<T>& orDefault(const Own<T>& defaultValue) const { | |
261 if (ptr == nullptr) { | |
262 return defaultValue; | |
263 } else { | |
264 return ptr; | |
265 } | |
266 } | |
267 | |
268 template <typename Func> | |
269 auto map(Func&& f) & -> Maybe<decltype(f(instance<Own<T>&>()))> { | |
270 if (ptr == nullptr) { | |
271 return nullptr; | |
272 } else { | |
273 return f(ptr); | |
274 } | |
275 } | |
276 | |
277 template <typename Func> | |
278 auto map(Func&& f) const & -> Maybe<decltype(f(instance<const Own<T>&>()))> { | |
279 if (ptr == nullptr) { | |
280 return nullptr; | |
281 } else { | |
282 return f(ptr); | |
283 } | |
284 } | |
285 | |
286 template <typename Func> | |
287 auto map(Func&& f) && -> Maybe<decltype(f(instance<Own<T>&&>()))> { | |
288 if (ptr == nullptr) { | |
289 return nullptr; | |
290 } else { | |
291 return f(kj::mv(ptr)); | |
292 } | |
293 } | |
294 | |
295 template <typename Func> | |
296 auto map(Func&& f) const && -> Maybe<decltype(f(instance<const Own<T>&&>()))> { | |
297 if (ptr == nullptr) { | |
298 return nullptr; | |
299 } else { | |
300 return f(kj::mv(ptr)); | |
301 } | |
302 } | |
303 | |
304 private: | |
305 Own<T> ptr; | |
306 | |
307 template <typename U> | |
308 friend class Maybe; | |
309 template <typename U> | |
310 friend _::OwnOwn<U> _::readMaybe(Maybe<Own<U>>&& maybe); | |
311 template <typename U> | |
312 friend Own<U>* _::readMaybe(Maybe<Own<U>>& maybe); | |
313 template <typename U> | |
314 friend const Own<U>* _::readMaybe(const Maybe<Own<U>>& maybe); | |
315 }; | |
316 | |
317 namespace _ { // private | |
318 | |
319 template <typename T> | |
320 class HeapDisposer final: public Disposer { | |
321 public: | |
322 virtual void disposeImpl(void* pointer) const override { delete reinterpret_cast<T*>(pointer); } | |
323 | |
324 static const HeapDisposer instance; | |
325 }; | |
326 | |
327 template <typename T> | |
328 const HeapDisposer<T> HeapDisposer<T>::instance = HeapDisposer<T>(); | |
329 | |
330 } // namespace _ (private) | |
331 | |
332 template <typename T, typename... Params> | |
333 Own<T> heap(Params&&... params) { | |
334 // heap<T>(...) allocates a T on the heap, forwarding the parameters to its constructor. The | |
335 // exact heap implementation is unspecified -- for now it is operator new, but you should not | |
336 // assume this. (Since we know the object size at delete time, we could actually implement an | |
337 // allocator that is more efficient than operator new.) | |
338 | |
339 return Own<T>(new T(kj::fwd<Params>(params)...), _::HeapDisposer<T>::instance); | |
340 } | |
341 | |
342 template <typename T> | |
343 Own<Decay<T>> heap(T&& orig) { | |
344 // Allocate a copy (or move) of the argument on the heap. | |
345 // | |
346 // The purpose of this overload is to allow you to omit the template parameter as there is only | |
347 // one argument and the purpose is to copy it. | |
348 | |
349 typedef Decay<T> T2; | |
350 return Own<T2>(new T2(kj::fwd<T>(orig)), _::HeapDisposer<T2>::instance); | |
351 } | |
352 | |
353 // ======================================================================================= | |
354 // SpaceFor<T> -- assists in manual allocation | |
355 | |
356 template <typename T> | |
357 class SpaceFor { | |
358 // A class which has the same size and alignment as T but does not call its constructor or | |
359 // destructor automatically. Instead, call construct() to construct a T in the space, which | |
360 // returns an Own<T> which will take care of calling T's destructor later. | |
361 | |
362 public: | |
363 inline SpaceFor() {} | |
364 inline ~SpaceFor() {} | |
365 | |
366 template <typename... Params> | |
367 Own<T> construct(Params&&... params) { | |
368 ctor(value, kj::fwd<Params>(params)...); | |
369 return Own<T>(&value, DestructorOnlyDisposer<T>::instance); | |
370 } | |
371 | |
372 private: | |
373 union { | |
374 T value; | |
375 }; | |
376 }; | |
377 | |
378 // ======================================================================================= | |
379 // Inline implementation details | |
380 | |
381 template <typename T> | |
382 struct Disposer::Dispose_<T, true> { | |
383 static void dispose(T* object, const Disposer& disposer) { | |
384 // Note that dynamic_cast<void*> does not require RTTI to be enabled, because the offset to | |
385 // the top of the object is in the vtable -- as it obviously needs to be to correctly implement | |
386 // operator delete. | |
387 disposer.disposeImpl(dynamic_cast<void*>(object)); | |
388 } | |
389 }; | |
390 template <typename T> | |
391 struct Disposer::Dispose_<T, false> { | |
392 static void dispose(T* object, const Disposer& disposer) { | |
393 disposer.disposeImpl(static_cast<void*>(object)); | |
394 } | |
395 }; | |
396 | |
397 template <typename T> | |
398 void Disposer::dispose(T* object) const { | |
399 Dispose_<T>::dispose(object, *this); | |
400 } | |
401 | |
402 } // namespace kj | |
403 | |
404 #endif // KJ_MEMORY_H_ |