// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #ifndef KJ_MEMORY_H_ #define KJ_MEMORY_H_ #if defined(__GNUC__) && !KJ_HEADER_WARNINGS #pragma GCC system_header #endif #include "common.h" namespace kj { // ======================================================================================= // Disposer -- Implementation details. class Disposer { // Abstract interface for a thing that "disposes" of objects, where "disposing" usually means // calling the destructor followed by freeing the underlying memory. `Own` encapsulates an // object pointer with corresponding Disposer. // // Few developers will ever touch this interface. It is primarily useful for those implementing // custom memory allocators. protected: // Do not declare a destructor, as doing so will force a global initializer for each HeapDisposer // instance. Eww! virtual void disposeImpl(void* pointer) const = 0; // Disposes of the object, given a pointer to the beginning of the object. If the object is // polymorphic, this pointer is determined by dynamic_cast(). For non-polymorphic types, // Own does not allow any casting, so the pointer exactly matches the original one given to // Own. public: template void dispose(T* object) const; // Helper wrapper around disposeImpl(). // // If T is polymorphic, calls `disposeImpl(dynamic_cast(object))`, otherwise calls // `disposeImpl(implicitCast(object))`. // // Callers must not call dispose() on the same pointer twice, even if the first call throws // an exception. private: template struct Dispose_; }; template class DestructorOnlyDisposer: public Disposer { // A disposer that merely calls the type's destructor and nothing else. public: static const DestructorOnlyDisposer instance; void disposeImpl(void* pointer) const override { reinterpret_cast(pointer)->~T(); } }; template const DestructorOnlyDisposer DestructorOnlyDisposer::instance = DestructorOnlyDisposer(); class NullDisposer: public Disposer { // A disposer that does nothing. public: static const NullDisposer instance; void disposeImpl(void* pointer) const override {} }; // ======================================================================================= // Own -- An owned pointer. template class Own { // A transferrable title to a T. When an Own goes out of scope, the object's Disposer is // called to dispose of it. An Own can be efficiently passed by move, without relocating the // underlying object; this transfers ownership. // // This is much like std::unique_ptr, except: // - You cannot release(). An owned object is not necessarily allocated with new (see next // point), so it would be hard to use release() correctly. // - The deleter is made polymorphic by virtual call rather than by template. This is much // more powerful -- it allows the use of custom allocators, freelists, etc. This could // _almost_ be accomplished with unique_ptr by forcing everyone to use something like // std::unique_ptr, except that things get hairy in the presence of multiple // inheritance and upcasting, and anyway if you force everyone to use a custom deleter // then you've lost any benefit to interoperating with the "standard" unique_ptr. public: KJ_DISALLOW_COPY(Own); inline Own(): disposer(nullptr), ptr(nullptr) {} inline Own(Own&& other) noexcept : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; } inline Own(Own>&& other) noexcept : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; } template ()>> inline Own(Own&& other) noexcept : disposer(other.disposer), ptr(other.ptr) { static_assert(__is_polymorphic(T), "Casting owned pointers requires that the target type is polymorphic."); other.ptr = nullptr; } inline Own(T* ptr, const Disposer& disposer) noexcept: disposer(&disposer), ptr(ptr) {} ~Own() noexcept(false) { dispose(); } inline Own& operator=(Own&& other) { // Move-assingnment operator. // Careful, this might own `other`. Therefore we have to transfer the pointers first, then // dispose. const Disposer* disposerCopy = disposer; T* ptrCopy = ptr; disposer = other.disposer; ptr = other.ptr; other.ptr = nullptr; if (ptrCopy != nullptr) { disposerCopy->dispose(const_cast*>(ptrCopy)); } return *this; } inline Own& operator=(decltype(nullptr)) { dispose(); return *this; } template Own downcast() { // Downcast the pointer to Own, destroying the original pointer. If this pointer does not // actually point at an instance of U, the results are undefined (throws an exception in debug // mode if RTTI is enabled, otherwise you're on your own). Own result; if (ptr != nullptr) { result.ptr = &kj::downcast(*ptr); result.disposer = disposer; ptr = nullptr; } return result; } #define NULLCHECK KJ_IREQUIRE(ptr != nullptr, "null Own<> dereference") inline T* operator->() { NULLCHECK; return ptr; } inline const T* operator->() const { NULLCHECK; return ptr; } inline T& operator*() { NULLCHECK; return *ptr; } inline const T& operator*() const { NULLCHECK; return *ptr; } #undef NULLCHECK inline T* get() { return ptr; } inline const T* get() const { return ptr; } inline operator T*() { return ptr; } inline operator const T*() const { return ptr; } private: const Disposer* disposer; // Only valid if ptr != nullptr. T* ptr; inline explicit Own(decltype(nullptr)): disposer(nullptr), ptr(nullptr) {} inline bool operator==(decltype(nullptr)) { return ptr == nullptr; } inline bool operator!=(decltype(nullptr)) { return ptr != nullptr; } // Only called by Maybe>. inline void dispose() { // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly // dispose again. T* ptrCopy = ptr; if (ptrCopy != nullptr) { ptr = nullptr; disposer->dispose(const_cast*>(ptrCopy)); } } template friend class Own; friend class Maybe>; }; namespace _ { // private template class OwnOwn { public: inline OwnOwn(Own&& value) noexcept: value(kj::mv(value)) {} inline Own& operator*() & { return value; } inline const Own& operator*() const & { return value; } inline Own&& operator*() && { return kj::mv(value); } inline const Own&& operator*() const && { return kj::mv(value); } inline Own* operator->() { return &value; } inline const Own* operator->() const { return &value; } inline operator Own*() { return value ? &value : nullptr; } inline operator const Own*() const { return value ? &value : nullptr; } private: Own value; }; template OwnOwn readMaybe(Maybe>&& maybe) { return OwnOwn(kj::mv(maybe.ptr)); } template Own* readMaybe(Maybe>& maybe) { return maybe.ptr ? &maybe.ptr : nullptr; } template const Own* readMaybe(const Maybe>& maybe) { return maybe.ptr ? &maybe.ptr : nullptr; } } // namespace _ (private) template class Maybe> { public: inline Maybe(): ptr(nullptr) {} inline Maybe(Own&& t) noexcept: ptr(kj::mv(t)) {} inline Maybe(Maybe&& other) noexcept: ptr(kj::mv(other.ptr)) {} template inline Maybe(Maybe>&& other): ptr(mv(other.ptr)) {} template inline Maybe(Own&& other): ptr(mv(other)) {} inline Maybe(decltype(nullptr)) noexcept: ptr(nullptr) {} inline operator Maybe() { return ptr.get(); } inline operator Maybe() const { return ptr.get(); } inline Maybe& operator=(Maybe&& other) { ptr = kj::mv(other.ptr); return *this; } inline bool operator==(decltype(nullptr)) const { return ptr == nullptr; } inline bool operator!=(decltype(nullptr)) const { return ptr != nullptr; } Own& orDefault(Own& defaultValue) { if (ptr == nullptr) { return defaultValue; } else { return ptr; } } const Own& orDefault(const Own& defaultValue) const { if (ptr == nullptr) { return defaultValue; } else { return ptr; } } template auto map(Func&& f) & -> Maybe&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(ptr); } } template auto map(Func&& f) const & -> Maybe&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(ptr); } } template auto map(Func&& f) && -> Maybe&&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(kj::mv(ptr)); } } template auto map(Func&& f) const && -> Maybe&&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(kj::mv(ptr)); } } private: Own ptr; template friend class Maybe; template friend _::OwnOwn _::readMaybe(Maybe>&& maybe); template friend Own* _::readMaybe(Maybe>& maybe); template friend const Own* _::readMaybe(const Maybe>& maybe); }; namespace _ { // private template class HeapDisposer final: public Disposer { public: virtual void disposeImpl(void* pointer) const override { delete reinterpret_cast(pointer); } static const HeapDisposer instance; }; template const HeapDisposer HeapDisposer::instance = HeapDisposer(); } // namespace _ (private) template Own heap(Params&&... params) { // heap(...) allocates a T on the heap, forwarding the parameters to its constructor. The // exact heap implementation is unspecified -- for now it is operator new, but you should not // assume this. (Since we know the object size at delete time, we could actually implement an // allocator that is more efficient than operator new.) return Own(new T(kj::fwd(params)...), _::HeapDisposer::instance); } template Own> heap(T&& orig) { // Allocate a copy (or move) of the argument on the heap. // // The purpose of this overload is to allow you to omit the template parameter as there is only // one argument and the purpose is to copy it. typedef Decay T2; return Own(new T2(kj::fwd(orig)), _::HeapDisposer::instance); } // ======================================================================================= // SpaceFor -- assists in manual allocation template class SpaceFor { // A class which has the same size and alignment as T but does not call its constructor or // destructor automatically. Instead, call construct() to construct a T in the space, which // returns an Own which will take care of calling T's destructor later. public: inline SpaceFor() {} inline ~SpaceFor() {} template Own construct(Params&&... params) { ctor(value, kj::fwd(params)...); return Own(&value, DestructorOnlyDisposer::instance); } private: union { T value; }; }; // ======================================================================================= // Inline implementation details template struct Disposer::Dispose_ { static void dispose(T* object, const Disposer& disposer) { // Note that dynamic_cast does not require RTTI to be enabled, because the offset to // the top of the object is in the vtable -- as it obviously needs to be to correctly implement // operator delete. disposer.disposeImpl(dynamic_cast(object)); } }; template struct Disposer::Dispose_ { static void dispose(T* object, const Disposer& disposer) { disposer.disposeImpl(static_cast(object)); } }; template void Disposer::dispose(T* object) const { Dispose_::dispose(object, *this); } } // namespace kj #endif // KJ_MEMORY_H_