annotate win64-msvc/include/kj/memory.h @ 147:45360b968bf4

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