1 /* grcbox.c: 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"
25 #include "gtestutils.h"
27 #ifdef ENABLE_VALGRIND
31 #include "glib_trace.h"
37 * @Title: Reference counted data
38 * @Short_description: Allocated memory with reference counting semantics
40 * A "reference counted box", or "RcBox", is an opaque wrapper data type
41 * that is guaranteed to be as big as the size of a given data type, and
42 * which augments the given data type with reference counting semantics
43 * for its memory management.
45 * RcBox is useful if you have a plain old data type, like a structure
46 * typically placed on the stack, and you wish to provide additional API
47 * to use it on the heap; or if you want to implement a new type to be
48 * passed around by reference without necessarily implementing copy/free
49 * semantics or your own reference counting.
53 * |[<!-- language="C" -->
65 * return g_rc_box_new0 (Person);
69 * Every time you wish to acquire a reference on the memory, you should
70 * call g_rc_box_acquire(); similarly, when you wish to release a reference
71 * you should call g_rc_box_release():
73 * |[<!-- language="C" -->
74 * // Add a Person to the Database; the Database acquires ownership
75 * // of the Person instance
77 * add_person_to_database (Database *db, Person *p)
79 * db->persons = g_list_prepend (db->persons, g_rc_box_acquire (p));
82 * // Removes a Person from the Database; the reference acquired by
83 * // add_person_to_database() is released here
85 * remove_person_from_database (Database *db, Person *p)
87 * db->persons = g_list_remove (db->persons, p);
88 * g_rc_box_release (p);
92 * If you have additional memory allocated inside the structure, you can
93 * use g_rc_box_release_full(), which takes a function pointer, which
94 * will be called if the reference released was the last:
96 * |[<!-- language="C" -->
98 * person_clear (Person *p)
101 * g_free (p->address);
107 * remove_person_from_database (Database *db, Person *p)
109 * db->persons = g_list_remove (db->persons, p);
110 * g_rc_box_release_full (p, (GDestroyNotify) person_clear);
114 * If you wish to transfer the ownership of a reference counted data
115 * type without increasing the reference count, you can use g_steal_pointer():
117 * |[<!-- language="C" -->
118 * Person *p = g_rc_box_new (Person);
120 * // fill_person_details() is defined elsewhere
121 * fill_person_details (p);
123 * // add_person_to_database_no_ref() is defined elsewhere; it adds
124 * // a Person to the Database without taking a reference
125 * add_person_to_database_no_ref (db, g_steal_pointer (&p));
130 * The reference counting operations on data allocated using g_rc_box_alloc(),
131 * g_rc_box_new(), and g_rc_box_dup() are not thread safe; it is your code's
132 * responsibility to ensure that references are acquired are released on the
135 * If you need thread safe reference counting, see the [atomic reference counted
138 * ## Automatic pointer clean up
140 * If you want to add g_autoptr() support to your plain old data type through
141 * reference counting, you can use the G_DEFINE_AUTOPTR_CLEANUP_FUNC() and
142 * g_rc_box_release():
144 * |[<!-- language="C" -->
145 * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, g_rc_box_release)
148 * If you need to clear the contents of the data, you will need to use an
149 * ancillary function that calls g_rc_box_release_full():
151 * |[<!-- laguage="C" -->
153 * my_data_struct_release (MyDataStruct *data)
155 * // my_data_struct_clear() is defined elsewhere
156 * g_rc_box_release_full (data, (GDestroyNotify) my_data_struct_clear);
159 * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, my_data_struct_clear)
165 #define G_RC_BOX(p) (GRcBox *) (((char *) (p)) - G_RC_BOX_SIZE)
167 /* We use the same alignment as GTypeInstance and GNU libc's malloc */
168 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
169 #define ALIGN_STRUCT(offset) ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
172 g_rc_box_alloc_full (gsize block_size
,
176 /* sizeof GArcBox == sizeof GRcBox */
177 gsize private_size
= G_ARC_BOX_SIZE
;
181 g_assert (block_size
< (G_MAXSIZE
- G_ARC_BOX_SIZE
));
182 real_size
= private_size
+ block_size
;
184 #ifdef ENABLE_VALGRIND
185 if (RUNNING_ON_VALGRIND
)
187 /* When running under Valgrind we massage the memory allocation
188 * to include a pointer at the tail end of the block; the pointer
189 * is then set to the start of the block. This trick allows
190 * Valgrind to keep track of the over-allocation and not be
191 * confused when passing the pointer around
193 g_assert (private_size
< (G_MAXSIZE
- ALIGN_STRUCT (1)));
194 private_size
+= ALIGN_STRUCT (1);
197 allocated
= g_malloc0 (real_size
+ sizeof (gpointer
));
199 allocated
= g_malloc (real_size
+ sizeof (gpointer
));
201 *(gpointer
*) (allocated
+ private_size
+ block_size
) = allocated
+ ALIGN_STRUCT (1);
203 VALGRIND_MALLOCLIKE_BLOCK (allocated
+ private_size
, block_size
+ sizeof (gpointer
), 0, TRUE
);
204 VALGRIND_MALLOCLIKE_BLOCK (allocated
+ ALIGN_STRUCT (1), private_size
- ALIGN_STRUCT (1), 0, TRUE
);
207 #endif /* ENABLE_VALGRIND */
210 allocated
= g_malloc0 (real_size
);
212 allocated
= g_malloc (real_size
);
217 GArcBox
*real_box
= (GArcBox
*) allocated
;
218 real_box
->mem_size
= block_size
;
219 #ifndef G_DISABLE_ASSERT
220 real_box
->magic
= G_BOX_MAGIC
;
222 g_atomic_ref_count_init (&real_box
->ref_count
);
226 GRcBox
*real_box
= (GRcBox
*) allocated
;
227 real_box
->mem_size
= block_size
;
228 #ifndef G_DISABLE_ASSERT
229 real_box
->magic
= G_BOX_MAGIC
;
231 g_ref_count_init (&real_box
->ref_count
);
234 TRACE (GLIB_RCBOX_ALLOC (allocated
, block_size
, atomic
, clear
));
236 return allocated
+ private_size
;
241 * @block_size: the size of the allocation, must be greater than 0
243 * Allocates @block_size bytes of memory, and adds reference
244 * counting semantics to it.
246 * The data will be freed when its reference count drops to
249 * Returns: (transfer full) (not nullable): a pointer to the allocated memory
254 g_rc_box_alloc (gsize block_size
)
256 g_return_val_if_fail (block_size
> 0, NULL
);
258 return g_rc_box_alloc_full (block_size
, FALSE
, FALSE
);
263 * @block_size: the size of the allocation, must be greater than 0
265 * Allocates @block_size bytes of memory, and adds reference
266 * counting semantics to it.
268 * The contents of the returned data is set to zero.
270 * The data will be freed when its reference count drops to
273 * Returns: (transfer full) (not nullable): a pointer to the allocated memory
278 g_rc_box_alloc0 (gsize block_size
)
280 g_return_val_if_fail (block_size
> 0, NULL
);
282 return g_rc_box_alloc_full (block_size
, FALSE
, TRUE
);
287 * @type: the type to allocate, typically a structure name
289 * A convenience macro to allocate reference counted data with
290 * the size of the given @type.
292 * This macro calls g_rc_box_alloc() with `sizeof (@type)` and
293 * casts the returned pointer to a pointer of the given @type,
294 * avoiding a type cast in the source code.
296 * Returns: (transfer full) (not nullable): a pointer to the
297 * allocated memory, cast to a pointer for the given @type
304 * @type: the type to allocate, typically a structure name
306 * A convenience macro to allocate reference counted data with
307 * the size of the given @type, and set its contents to zero.
309 * This macro calls g_rc_box_alloc0() with `sizeof (@type)` and
310 * casts the returned pointer to a pointer of the given @type,
311 * avoiding a type cast in the source code.
313 * Returns: (transfer full) (not nullable): a pointer to the
314 * allocated memory, cast to a pointer for the given @type
321 * @block_size: the number of bytes to copy, must be greater than 0
322 * @mem_block: (not nullable): the memory to copy
324 * Allocates a new block of data with reference counting
325 * semantics, and copies @block_size bytes of @mem_block
328 * Returns: (transfer full) (not nullable): a pointer to the allocated
334 (g_rc_box_dup
) (gsize block_size
,
335 gconstpointer mem_block
)
339 g_return_val_if_fail (block_size
> 0, NULL
);
340 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
342 res
= g_rc_box_alloc_full (block_size
, FALSE
, FALSE
);
343 memcpy (res
, mem_block
, block_size
);
350 * @mem_block: (not nullable): a pointer to reference counted data
352 * Acquires a reference on the data pointed by @mem_block.
354 * Returns: (transfer full) (not nullable): a pointer to the data,
355 * with its reference count increased
360 (g_rc_box_acquire
) (gpointer mem_block
)
362 GRcBox
*real_box
= G_RC_BOX (mem_block
);
364 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
365 #ifndef G_DISABLE_ASSERT
366 g_return_val_if_fail (real_box
->magic
== G_BOX_MAGIC
, NULL
);
369 g_ref_count_inc (&real_box
->ref_count
);
371 TRACE (GLIB_RCBOX_ACQUIRE (mem_block
, 0));
378 * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
380 * Releases a reference on the data pointed by @mem_block.
382 * If the reference was the last one, it will free the
383 * resources allocated for @mem_block.
388 g_rc_box_release (gpointer mem_block
)
390 g_rc_box_release_full (mem_block
, NULL
);
394 * g_rc_box_release_full:
395 * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
396 * @clear_func: (not nullable): a function to call when clearing the data
398 * Releases a reference on the data pointed by @mem_block.
400 * If the reference was the last one, it will call @clear_func
401 * to clear the contents of @mem_block, and then will free the
402 * resources allocated for @mem_block.
407 g_rc_box_release_full (gpointer mem_block
,
408 GDestroyNotify clear_func
)
410 GRcBox
*real_box
= G_RC_BOX (mem_block
);
412 g_return_if_fail (mem_block
!= NULL
);
413 #ifndef G_DISABLE_ASSERT
414 g_return_if_fail (real_box
->magic
== G_BOX_MAGIC
);
417 if (g_ref_count_dec (&real_box
->ref_count
))
419 TRACE (GLIB_RCBOX_RELEASE (mem_block
, 0));
421 if (clear_func
!= NULL
)
422 clear_func (mem_block
);
424 TRACE (GLIB_RCBOX_FREE (mem_block
));
431 * @mem_block: (not nullable): a pointer to reference counted data
433 * Retrieves the size of the reference counted data pointed by @mem_block.
435 * Returns: the size of the data, in bytes
440 g_rc_box_get_size (gpointer mem_block
)
442 GRcBox
*real_box
= G_RC_BOX (mem_block
);
444 g_return_val_if_fail (mem_block
!= NULL
, 0);
445 #ifndef G_DISABLE_ASSERT
446 g_return_val_if_fail (real_box
->magic
== G_BOX_MAGIC
, 0);
449 return real_box
->mem_size
;