annotate win32-mingw/include/kj/memory.h @ 140:59a8758c56b1

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