12SN/A/*
213590Srekai.gonzalezalberquilla@arm.com * Copyright (c) 2017-2018 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{
12213474Sgiacomo.gabrielli@arm.com  public:
12313474Sgiacomo.gabrielli@arm.com    using PtrType = T*;
12413474Sgiacomo.gabrielli@arm.com
1251078SN/A  protected:
12613429Srekai.gonzalezalberquilla@arm.com    /** Convenience aliases for const/non-const versions of T w/ friendship. */
12713429Srekai.gonzalezalberquilla@arm.com    /** @{ */
12813429Srekai.gonzalezalberquilla@arm.com    static constexpr auto TisConst = std::is_const<T>::value;
12913429Srekai.gonzalezalberquilla@arm.com    using ConstT = typename std::conditional<TisConst,
13013429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<T>,
13113429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<typename std::add_const<T>::type>>::type;
13213429Srekai.gonzalezalberquilla@arm.com    friend ConstT;
13313429Srekai.gonzalezalberquilla@arm.com    using NonConstT = typename std::conditional<TisConst,
13413429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<typename std::remove_const<T>::type>,
13513429Srekai.gonzalezalberquilla@arm.com            RefCountingPtr<T>>::type;
13613429Srekai.gonzalezalberquilla@arm.com    friend NonConstT;
13713429Srekai.gonzalezalberquilla@arm.com    /** @} */
1388221Snate@binkert.org    /// The stored pointer.
1398221Snate@binkert.org    /// Arguably this should be private.
1402SN/A    T *data;
1412SN/A
1428221Snate@binkert.org    /**
1438221Snate@binkert.org     * Copy a new pointer value and increment the reference count if
1448221Snate@binkert.org     * it is a valid pointer.  Note, this does not delete the
1458221Snate@binkert.org     * reference any existing object.
1468221Snate@binkert.org     * @param d Pointer to store.
1478221Snate@binkert.org     */
1488221Snate@binkert.org    void
1498221Snate@binkert.org    copy(T *d)
1501114SN/A    {
1512SN/A        data = d;
1522SN/A        if (data)
1532SN/A            data->incref();
1542SN/A    }
1558221Snate@binkert.org
1568221Snate@binkert.org    /**
1578221Snate@binkert.org     * Delete the reference to any existing object if it is non NULL.
1588221Snate@binkert.org     * @attention this doesn't clear the pointer value, so a double
1598221Snate@binkert.org     * decref could happen if not careful.
1608221Snate@binkert.org     */
1618221Snate@binkert.org    void
1628221Snate@binkert.org    del()
1631114SN/A    {
1642SN/A        if (data)
1652SN/A            data->decref();
1662SN/A    }
1678221Snate@binkert.org
1688221Snate@binkert.org    /**
1698221Snate@binkert.org     * Drop the old reference and change it to something new.
1708221Snate@binkert.org     */
1718221Snate@binkert.org    void
1728221Snate@binkert.org    set(T *d)
1731114SN/A    {
1748221Snate@binkert.org        // Need to check if we're actually changing because otherwise
1758221Snate@binkert.org        // we could delete the last reference before adding the new
1768221Snate@binkert.org        // reference.
1778221Snate@binkert.org        if (data != d) {
1788221Snate@binkert.org            del();
1798221Snate@binkert.org            copy(d);
1808221Snate@binkert.org        }
1811114SN/A    }
1821114SN/A
1838221Snate@binkert.org  public:
1848221Snate@binkert.org    /// Create an empty reference counting pointer.
1858221Snate@binkert.org    RefCountingPtr() : data(0) {}
1862SN/A
1878221Snate@binkert.org    /// Create a new reference counting pointer to some object
1888221Snate@binkert.org    /// (probably something newly created).  Adds a reference.
1892SN/A    RefCountingPtr(T *data) { copy(data); }
1908221Snate@binkert.org
1918221Snate@binkert.org    /// Create a new reference counting pointer by copying another
1928221Snate@binkert.org    /// one.  Adds a reference.
193502SN/A    RefCountingPtr(const RefCountingPtr &r) { copy(r.data); }
1948221Snate@binkert.org
19513429Srekai.gonzalezalberquilla@arm.com    /** Move-constructor.
19613429Srekai.gonzalezalberquilla@arm.com     * Does not add a reference.
19713429Srekai.gonzalezalberquilla@arm.com     */
19813429Srekai.gonzalezalberquilla@arm.com    RefCountingPtr(RefCountingPtr&& r)
19913429Srekai.gonzalezalberquilla@arm.com    {
20013429Srekai.gonzalezalberquilla@arm.com        data = r.data;
20113429Srekai.gonzalezalberquilla@arm.com        r.data = nullptr;
20213429Srekai.gonzalezalberquilla@arm.com    }
20313429Srekai.gonzalezalberquilla@arm.com
20413429Srekai.gonzalezalberquilla@arm.com    template <bool B = TisConst>
20513429Srekai.gonzalezalberquilla@arm.com    RefCountingPtr(const NonConstT &r) { copy(r.data); }
20613429Srekai.gonzalezalberquilla@arm.com
2078221Snate@binkert.org    /// Destroy the pointer and any reference it may hold.
2082SN/A    ~RefCountingPtr() { del(); }
2092SN/A
2108221Snate@binkert.org    // The following pointer access functions are const because they
2118221Snate@binkert.org    // don't actually change the pointer, though the user could change
2128221Snate@binkert.org    // what is pointed to.  This is analagous to a "Foo * const".
2138221Snate@binkert.org
2148221Snate@binkert.org    /// Access a member variable.
2157866Snate@binkert.org    T *operator->() const { return data; }
2168221Snate@binkert.org
2178221Snate@binkert.org    /// Dereference the pointer.
2187866Snate@binkert.org    T &operator*() const { return *data; }
2198221Snate@binkert.org
2208221Snate@binkert.org    /// Directly access the pointer itself without taking a reference.
2217866Snate@binkert.org    T *get() const { return data; }
2222SN/A
22313429Srekai.gonzalezalberquilla@arm.com    template <bool B = TisConst>
22413429Srekai.gonzalezalberquilla@arm.com    operator RefCountingPtr<typename std::enable_if<!B, ConstT>::type>()
22513429Srekai.gonzalezalberquilla@arm.com    {
22613429Srekai.gonzalezalberquilla@arm.com        return RefCountingPtr<const T>(*this);
22713429Srekai.gonzalezalberquilla@arm.com    }
22813429Srekai.gonzalezalberquilla@arm.com
2298221Snate@binkert.org    /// Assign a new value to the pointer
2303877Sbinkertn@umich.edu    const RefCountingPtr &operator=(T *p) { set(p); return *this; }
2318221Snate@binkert.org
2328221Snate@binkert.org    /// Copy the pointer from another RefCountingPtr
2333877Sbinkertn@umich.edu    const RefCountingPtr &operator=(const RefCountingPtr &r)
2341114SN/A    { return operator=(r.data); }
2352SN/A
23613429Srekai.gonzalezalberquilla@arm.com    /// Move-assign the pointer from another RefCountingPtr
23713429Srekai.gonzalezalberquilla@arm.com    const RefCountingPtr &operator=(RefCountingPtr&& r)
23813429Srekai.gonzalezalberquilla@arm.com    {
23913429Srekai.gonzalezalberquilla@arm.com        /* This happens regardless of whether the pointer is the same or not,
24013429Srekai.gonzalezalberquilla@arm.com         * because of the move semantics, the rvalue needs to be 'destroyed'.
24113429Srekai.gonzalezalberquilla@arm.com         */
24213429Srekai.gonzalezalberquilla@arm.com        del();
24313429Srekai.gonzalezalberquilla@arm.com        data = r.data;
24413429Srekai.gonzalezalberquilla@arm.com        r.data = nullptr;
24513429Srekai.gonzalezalberquilla@arm.com        return *this;
24613429Srekai.gonzalezalberquilla@arm.com    }
24713429Srekai.gonzalezalberquilla@arm.com
2488221Snate@binkert.org    /// Check if the pointer is empty
2492SN/A    bool operator!() const { return data == 0; }
2508221Snate@binkert.org
2518221Snate@binkert.org    /// Check if the pointer is non-empty
2522SN/A    operator bool() const { return data != 0; }
2532SN/A};
2542SN/A
2558221Snate@binkert.org/// Check for equality of two reference counting pointers.
2562SN/Atemplate<class T>
2578220Snate@binkert.orginline bool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
2582SN/A{ return l.get() == r.get(); }
2592SN/A
2608221Snate@binkert.org/// Check for equality of of a reference counting pointers and a
2618221Snate@binkert.org/// regular pointer
2622SN/Atemplate<class T>
2638220Snate@binkert.orginline bool operator==(const RefCountingPtr<T> &l, const T *r)
2642SN/A{ return l.get() == r; }
2652SN/A
2668221Snate@binkert.org/// Check for equality of of a reference counting pointers and a
2678221Snate@binkert.org/// regular pointer
2682SN/Atemplate<class T>
2698220Snate@binkert.orginline bool operator==(const T *l, const RefCountingPtr<T> &r)
2702SN/A{ return l == r.get(); }
2712SN/A
2728221Snate@binkert.org/// Check for inequality of two reference counting pointers.
2732SN/Atemplate<class T>
2748220Snate@binkert.orginline bool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
2752SN/A{ return l.get() != r.get(); }
2762SN/A
2778221Snate@binkert.org/// Check for inequality of of a reference counting pointers and a
2788221Snate@binkert.org/// regular pointer
2792SN/Atemplate<class T>
2808220Snate@binkert.orginline bool operator!=(const RefCountingPtr<T> &l, const T *r)
2812SN/A{ return l.get() != r; }
2822SN/A
2838221Snate@binkert.org/// Check for inequality of of a reference counting pointers and a
2848221Snate@binkert.org/// regular pointer
2852SN/Atemplate<class T>
2868220Snate@binkert.orginline bool operator!=(const T *l, const RefCountingPtr<T> &r)
2872SN/A{ return l != r.get(); }
2882SN/A
2893877Sbinkertn@umich.edu#endif // __BASE_REFCNT_HH__
290