annotate osx/include/kj/memory.h @ 169:223a55898ab9 tip default

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