Chris@63: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@63: // Licensed under the MIT License: Chris@63: // Chris@63: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@63: // of this software and associated documentation files (the "Software"), to deal Chris@63: // in the Software without restriction, including without limitation the rights Chris@63: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@63: // copies of the Software, and to permit persons to whom the Software is Chris@63: // furnished to do so, subject to the following conditions: Chris@63: // Chris@63: // The above copyright notice and this permission notice shall be included in Chris@63: // all copies or substantial portions of the Software. Chris@63: // Chris@63: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@63: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@63: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@63: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@63: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@63: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@63: // THE SOFTWARE. Chris@63: Chris@63: #include "memory.h" Chris@63: Chris@63: #ifndef KJ_REFCOUNT_H_ Chris@63: #define KJ_REFCOUNT_H_ Chris@63: Chris@63: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@63: #pragma GCC system_header Chris@63: #endif Chris@63: Chris@63: namespace kj { Chris@63: Chris@63: class Refcounted: private Disposer { Chris@63: // Subclass this to create a class that contains a reference count. Then, use Chris@63: // `kj::refcounted()` to allocate a new refcounted pointer. Chris@63: // Chris@63: // Do NOT use this lightly. Refcounting is a crutch. Good designs should strive to make object Chris@63: // ownership clear, so that refcounting is not necessary. All that said, reference counting can Chris@63: // sometimes simplify code that would otherwise become convoluted with explicit ownership, even Chris@63: // when ownership relationships are clear at an abstract level. Chris@63: // Chris@63: // NOT THREADSAFE: This refcounting implementation assumes that an object's references are Chris@63: // manipulated only in one thread, because atomic (thread-safe) refcounting is surprisingly slow. Chris@63: // Chris@63: // In general, abstract classes should _not_ subclass this. The concrete class at the bottom Chris@63: // of the hierarchy should be the one to decide how it implements refcounting. Interfaces should Chris@63: // expose only an `addRef()` method that returns `Own`. There are two reasons for Chris@63: // this rule: Chris@63: // 1. Interfaces would need to virtually inherit Refcounted, otherwise two refcounted interfaces Chris@63: // could not be inherited by the same subclass. Virtual inheritance is awkward and Chris@63: // inefficient. Chris@63: // 2. An implementation may decide that it would rather return a copy than a refcount, or use Chris@63: // some other strategy. Chris@63: // Chris@63: // TODO(cleanup): Rethink above. Virtual inheritance is not necessarily that bad. OTOH, a Chris@63: // virtual function call for every refcount is sad in its own way. A Ref type to replace Chris@63: // Own could also be nice. Chris@63: Chris@63: public: Chris@63: virtual ~Refcounted() noexcept(false); Chris@63: Chris@63: inline bool isShared() const { return refcount > 1; } Chris@63: // Check if there are multiple references to this object. This is sometimes useful for deciding Chris@63: // whether it's safe to modify the object vs. make a copy. Chris@63: Chris@63: private: Chris@63: mutable uint refcount = 0; Chris@63: // "mutable" because disposeImpl() is const. Bleh. Chris@63: Chris@63: void disposeImpl(void* pointer) const override; Chris@63: template Chris@63: static Own addRefInternal(T* object); Chris@63: Chris@63: template Chris@63: friend Own addRef(T& object); Chris@63: template Chris@63: friend Own refcounted(Params&&... params); Chris@63: }; Chris@63: Chris@63: template Chris@63: inline Own refcounted(Params&&... params) { Chris@63: // Allocate a new refcounted instance of T, passing `params` to its constructor. Returns an Chris@63: // initial reference to the object. More references can be created with `kj::addRef()`. Chris@63: Chris@63: return Refcounted::addRefInternal(new T(kj::fwd(params)...)); Chris@63: } Chris@63: Chris@63: template Chris@63: Own addRef(T& object) { Chris@63: // Return a new reference to `object`, which must subclass Refcounted and have been allocated Chris@63: // using `kj::refcounted<>()`. It is suggested that subclasses implement a non-static addRef() Chris@63: // method which wraps this and returns the appropriate type. Chris@63: Chris@63: KJ_IREQUIRE(object.Refcounted::refcount > 0, "Object not allocated with kj::refcounted()."); Chris@63: return Refcounted::addRefInternal(&object); Chris@63: } Chris@63: Chris@63: template Chris@63: Own Refcounted::addRefInternal(T* object) { Chris@63: Refcounted* refcounted = object; Chris@63: ++refcounted->refcount; Chris@63: return Own(object, *refcounted); Chris@63: } Chris@63: Chris@63: } // namespace kj Chris@63: Chris@63: #endif // KJ_REFCOUNT_H_