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