annotate win64-msvc/include/kj/memory.h @ 56:af97cad61ff0

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