1 /* garcbox.c: Atomically reference counted data
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 #include "grcboxprivate.h"
23 #include "gmessages.h"
24 #include "grefcount.h"
26 #ifdef ENABLE_VALGRIND
30 #include "glib_trace.h"
34 #define G_ARC_BOX(p) (GArcBox *) (((char *) (p)) - G_ARC_BOX_SIZE)
38 * @Title: Atomically reference counted data
39 * @Short_description: Allocated memory with atomic reference counting semantics
41 * An "atomically reference counted box", or "ArcBox", is an opaque wrapper
42 * data type that is guaranteed to be as big as the size of a given data type,
43 * and which augments the given data type with thread safe reference counting
44 * semantics for its memory management.
46 * ArcBox is useful if you have a plain old data type, like a structure
47 * typically placed on the stack, and you wish to provide additional API
48 * to use it on the heap; or if you want to implement a new type to be
49 * passed around by reference without necessarily implementing copy/free
50 * semantics or your own reference counting.
54 * |[<!-- language="C" -->
66 * return g_atomic_rc_box_new0 (Person);
70 * Every time you wish to acquire a reference on the memory, you should
71 * call g_atomic_rc_box_acquire(); similarly, when you wish to release a reference
72 * you should call g_atomic_rc_box_release():
74 * |[<!-- language="C" -->
75 * // Add a Person to the Database; the Database acquires ownership
76 * // of the Person instance
78 * add_person_to_database (Database *db, Person *p)
80 * db->persons = g_list_prepend (db->persons, g_atomic_rc_box_acquire (p));
83 * // Removes a Person from the Database; the reference acquired by
84 * // add_person_to_database() is released here
86 * remove_person_from_database (Database *db, Person *p)
88 * db->persons = g_list_remove (db->persons, p);
89 * g_atomic_rc_box_release (p);
93 * If you have additional memory allocated inside the structure, you can
94 * use g_atomic_rc_box_release_full(), which takes a function pointer, which
95 * will be called if the reference released was the last:
97 * |[<!-- language="C" -->
99 * person_clear (Person *p)
102 * g_free (p->address);
108 * remove_person_from_database (Database *db, Person *p)
110 * db->persons = g_list_remove (db->persons, p);
111 * g_atomic_rc_box_release_full (p, (GDestroyNotify) person_clear);
115 * If you wish to transfer the ownership of a reference counted data
116 * type without increasing the reference count, you can use g_steal_pointer():
118 * |[<!-- language="C" -->
119 * Person *p = g_atomic_rc_box_new (Person);
121 * fill_person_details (p);
123 * add_person_to_database (db, g_steal_pointer (&p));
128 * The reference counting operations on data allocated using g_atomic_rc_box_alloc(),
129 * g_atomic_rc_box_new(), and g_atomic_rc_box_dup() are guaranteed to be atomic, and thus
130 * can be safely be performed by different threads. It is important to note that
131 * only the reference acquisition and release are atomic; changes to the content
132 * of the data are your responsibility.
134 * ## Automatic pointer clean up
136 * If you want to add g_autoptr() support to your plain old data type through
137 * reference counting, you can use the G_DEFINE_AUTOPTR_CLEANUP_FUNC() and
138 * g_atomic_rc_box_release():
140 * |[<!-- language="C" -->
141 * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, g_atomic_rc_box_release)
144 * If you need to clear the contents of the data, you will need to use an
145 * ancillary function that calls g_rc_box_release_full():
147 * |[<!-- laguage="C" -->
149 * my_data_struct_release (MyDataStruct *data)
151 * // my_data_struct_clear() is defined elsewhere
152 * g_atomic_rc_box_release_full (data, (GDestroyNotify) my_data_struct_clear);
155 * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, my_data_struct_clear)
162 * g_atomic_rc_box_alloc:
163 * @block_size: the size of the allocation, must be greater than 0
165 * Allocates @block_size bytes of memory, and adds atomic
166 * reference counting semantics to it.
168 * The data will be freed when its reference count drops to
171 * Returns: (transfer full) (not nullable): a pointer to the allocated memory
176 g_atomic_rc_box_alloc (gsize block_size
)
178 g_return_val_if_fail (block_size
> 0, NULL
);
180 return g_rc_box_alloc_full (block_size
, TRUE
, FALSE
);
184 * g_atomic_rc_box_alloc0:
185 * @block_size: the size of the allocation, must be greater than 0
187 * Allocates @block_size bytes of memory, and adds atomic
188 * referenc counting semantics to it.
190 * The contents of the returned data is set to zero.
192 * The data will be freed when its reference count drops to
195 * Returns: (transfer full) (not nullable): a pointer to the allocated memory
200 g_atomic_rc_box_alloc0 (gsize block_size
)
202 g_return_val_if_fail (block_size
> 0, NULL
);
204 return g_rc_box_alloc_full (block_size
, TRUE
, TRUE
);
208 * g_atomic_rc_box_new:
209 * @type: the type to allocate, typically a structure name
211 * A convenience macro to allocate atomically reference counted
212 * data with the size of the given @type.
214 * This macro calls g_atomic_rc_box_alloc() with `sizeof (@type)` and
215 * casts the returned pointer to a pointer of the given @type,
216 * avoiding a type cast in the source code.
218 * Returns: (transfer full) (not nullable): a pointer to the allocated
219 * memory, cast to a pointer for the given @type
225 * g_atomic_rc_box_new0:
226 * @type: the type to allocate, typically a structure name
228 * A convenience macro to allocate atomically reference counted
229 * data with the size of the given @type, and set its contents
232 * This macro calls g_atomic_rc_box_alloc0() with `sizeof (@type)` and
233 * casts the returned pointer to a pointer of the given @type,
234 * avoiding a type cast in the source code.
236 * Returns: (transfer full) (not nullable): a pointer to the allocated
237 * memory, cast to a pointer for the given @type
243 * g_atomic_rc_box_dup:
244 * @block_size: the number of bytes to copy, must be greater than 0
245 * @mem_block: (not nullable): the memory to copy
247 * Allocates a new block of data with atomit reference counting
248 * semantics, and copies @block_size bytes of @mem_block
251 * Returns: (transfer full) (not nullable): a pointer to the allocated
257 (g_atomic_rc_box_dup
) (gsize block_size
,
258 gconstpointer mem_block
)
262 g_return_val_if_fail (block_size
> 0, NULL
);
263 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
265 res
= g_rc_box_alloc_full (block_size
, TRUE
, FALSE
);
266 memcpy (res
, mem_block
, block_size
);
272 * g_atomic_rc_box_acquire:
273 * @mem_block: (not nullable): a pointer to reference counted data
275 * Atomically acquires a reference on the data pointed by @mem_block.
277 * Returns: (transfer full) (not nullable): a pointer to the data,
278 * with its reference count increased
283 (g_atomic_rc_box_acquire
) (gpointer mem_block
)
285 GArcBox
*real_box
= G_ARC_BOX (mem_block
);
287 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
288 #ifndef G_DISABLE_ASSERT
289 g_return_val_if_fail (real_box
->magic
== G_BOX_MAGIC
, NULL
);
292 g_atomic_ref_count_inc (&real_box
->ref_count
);
294 TRACE (GLIB_RCBOX_ACQUIRE (mem_block
, 1));
300 * g_atomic_rc_box_release:
301 * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
303 * Atomically releases a reference on the data pointed by @mem_block.
305 * If the reference was the last one, it will free the
306 * resources allocated for @mem_block.
311 g_atomic_rc_box_release (gpointer mem_block
)
313 g_atomic_rc_box_release_full (mem_block
, NULL
);
317 * g_atomic_rc_box_release_full:
318 * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
319 * @clear_func: (not nullable): a function to call when clearing the data
321 * Atomically releases a reference on the data pointed by @mem_block.
323 * If the reference was the last one, it will call @clear_func
324 * to clear the contents of @mem_block, and then will free the
325 * resources allocated for @mem_block.
330 g_atomic_rc_box_release_full (gpointer mem_block
,
331 GDestroyNotify clear_func
)
333 GArcBox
*real_box
= G_ARC_BOX (mem_block
);
335 g_return_if_fail (mem_block
!= NULL
);
336 #ifndef G_DISABLE_ASSERT
337 g_return_if_fail (real_box
->magic
== G_BOX_MAGIC
);
340 if (g_atomic_ref_count_dec (&real_box
->ref_count
))
342 TRACE (GLIB_RCBOX_RELEASE (mem_block
, 1));
344 if (clear_func
!= NULL
)
345 clear_func (mem_block
);
347 TRACE (GLIB_RCBOX_FREE (mem_block
));
353 * g_atomic_rc_box_get_size:
354 * @mem_block: (not nullable): a pointer to reference counted data
356 * Retrieves the size of the reference counted data pointed by @mem_block.
358 * Returns: the size of the data, in bytes
363 g_atomic_rc_box_get_size (gpointer mem_block
)
365 GArcBox
*real_box
= G_ARC_BOX (mem_block
);
367 g_return_val_if_fail (mem_block
!= NULL
, 0);
368 #ifndef G_DISABLE_ASSERT
369 g_return_val_if_fail (real_box
->magic
== G_BOX_MAGIC
, 0);
372 return real_box
->mem_size
;