annotate osx/include/kj/memory.h @ 135:38d1c0e7850b

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