tests: Fix leak when checking for du binary
[glib.git] / glib / grefcount.h
blobdec9a5ffb81738a64ece254e7b44727e09d334e4
1 /* grefcount.h: Reference counting
3 * Copyright 2018 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #ifndef __GREFCOUNT_H__
20 #define __GREFCOUNT_H__
22 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
24 #endif
26 #include <glib/gtypes.h>
28 G_BEGIN_DECLS
30 GLIB_AVAILABLE_IN_2_58
31 void g_ref_count_init (grefcount *rc);
32 GLIB_AVAILABLE_IN_2_58
33 void g_ref_count_inc (grefcount *rc);
34 GLIB_AVAILABLE_IN_2_58
35 gboolean g_ref_count_dec (grefcount *rc);
36 GLIB_AVAILABLE_IN_2_58
37 gboolean g_ref_count_compare (grefcount *rc,
38 gint val);
40 GLIB_AVAILABLE_IN_2_58
41 void g_atomic_ref_count_init (gatomicrefcount *arc);
42 GLIB_AVAILABLE_IN_2_58
43 void g_atomic_ref_count_inc (gatomicrefcount *arc);
44 GLIB_AVAILABLE_IN_2_58
45 gboolean g_atomic_ref_count_dec (gatomicrefcount *arc);
46 GLIB_AVAILABLE_IN_2_58
47 gboolean g_atomic_ref_count_compare (gatomicrefcount *arc,
48 gint val);
50 /* On GCC we can use __extension__ to inline the API without using
51 * ancillary functions; we only do this when disabling checks, as
52 * it disables warnings when saturating the reference counters
54 #if defined(__GNUC__) && defined(G_DISABLE_CHECKS)
56 # define g_ref_count_init(rc) \
57 (G_GNUC_EXTENSION ({ \
58 G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
59 (void) (0 ? *(rc) ^ *(rc) : 1); \
60 *(rc) = -1; \
61 }))
63 # define g_ref_count_inc(rc) \
64 (G_GNUC_EXTENSION ({ \
65 G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
66 (void) (0 ? *(rc) ^ *(rc) : 1); \
67 if (*(rc) == G_MININT) ; else { \
68 *(rc) -= 1; \
69 } \
70 }))
72 # define g_ref_count_dec(rc) \
73 (G_GNUC_EXTENSION ({ \
74 G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
75 grefcount __rc = *(rc); \
76 __rc += 1; \
77 if (__rc == 0) ; else { \
78 *(rc) = __rc; \
79 } \
80 (gboolean) (__rc == 0); \
81 }))
83 # define g_ref_count_compare(rc,val) \
84 (G_GNUC_EXTENSION ({ \
85 G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
86 (void) (0 ? *(rc) ^ (val) : 1); \
87 (gboolean) (*(rc) == -(val)); \
88 }))
90 # define g_atomic_ref_count_init(rc) \
91 (G_GNUC_EXTENSION ({ \
92 G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
93 (void) (0 ? *(rc) ^ *(rc) : 1); \
94 g_atomic_int_set ((rc), 1); \
95 }))
97 # define g_atomic_ref_count_inc(rc) \
98 (G_GNUC_EXTENSION ({ \
99 G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
100 (void) (0 ? *(rc) ^ *(rc) : 1); \
101 (void) (g_atomic_int_get (rc) == G_MAXINT ? 0 : g_atomic_int_inc ((rc))); \
104 # define g_atomic_ref_count_dec(rc) \
105 (G_GNUC_EXTENSION ({ \
106 G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
107 (void) (0 ? *(rc) ^ *(rc) : 1); \
108 g_atomic_int_dec_and_test ((rc)); \
111 # define g_atomic_ref_count_compare(rc,val) \
112 (G_GNUC_EXTENSION ({ \
113 G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
114 (void) (0 ? *(rc) ^ (val) : 1); \
115 (gboolean) (g_atomic_int_get (rc) == (val)); \
118 #endif /* __GNUC__ && G_DISABLE_CHECKS */
120 G_END_DECLS
122 #endif /* __GREFCOUNT_H__ */