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