refcnt.hh revision 13429
12SN/A/*
213429Srekai.gonzalezalberquilla@arm.com * Copyright (c) 2017 ARM Limited
313429Srekai.gonzalezalberquilla@arm.com * All rights reserved.
413429Srekai.gonzalezalberquilla@arm.com *
513429Srekai.gonzalezalberquilla@arm.com * The license below extends only to copyright in the software and shall
613429Srekai.gonzalezalberquilla@arm.com * not be construed as granting a license to any other intellectual
713429Srekai.gonzalezalberquilla@arm.com * property including but not limited to intellectual property relating
813429Srekai.gonzalezalberquilla@arm.com * to a hardware implementation of the functionality of the software
913429Srekai.gonzalezalberquilla@arm.com * licensed hereunder.  You may use the software subject to the license
1013429Srekai.gonzalezalberquilla@arm.com * terms below provided that you ensure that this notice is replicated
1113429Srekai.gonzalezalberquilla@arm.com * unmodified and in its entirety in all distributions of the software,
1213429Srekai.gonzalezalberquilla@arm.com * modified or unmodified, in source code or in binary form.
1313429Srekai.gonzalezalberquilla@arm.com *
141762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
152SN/A * All rights reserved.
162SN/A *
172SN/A * Redistribution and use in source and binary forms, with or without
182SN/A * modification, are permitted provided that the following conditions are
192SN/A * met: redistributions of source code must retain the above copyright
202SN/A * notice, this list of conditions and the following disclaimer;
212SN/A * redistributions in binary form must reproduce the above copyright
222SN/A * notice, this list of conditions and the following disclaimer in the
232SN/A * documentation and/or other materials provided with the distribution;
242SN/A * neither the name of the copyright holders nor the names of its
252SN/A * contributors may be used to endorse or promote products derived from
262SN/A * this software without specific prior written permission.
272SN/A *
282SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
412SN/A */
422SN/A
433877Sbinkertn@umich.edu#ifndef __BASE_REFCNT_HH__
443877Sbinkertn@umich.edu#define __BASE_REFCNT_HH__
452147SN/A
4613429Srekai.gonzalezalberquilla@arm.com#include <type_traits>
4713429Srekai.gonzalezalberquilla@arm.com
488221Snate@binkert.org/**
498221Snate@binkert.org * @file base/refcnt.hh
508221Snate@binkert.org *
518221Snate@binkert.org * Classes for managing reference counted objects.
528221Snate@binkert.org */
538221Snate@binkert.org
548221Snate@binkert.org/**
558221Snate@binkert.org * Derive from RefCounted if you want to enable reference counting of
568221Snate@binkert.org * this class.  If you want to use automatic reference counting, you
578221Snate@binkert.org * should use RefCountingPtr<T> instead of regular pointers.
588221Snate@binkert.org */
592SN/Aclass RefCounted
602SN/A{
612SN/A  private:
628221Snate@binkert.org    // The reference count is mutable because one may want to
638221Snate@binkert.org    // reference count a const pointer.  This really is OK because
648221Snate@binkert.org    // const is about logical constness of the object not really about
658221Snate@binkert.org    // strictly disallowing an object to change.
667866Snate@binkert.org    mutable int count;
672SN/A
682SN/A  private:
697057Snate@binkert.org    // Don't allow a default copy constructor or copy operator on
707057Snate@binkert.org    // these objects because the default operation will copy the
717057Snate@binkert.org    // reference count as well and we certainly don't want that.
722SN/A    RefCounted(const RefCounted &);
737057Snate@binkert.org    RefCounted &operator=(const RefCounted &);
742SN/A
752SN/A  public:
768221Snate@binkert.org    /**
778221Snate@binkert.org     * We initialize the reference count to zero and the first object
788221Snate@binkert.org     * to take ownership of it must increment it to one.
798221Snate@binkert.org     *
808221Snate@binkert.org     * @attention A memory leak will occur if you never assign a newly
818221Snate@binkert.org     * constructed object to a reference counting pointer.
828221Snate@binkert.org     */
832SN/A    RefCounted() : count(0) {}
848221Snate@binkert.org
858221Snate@binkert.org    /**
868221Snate@binkert.org     * We make the destructor virtual because we're likely to have
878221Snate@binkert.org     * virtual functions on reference counted objects.
888221Snate@binkert.org     *
898221Snate@binkert.org     * @todo Even if this were true, does it matter?  Shouldn't the
908221Snate@binkert.org     * derived class indicate this?  This only matters if we would
918221Snate@binkert.org     * ever choose to delete a "RefCounted *" which I doubt we'd ever
928221Snate@binkert.org     * do.  We don't ever delete a "void *".
938221Snate@binkert.org     */
942SN/A    virtual ~RefCounted() {}
952SN/A
968221Snate@binkert.org    /// Increment the reference count
9713429Srekai.gonzalezalberquilla@arm.com    void incref() const { ++count; }
988221Snate@binkert.org
998221Snate@binkert.org    /// Decrement the reference count and destroy the object if all
1008221Snate@binkert.org    /// references are gone.
10113429Srekai.gonzalezalberquilla@arm.com    void decref() const { if (--count <= 0) delete this; }
1022SN/A};
1032SN/A
1048221Snate@binkert.org/**
1058221Snate@binkert.org * If you want a reference counting pointer to a mutable object,
1068221Snate@binkert.org * create it like this:
1078221Snate@binkert.org * @code
1088221Snate@binkert.org * typedef RefCountingPtr<Foo> FooPtr;
1098221Snate@binkert.org * @endcode
1108221Snate@binkert.org *
1118221Snate@binkert.org * @attention Do not use "const FooPtr"
1128221Snate@binkert.org * To create a reference counting pointer to a const object, use this:
1138221Snate@binkert.org * @code
1148221Snate@binkert.org * typedef RefCountingPtr<const Foo> ConstFooPtr;
1158221Snate@binkert.org * @endcode
1168221Snate@binkert.org *
1178221Snate@binkert.org * These two usages are analogous to iterator and const_iterator in the stl.
1188221Snate@binkert.org */
1192SN/Atemplate <class T>
1202SN/Aclass RefCountingPtr
1212SN/A{
1221078SN/A  protected:
12313429Srekai.gonzalezalberquilla@arm.com    /** Convenience aliases for const/non-const versions of T w/ friendship. */
12413429Srekai.gonzalezalberquilla@arm.com    /** @{ */
12513429Srekai.gonzalezalberquilla@arm.com    static constexpr auto TisConst = std::is_const<T>::value;
12613429Srekai.gonzalezalberquilla@arm.com    using ConstT = typename std::conditional<TisConst,
12713429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<T>,
12813429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<typename std::add_const<T>::type>>::type;
12913429Srekai.gonzalezalberquilla@arm.com    friend ConstT;
13013429Srekai.gonzalezalberquilla@arm.com    using NonConstT = typename std::conditional<TisConst,
13113429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<typename std::remove_const<T>::type>,
13213429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<T>>::type;
13313429Srekai.gonzalezalberquilla@arm.com    friend NonConstT;
13413429Srekai.gonzalezalberquilla@arm.com    /** @} */
1358221Snate@binkert.org    /// The stored pointer.
1368221Snate@binkert.org    /// Arguably this should be private.
1372SN/A    T *data;
1382SN/A
1398221Snate@binkert.org    /**
1408221Snate@binkert.org     * Copy a new pointer value and increment the reference count if
1418221Snate@binkert.org     * it is a valid pointer.  Note, this does not delete the
1428221Snate@binkert.org     * reference any existing object.
1438221Snate@binkert.org     * @param d Pointer to store.
1448221Snate@binkert.org     */
1458221Snate@binkert.org    void
1468221Snate@binkert.org    copy(T *d)
1471114SN/A    {
1482SN/A        data = d;
1492SN/A        if (data)
1502SN/A            data->incref();
1512SN/A    }
1528221Snate@binkert.org
1538221Snate@binkert.org    /**
1548221Snate@binkert.org     * Delete the reference to any existing object if it is non NULL.
1558221Snate@binkert.org     * @attention this doesn't clear the pointer value, so a double
1568221Snate@binkert.org     * decref could happen if not careful.
1578221Snate@binkert.org     */
1588221Snate@binkert.org    void
1598221Snate@binkert.org    del()
1601114SN/A    {
1612SN/A        if (data)
1622SN/A            data->decref();
1632SN/A    }
1648221Snate@binkert.org
1658221Snate@binkert.org    /**
1668221Snate@binkert.org     * Drop the old reference and change it to something new.
1678221Snate@binkert.org     */
1688221Snate@binkert.org    void
1698221Snate@binkert.org    set(T *d)
1701114SN/A    {
1718221Snate@binkert.org        // Need to check if we're actually changing because otherwise
1728221Snate@binkert.org        // we could delete the last reference before adding the new
1738221Snate@binkert.org        // reference.
1748221Snate@binkert.org        if (data != d) {
1758221Snate@binkert.org            del();
1768221Snate@binkert.org            copy(d);
1778221Snate@binkert.org        }
1781114SN/A    }
1791114SN/A
1808221Snate@binkert.org  public:
1818221Snate@binkert.org    /// Create an empty reference counting pointer.
1828221Snate@binkert.org    RefCountingPtr() : data(0) {}
1832SN/A
1848221Snate@binkert.org    /// Create a new reference counting pointer to some object
1858221Snate@binkert.org    /// (probably something newly created).  Adds a reference.
1862SN/A    RefCountingPtr(T *data) { copy(data); }
1878221Snate@binkert.org
1888221Snate@binkert.org    /// Create a new reference counting pointer by copying another
1898221Snate@binkert.org    /// one.  Adds a reference.
190502SN/A    RefCountingPtr(const RefCountingPtr &r) { copy(r.data); }
1918221Snate@binkert.org
19213429Srekai.gonzalezalberquilla@arm.com    /** Move-constructor.
19313429Srekai.gonzalezalberquilla@arm.com     * Does not add a reference.
19413429Srekai.gonzalezalberquilla@arm.com     */
19513429Srekai.gonzalezalberquilla@arm.com    RefCountingPtr(RefCountingPtr&& r)
19613429Srekai.gonzalezalberquilla@arm.com    {
19713429Srekai.gonzalezalberquilla@arm.com        data = r.data;
19813429Srekai.gonzalezalberquilla@arm.com        r.data = nullptr;
19913429Srekai.gonzalezalberquilla@arm.com    }
20013429Srekai.gonzalezalberquilla@arm.com
20113429Srekai.gonzalezalberquilla@arm.com    template <bool B = TisConst>
20213429Srekai.gonzalezalberquilla@arm.com    RefCountingPtr(const NonConstT &r) { copy(r.data); }
20313429Srekai.gonzalezalberquilla@arm.com
2048221Snate@binkert.org    /// Destroy the pointer and any reference it may hold.
2052SN/A    ~RefCountingPtr() { del(); }
2062SN/A
2078221Snate@binkert.org    // The following pointer access functions are const because they
2088221Snate@binkert.org    // don't actually change the pointer, though the user could change
2098221Snate@binkert.org    // what is pointed to.  This is analagous to a "Foo * const".
2108221Snate@binkert.org
2118221Snate@binkert.org    /// Access a member variable.
2127866Snate@binkert.org    T *operator->() const { return data; }
2138221Snate@binkert.org
2148221Snate@binkert.org    /// Dereference the pointer.
2157866Snate@binkert.org    T &operator*() const { return *data; }
2168221Snate@binkert.org
2178221Snate@binkert.org    /// Directly access the pointer itself without taking a reference.
2187866Snate@binkert.org    T *get() const { return data; }
2192SN/A
22013429Srekai.gonzalezalberquilla@arm.com    template <bool B = TisConst>
22113429Srekai.gonzalezalberquilla@arm.com    operator RefCountingPtr<typename std::enable_if<!B, ConstT>::type>()
22213429Srekai.gonzalezalberquilla@arm.com    {
22313429Srekai.gonzalezalberquilla@arm.com        return RefCountingPtr<const T>(*this);
22413429Srekai.gonzalezalberquilla@arm.com    }
22513429Srekai.gonzalezalberquilla@arm.com
2268221Snate@binkert.org    /// Assign a new value to the pointer
2273877Sbinkertn@umich.edu    const RefCountingPtr &operator=(T *p) { set(p); return *this; }
2288221Snate@binkert.org
2298221Snate@binkert.org    /// Copy the pointer from another RefCountingPtr
2303877Sbinkertn@umich.edu    const RefCountingPtr &operator=(const RefCountingPtr &r)
2311114SN/A    { return operator=(r.data); }
2322SN/A
23313429Srekai.gonzalezalberquilla@arm.com    /// Move-assign the pointer from another RefCountingPtr
23413429Srekai.gonzalezalberquilla@arm.com    const RefCountingPtr &operator=(RefCountingPtr&& r)
23513429Srekai.gonzalezalberquilla@arm.com    {
23613429Srekai.gonzalezalberquilla@arm.com        /* This happens regardless of whether the pointer is the same or not,
23713429Srekai.gonzalezalberquilla@arm.com         * because of the move semantics, the rvalue needs to be 'destroyed'.
23813429Srekai.gonzalezalberquilla@arm.com         */
23913429Srekai.gonzalezalberquilla@arm.com        del();
24013429Srekai.gonzalezalberquilla@arm.com        data = r.data;
24113429Srekai.gonzalezalberquilla@arm.com        r.data = nullptr;
24213429Srekai.gonzalezalberquilla@arm.com        return *this;
24313429Srekai.gonzalezalberquilla@arm.com    }
24413429Srekai.gonzalezalberquilla@arm.com
2458221Snate@binkert.org    /// Check if the pointer is empty
2462SN/A    bool operator!() const { return data == 0; }
2478221Snate@binkert.org
2488221Snate@binkert.org    /// Check if the pointer is non-empty
2492SN/A    operator bool() const { return data != 0; }
2502SN/A};
2512SN/A
2528221Snate@binkert.org/// Check for equality of two reference counting pointers.
2532SN/Atemplate<class T>
2548220Snate@binkert.orginline bool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
2552SN/A{ return l.get() == r.get(); }
2562SN/A
2578221Snate@binkert.org/// Check for equality of of a reference counting pointers and a
2588221Snate@binkert.org/// regular pointer
2592SN/Atemplate<class T>
2608220Snate@binkert.orginline bool operator==(const RefCountingPtr<T> &l, const T *r)
2612SN/A{ return l.get() == r; }
2622SN/A
2638221Snate@binkert.org/// Check for equality of of a reference counting pointers and a
2648221Snate@binkert.org/// regular pointer
2652SN/Atemplate<class T>
2668220Snate@binkert.orginline bool operator==(const T *l, const RefCountingPtr<T> &r)
2672SN/A{ return l == r.get(); }
2682SN/A
2698221Snate@binkert.org/// Check for inequality of two reference counting pointers.
2702SN/Atemplate<class T>
2718220Snate@binkert.orginline bool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
2722SN/A{ return l.get() != r.get(); }
2732SN/A
2748221Snate@binkert.org/// Check for inequality of of a reference counting pointers and a
2758221Snate@binkert.org/// regular pointer
2762SN/Atemplate<class T>
2778220Snate@binkert.orginline bool operator!=(const RefCountingPtr<T> &l, const T *r)
2782SN/A{ return l.get() != r; }
2792SN/A
2808221Snate@binkert.org/// Check for inequality of of a reference counting pointers and a
2818221Snate@binkert.org/// regular pointer
2822SN/Atemplate<class T>
2838220Snate@binkert.orginline bool operator!=(const T *l, const RefCountingPtr<T> &r)
2842SN/A{ return l != r.get(); }
2852SN/A
2863877Sbinkertn@umich.edu#endif // __BASE_REFCNT_HH__
287