annotate win32-mingw/include/kj/memory.h @ 163:5cc1366da2e9

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