refcnt.hh revision 8220
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
313877Sbinkertn@umich.edu#ifndef __BASE_REFCNT_HH__
323877Sbinkertn@umich.edu#define __BASE_REFCNT_HH__
332147SN/A
342SN/Aclass RefCounted
352SN/A{
362SN/A  private:
377866Snate@binkert.org    mutable int count;
382SN/A
392SN/A  private:
407057Snate@binkert.org    // Don't allow a default copy constructor or copy operator on
417057Snate@binkert.org    // these objects because the default operation will copy the
427057Snate@binkert.org    // reference count as well and we certainly don't want that.
432SN/A    RefCounted(const RefCounted &);
447057Snate@binkert.org    RefCounted &operator=(const RefCounted &);
452SN/A
462SN/A  public:
472SN/A    RefCounted() : count(0) {}
482SN/A    virtual ~RefCounted() {}
492SN/A
502SN/A    void incref() { ++count; }
512SN/A    void decref() { if (--count <= 0) delete this; }
522SN/A};
532SN/A
542SN/Atemplate <class T>
552SN/Aclass RefCountingPtr
562SN/A{
571078SN/A  protected:
582SN/A    T *data;
592SN/A
601114SN/A    void copy(T *d)
611114SN/A    {
622SN/A        data = d;
632SN/A        if (data)
642SN/A            data->incref();
652SN/A    }
661114SN/A    void del()
671114SN/A    {
682SN/A        if (data)
692SN/A            data->decref();
702SN/A    }
711114SN/A    void set(T *d)
721114SN/A    {
731114SN/A        if (data == d)
741114SN/A            return;
751114SN/A
761114SN/A        del();
771114SN/A        copy(d);
781114SN/A    }
791114SN/A
802SN/A
812SN/A  public:
823877Sbinkertn@umich.edu    RefCountingPtr() : data(0) {}
832SN/A    RefCountingPtr(T *data) { copy(data); }
84502SN/A    RefCountingPtr(const RefCountingPtr &r) { copy(r.data); }
852SN/A    ~RefCountingPtr() { del(); }
862SN/A
877866Snate@binkert.org    T *operator->() const { return data; }
887866Snate@binkert.org    T &operator*() const { return *data; }
897866Snate@binkert.org    T *get() const { return data; }
902SN/A
913877Sbinkertn@umich.edu    const RefCountingPtr &operator=(T *p) { set(p); return *this; }
923877Sbinkertn@umich.edu    const RefCountingPtr &operator=(const RefCountingPtr &r)
931114SN/A    { return operator=(r.data); }
942SN/A
952SN/A    bool operator!() const { return data == 0; }
962SN/A    operator bool() const { return data != 0; }
972SN/A};
982SN/A
992SN/Atemplate<class T>
1008220Snate@binkert.orginline bool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
1012SN/A{ return l.get() == r.get(); }
1022SN/A
1032SN/Atemplate<class T>
1048220Snate@binkert.orginline bool operator==(const RefCountingPtr<T> &l, const T *r)
1052SN/A{ return l.get() == r; }
1062SN/A
1072SN/Atemplate<class T>
1088220Snate@binkert.orginline bool operator==(const T *l, const RefCountingPtr<T> &r)
1092SN/A{ return l == r.get(); }
1102SN/A
1112SN/Atemplate<class T>
1128220Snate@binkert.orginline bool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
1132SN/A{ return l.get() != r.get(); }
1142SN/A
1152SN/Atemplate<class T>
1168220Snate@binkert.orginline bool operator!=(const RefCountingPtr<T> &l, const T *r)
1172SN/A{ return l.get() != r; }
1182SN/A
1192SN/Atemplate<class T>
1208220Snate@binkert.orginline bool operator!=(const T *l, const RefCountingPtr<T> &r)
1212SN/A{ return l != r.get(); }
1222SN/A
1233877Sbinkertn@umich.edu#endif // __BASE_REFCNT_HH__
124