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