1 /* grefcount.c: 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/>.
21 * @Title: Reference counting
22 * @Short_description: Reference counting types and functions
24 * Reference counting is a garbage collection mechanism that is based on
25 * assigning a counter to a data type, or any memory area; the counter is
26 * increased whenever a new reference to that data type is acquired, and
27 * decreased whenever the reference is released. Once the last reference
28 * is released, the resources associated to that data type are freed.
30 * GLib uses reference counting in many of its data types, and provides
31 * the #grefcount and #gatomicrefcount types to implement safe and atomic
32 * reference counting semantics in new data types.
34 * It is important to note that #grefcount and #gatomicrefcount should be
35 * considered completely opaque types; you should always use the provided
36 * API to increase and decrease the counters, and you should never check
37 * their content directly, or compare their content with other values.
44 #include "grefcount.h"
47 #include "gmessages.h"
52 * A type for implementing non-atomic reference count semantics.
54 * Use g_ref_count_init() to initialize it; g_ref_count_inc() to
55 * increase the counter, and g_ref_count_dec() to decrease it.
57 * It is safe to use #grefcount only if you're expecting to operate
58 * on the reference counter from a single thread. It is entirely up
59 * to you to ensure that all reference count changes happen in the
62 * See also: #gatomicrefcount
70 * A type for implementing atomic reference count semantics.
72 * Use g_atomic_ref_count_init() to initialize it; g_atomic_ref_count_inc()
73 * to increase the counter, and g_atomic_ref_count_dec() to decrease it.
75 * It is safe to use #gatomicrefcount if you're expecting to operate on the
76 * reference counter from multiple threads.
78 * See also: #grefcount
85 * @rc: the address of a reference count variable
87 * Initializes a reference count variable.
92 (g_ref_count_init
) (grefcount
*rc
)
94 g_return_if_fail (rc
!= NULL
);
96 /* Non-atomic refcounting is implemented using the negative range
99 * G_MININT Z¯< 0 > Z⁺ G_MAXINT
100 * |----------------------------|----------------------------|
102 * Acquiring a reference moves us towards MININT, and releasing a
103 * reference moves us towards 0.
110 * @rc: the address of a reference count variable
112 * Increases the reference count.
117 (g_ref_count_inc
) (grefcount
*rc
)
121 g_return_if_fail (rc
!= NULL
);
125 g_return_if_fail (rrc
< 0);
127 /* Check for saturation */
130 g_critical ("Reference count %p has reached saturation", rc
);
141 * @rc: the address of a reference count variable
143 * Decreases the reference count.
145 * Returns: %TRUE if the reference count reached 0, and %FALSE otherwise
150 (g_ref_count_dec
) (grefcount
*rc
)
154 g_return_val_if_fail (rc
!= NULL
, FALSE
);
158 g_return_val_if_fail (rrc
< 0, FALSE
);
170 * g_ref_count_compare:
171 * @rc: the address of a reference count variable
172 * @val: the value to compare
174 * Compares the current value of @rc with @val.
176 * Returns: %TRUE if the reference count is the same
182 (g_ref_count_compare
) (grefcount
*rc
,
187 g_return_val_if_fail (rc
!= NULL
, FALSE
);
188 g_return_val_if_fail (val
>= 0, FALSE
);
193 return rrc
== G_MININT
;
199 * g_atomic_ref_count_init:
200 * @arc: the address of an atomic reference count variable
202 * Atomically initializes a reference count variable.
207 (g_atomic_ref_count_init
) (gatomicrefcount
*arc
)
209 g_return_if_fail (arc
!= NULL
);
211 /* Atomic refcounting is implemented using the positive range
212 * of signed integers:
214 * G_MININT Z¯< 0 > Z⁺ G_MAXINT
215 * |----------------------------|----------------------------|
217 * Acquiring a reference moves us towards MAXINT, and releasing a
218 * reference moves us towards 0.
220 g_atomic_int_set (arc
, 1);
224 * g_atomic_ref_count_inc:
225 * @arc: the address of an atomic reference count variable
227 * Atomically increases the reference count.
232 (g_atomic_ref_count_inc
) (gatomicrefcount
*arc
)
234 g_return_if_fail (arc
!= NULL
);
235 g_return_if_fail (g_atomic_int_get (arc
) > 0);
237 if (g_atomic_int_get (arc
) == G_MAXINT
)
239 g_critical ("Reference count has reached saturation");
243 g_atomic_int_inc (arc
);
247 * g_atomic_ref_count_dec:
248 * @arc: the address of an atomic reference count variable
250 * Atomically decreases the reference count.
252 * Returns: %TRUE if the reference count reached 0, and %FALSE otherwise
257 (g_atomic_ref_count_dec
) (gatomicrefcount
*arc
)
259 g_return_val_if_fail (arc
!= NULL
, FALSE
);
260 g_return_val_if_fail (g_atomic_int_get (arc
) > 0, FALSE
);
262 return g_atomic_int_dec_and_test (arc
);
266 * g_atomic_ref_count_compare:
267 * @arc: the address of an atomic reference count variable
268 * @val: the value to compare
270 * Atomically compares the current value of @arc with @val.
272 * Returns: %TRUE if the reference count is the same
278 (g_atomic_ref_count_compare
) (gatomicrefcount
*arc
,
281 g_return_val_if_fail (arc
!= NULL
, FALSE
);
282 g_return_val_if_fail (val
>= 0, FALSE
);
284 return g_atomic_int_get (arc
) == val
;