xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/refcounted-object.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* Base class of intrusively reference-counted objects.
2*5ba1f45fSchristos    Copyright (C) 2017-2024 Free Software Foundation, Inc.
38dffb485Schristos 
48dffb485Schristos    This file is part of GDB.
58dffb485Schristos 
68dffb485Schristos    This program is free software; you can redistribute it and/or modify
78dffb485Schristos    it under the terms of the GNU General Public License as published by
88dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
98dffb485Schristos    (at your option) any later version.
108dffb485Schristos 
118dffb485Schristos    This program is distributed in the hope that it will be useful,
128dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148dffb485Schristos    GNU General Public License for more details.
158dffb485Schristos 
168dffb485Schristos    You should have received a copy of the GNU General Public License
178dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
188dffb485Schristos 
198dffb485Schristos #ifndef COMMON_REFCOUNTED_OBJECT_H
208dffb485Schristos #define COMMON_REFCOUNTED_OBJECT_H
218dffb485Schristos 
228dffb485Schristos /* Base class of intrusively reference-countable objects.
238dffb485Schristos    Incrementing and decrementing the reference count is an external
248dffb485Schristos    responsibility.  */
258dffb485Schristos 
268dffb485Schristos class refcounted_object
278dffb485Schristos {
288dffb485Schristos public:
298dffb485Schristos   refcounted_object () = default;
308dffb485Schristos 
318dffb485Schristos   /* Increase the refcount.  */
328dffb485Schristos   void incref ()
338dffb485Schristos   {
348dffb485Schristos     gdb_assert (m_refcount >= 0);
358dffb485Schristos     m_refcount++;
368dffb485Schristos   }
378dffb485Schristos 
388dffb485Schristos   /* Decrease the refcount.  */
398dffb485Schristos   void decref ()
408dffb485Schristos   {
418dffb485Schristos     m_refcount--;
428dffb485Schristos     gdb_assert (m_refcount >= 0);
438dffb485Schristos   }
448dffb485Schristos 
458dffb485Schristos   int refcount () const { return m_refcount; }
468dffb485Schristos 
478dffb485Schristos private:
488dffb485Schristos   DISABLE_COPY_AND_ASSIGN (refcounted_object);
498dffb485Schristos 
508dffb485Schristos   /* The reference count.  */
518dffb485Schristos   int m_refcount = 0;
528dffb485Schristos };
538dffb485Schristos 
548dffb485Schristos /* A policy class to interface gdb::ref_ptr with a
558dffb485Schristos    refcounted_object.  */
568dffb485Schristos 
578dffb485Schristos struct refcounted_object_ref_policy
588dffb485Schristos {
598dffb485Schristos   static void incref (refcounted_object *ptr)
608dffb485Schristos   {
618dffb485Schristos     ptr->incref ();
628dffb485Schristos   }
638dffb485Schristos 
648dffb485Schristos   static void decref (refcounted_object *ptr)
658dffb485Schristos   {
668dffb485Schristos     ptr->decref ();
678dffb485Schristos   }
688dffb485Schristos };
698dffb485Schristos 
70*5ba1f45fSchristos /* A policy class to interface gdb::ref_ptr with a refcounted_object, that
71*5ba1f45fSchristos    deletes the object once the refcount reaches 0..  */
72*5ba1f45fSchristos 
73*5ba1f45fSchristos template<typename T>
74*5ba1f45fSchristos struct refcounted_object_delete_ref_policy
75*5ba1f45fSchristos {
76*5ba1f45fSchristos   static void incref (T *obj)
77*5ba1f45fSchristos   { obj->incref (); }
78*5ba1f45fSchristos 
79*5ba1f45fSchristos   static void decref (T *obj)
80*5ba1f45fSchristos   {
81*5ba1f45fSchristos     obj->decref ();
82*5ba1f45fSchristos     if (obj->refcount () == 0)
83*5ba1f45fSchristos       delete obj;
84*5ba1f45fSchristos   }
85*5ba1f45fSchristos };
86*5ba1f45fSchristos 
878dffb485Schristos #endif /* COMMON_REFCOUNTED_OBJECT_H */
88