Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-06 08:41:18

0001 /* grefcount.h: Reference counting
0002  *
0003  * Copyright 2018  Emmanuele Bassi
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-or-later
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2.1 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #ifndef __GREFCOUNT_H__
0022 #define __GREFCOUNT_H__
0023 
0024 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
0025 #error "Only <glib.h> can be included directly."
0026 #endif
0027 
0028 #include <glib/gatomic.h>
0029 #include <glib/gtypes.h>
0030 
0031 G_BEGIN_DECLS
0032 
0033 GLIB_AVAILABLE_IN_2_58
0034 void            g_ref_count_init                (grefcount       *rc);
0035 GLIB_AVAILABLE_IN_2_58
0036 void            g_ref_count_inc                 (grefcount       *rc);
0037 GLIB_AVAILABLE_IN_2_58
0038 gboolean        g_ref_count_dec                 (grefcount       *rc);
0039 GLIB_AVAILABLE_IN_2_58
0040 gboolean        g_ref_count_compare             (grefcount       *rc,
0041                                                  gint             val);
0042 
0043 GLIB_AVAILABLE_IN_2_58
0044 void            g_atomic_ref_count_init         (gatomicrefcount *arc);
0045 GLIB_AVAILABLE_IN_2_58
0046 void            g_atomic_ref_count_inc          (gatomicrefcount *arc);
0047 GLIB_AVAILABLE_IN_2_58
0048 gboolean        g_atomic_ref_count_dec          (gatomicrefcount *arc);
0049 GLIB_AVAILABLE_IN_2_58
0050 gboolean        g_atomic_ref_count_compare      (gatomicrefcount *arc,
0051                                                  gint             val);
0052 
0053 /**
0054  * G_REF_COUNT_INIT:
0055  *
0056  * Evaluates to the initial reference count for `grefcount`.
0057  *
0058  * This macro is useful for initializing `grefcount` fields inside
0059  * structures, for instance:
0060  *
0061  * |[<!-- language="C" -->
0062  * typedef struct {
0063  *   grefcount ref_count;
0064  *   char *name;
0065  *   char *address;
0066  * } Person;
0067  *
0068  * static const Person default_person = {
0069  *   .ref_count = G_REF_COUNT_INIT,
0070  *   .name = "Default name",
0071  *   .address = "Default address",
0072  * };
0073  * ]|
0074  *
0075  * Since: 2.78
0076  */
0077 #define G_REF_COUNT_INIT          -1 \
0078   GLIB_AVAILABLE_MACRO_IN_2_78
0079 
0080 /**
0081  * G_ATOMIC_REF_COUNT_INIT:
0082  *
0083  * Evaluates to the initial reference count for `gatomicrefcount`.
0084  *
0085  * This macro is useful for initializing `gatomicrefcount` fields inside
0086  * structures, for instance:
0087  *
0088  * |[<!-- language="C" -->
0089  * typedef struct {
0090  *   gatomicrefcount ref_count;
0091  *   char *name;
0092  *   char *address;
0093  * } Person;
0094  *
0095  * static const Person default_person = {
0096  *   .ref_count = G_ATOMIC_REF_COUNT_INIT,
0097  *   .name = "Default name",
0098  *   .address = "Default address",
0099  * };
0100  * ]|
0101  *
0102  * Since: 2.78
0103  */
0104 #define G_ATOMIC_REF_COUNT_INIT   1 \
0105   GLIB_AVAILABLE_MACRO_IN_2_78
0106 
0107 /* On GCC we can use __extension__ to inline the API without using
0108  * ancillary functions; we only do this when disabling checks, as
0109  * it disables warnings when saturating the reference counters
0110  */
0111 #if defined(__GNUC__) && defined(G_DISABLE_CHECKS)
0112 
0113 # define g_ref_count_init(rc) \
0114   (G_GNUC_EXTENSION ({ \
0115     G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
0116     (void) (0 ? *(rc) ^ *(rc) : 1); \
0117     *(rc) = -1; \
0118   }))
0119 
0120 # define g_ref_count_inc(rc) \
0121   (G_GNUC_EXTENSION ({ \
0122     G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
0123     (void) (0 ? *(rc) ^ *(rc) : 1); \
0124     if (*(rc) == G_MININT) ; else { \
0125       *(rc) -= 1; \
0126     } \
0127   }))
0128 
0129 # define g_ref_count_dec(rc) \
0130   (G_GNUC_EXTENSION ({ \
0131     G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
0132     grefcount __rc = *(rc); \
0133     __rc += 1; \
0134     if (__rc == 0) ; else { \
0135       *(rc) = __rc; \
0136     } \
0137     (gboolean) (__rc == 0); \
0138   }))
0139 
0140 # define g_ref_count_compare(rc,val) \
0141   (G_GNUC_EXTENSION ({ \
0142     G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
0143     (void) (0 ? *(rc) ^ (val) : 1); \
0144     (gboolean) (*(rc) == -(val)); \
0145   }))
0146 
0147 # define g_atomic_ref_count_init(rc) \
0148   (G_GNUC_EXTENSION ({ \
0149     G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
0150     (void) (0 ? *(rc) ^ *(rc) : 1); \
0151     *(rc) = 1; \
0152   }))
0153 
0154 # define g_atomic_ref_count_inc(rc) \
0155   (G_GNUC_EXTENSION ({ \
0156     G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
0157     (void) (0 ? *(rc) ^ *(rc) : 1); \
0158     (void) (g_atomic_int_get (rc) == G_MAXINT ? 0 : g_atomic_int_inc ((rc))); \
0159   }))
0160 
0161 # define g_atomic_ref_count_dec(rc) \
0162   (G_GNUC_EXTENSION ({ \
0163     G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
0164     (void) (0 ? *(rc) ^ *(rc) : 1); \
0165     g_atomic_int_dec_and_test ((rc)); \
0166   }))
0167 
0168 # define g_atomic_ref_count_compare(rc,val) \
0169   (G_GNUC_EXTENSION ({ \
0170     G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
0171     (void) (0 ? *(rc) ^ (val) : 1); \
0172     (gboolean) (g_atomic_int_get (rc) == (val)); \
0173   }))
0174 
0175 #endif /* __GNUC__ && G_DISABLE_CHECKS */
0176 
0177 G_END_DECLS
0178 
0179 #endif /* __GREFCOUNT_H__ */