annotate osx/include/kj/memory.h @ 73:02caadb7509e

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