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/>.
23 #include "gmessages.h"
24 #include "grcboxprivate.h"
25 #include "grefcount.h"
27 #ifdef ENABLE_VALGRIND
35 * @Title: Reference counted data
36 * @Short_description: Allocated memory with reference counting semantics
38 * A "reference counted box", or "RcBox", is an opaque wrapper data type
39 * that is guaranteed to be as big as the size of a given data type, and
40 * which augments the given data type with reference counting semantics
41 * for its memory management.
43 * RcBox is useful if you have a plain old data type, like a structure
44 * typically placed on the stack, and you wish to provide additional API
45 * to use it on the heap, without necessarily implementing copy/free
46 * semantics, or your own reference counting.
50 * |[<!-- language="C" -->
56 * point_new (float x, float y)
58 * Point *res = g_rc_box_new (Point);
67 * Every time you wish to acquire a reference on the memory, you should
68 * call g_rc_box_acquire(); similarly, when you wish to release a reference
69 * you should call g_rc_box_release():
71 * |[<!-- language="C" -->
73 * point_ref (Point *p)
75 * return g_rc_box_acquire (p);
79 * point_unref (Point *p)
81 * g_rc_box_release (p);
85 * If you have additional memory allocated inside the structure, you can
86 * use g_rc_box_release_full(), which takes a function pointer, which
87 * will be called if the reference released was the last:
89 * |[<!-- language="C" -->
99 * person_clear (Person *p)
102 * g_free (p->address);
108 * person_unref (Person *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 (p);
122 * add_person_to_database (db, g_steal_pointer (&p));
125 * The reference counting operations on data allocated using g_rc_box_alloc(),
126 * g_rc_box_new(), and g_rc_box_dup() are not thread safe; it is your code's
127 * responsibility to ensure that references are acquired are released on the
133 #define G_RC_BOX(p) (GRcBox *) (((char *) (p)) - G_RC_BOX_SIZE)
135 /* We use the same alignment as GTypeInstance and GNU libc's malloc */
136 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
137 #define ALIGN_STRUCT(offset) ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
140 g_rc_box_alloc_full (gsize block_size
,
144 /* sizeof GArcBox == sizeof GRcBox */
145 gsize private_size
= G_ARC_BOX_SIZE
;
146 gsize real_size
= private_size
+ block_size
;
149 #ifdef ENABLE_VALGRIND
150 if (RUNNING_ON_VALGRIND
)
152 /* When running under Valgrind we massage the memory allocation
153 * to include a pointer at the tail end of the block; the pointer
154 * is then set to the start of the block. This trick allows
155 * Valgrind to keep track of the over-allocation and not be
156 * confused when passing the pointer around
158 private_size
+= ALIGN_STRUCT (1);
161 allocated
= g_malloc0 (real_size
+ sizeof (gpointer
));
163 allocated
= g_malloc (real_size
+ sizeof (gpointer
));
165 *(gpointer
*) (allocated
+ private_size
+ block_size
) = allocated
+ ALIGN_STRUCT (1);
167 VALGRIND_MALLOCLIKE_BLOCK (allocated
+ private_size
, block_size
+ sizeof (gpointer
), 0, TRUE
);
168 VALGRIND_MALLOCLIKE_BLOCK (allocated
+ ALIGN_STRUCT (1), private_size
- ALIGN_STRUCT (1), 0, TRUE
);
171 #endif /* ENABLE_VALGRIND */
174 allocated
= g_malloc0 (real_size
);
176 allocated
= g_malloc (real_size
);
181 GArcBox
*real_box
= (GArcBox
*) allocated
;
182 real_box
->mem_size
= block_size
;
183 #ifndef G_DISABLE_ASSERT
184 real_box
->magic
= G_BOX_MAGIC
;
186 g_atomic_ref_count_init (&real_box
->ref_count
);
190 GRcBox
*real_box
= (GRcBox
*) allocated
;
191 real_box
->mem_size
= block_size
;
192 #ifndef G_DISABLE_ASSERT
193 real_box
->magic
= G_BOX_MAGIC
;
195 g_ref_count_init (&real_box
->ref_count
);
198 return allocated
+ private_size
;
203 * @block_size: the size of the allocation
205 * Allocates @block_size bytes of memory, and adds reference
206 * counting semantics to it.
208 * The data will be freed when its reference count drops to
211 * Returns: a pointer to the allocated memory
216 g_rc_box_alloc (gsize block_size
)
218 g_return_val_if_fail (block_size
> 0, NULL
);
220 return g_rc_box_alloc_full (block_size
, FALSE
, FALSE
);
225 * @block_size: the size of the allocation
227 * Allocates @block_size bytes of memory, and adds reference
228 * counting semantics to it.
230 * The contents of the returned data is set to 0's.
232 * The data will be freed when its reference count drops to
235 * Returns: a pointer to the allocated memory
240 g_rc_box_alloc0 (gsize block_size
)
242 g_return_val_if_fail (block_size
> 0, NULL
);
244 return g_rc_box_alloc_full (block_size
, FALSE
, TRUE
);
249 * @type: the type to allocate, typically a structure name
251 * A convenience macro to allocate reference counted data with
252 * the size of the given @type.
254 * This macro calls g_rc_box_alloc() with `sizeof (@type)` and
255 * casts the returned pointer to a pointer of the given @type,
256 * avoiding a type cast in the source code.
258 * This macro cannot return %NULL, as the minimum allocation
259 * size from `sizeof (@type)` is 1 byte.
261 * Returns: (not nullable): a pointer to the allocated memory,
262 * cast to a pointer for the given @type
269 * @type: the type to allocate, typically a structure name
271 * A convenience macro to allocate reference counted data with
272 * the size of the given @type, and set its contents to 0.
274 * This macro calls g_rc_box_alloc0() with `sizeof (@type)` and
275 * casts the returned pointer to a pointer of the given @type,
276 * avoiding a type cast in the source code.
278 * This macro cannot return %NULL, as the minimum allocation
279 * size from `sizeof (@type)` is 1 byte.
281 * Returns: (not nullable): a pointer to the allocated memory,
282 * cast to a pointer for the given @type
289 * @block_size: the number of bytes to copy
290 * @mem_block: (not nullable): the memory to copy
292 * Allocates a new block of data with reference counting
293 * semantics, and copies @block_size bytes of @mem_block
296 * Returns: (not nullable): a pointer to the allocated memory
301 (g_rc_box_dup
) (gsize block_size
,
302 gconstpointer mem_block
)
306 g_return_val_if_fail (block_size
> 0, NULL
);
307 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
309 res
= g_rc_box_alloc_full (block_size
, FALSE
, FALSE
);
310 memcpy (res
, mem_block
, block_size
);
317 * @mem_block: (not nullable): a pointer to reference counted data
319 * Acquires a reference on the data pointed by @mem_block.
321 * Returns: (not nullabl): a pointer to the data, with its reference
327 (g_rc_box_acquire
) (gpointer mem_block
)
329 GRcBox
*real_box
= G_RC_BOX (mem_block
);
331 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
332 #ifndef G_DISABLE_ASSERT
333 g_return_val_if_fail (real_box
->magic
== G_BOX_MAGIC
, NULL
);
336 g_ref_count_inc (&real_box
->ref_count
);
343 * @mem_block: (not nullable): a pointer to reference counted data
345 * Releases a reference on the data pointed by @mem_block.
347 * If the reference was the last one, it will free the
348 * resources allocated for @mem_block.
353 g_rc_box_release (gpointer mem_block
)
355 GRcBox
*real_box
= G_RC_BOX (mem_block
);
357 g_return_if_fail (mem_block
!= NULL
);
358 #ifndef G_DISABLE_ASSERT
359 g_return_if_fail (real_box
->magic
== G_BOX_MAGIC
);
362 if (g_ref_count_dec (&real_box
->ref_count
))
367 * g_rc_box_release_full:
368 * @mem_block: (not nullabl): a pointer to reference counted data
369 * @clear_func: (not nullable): a function to call when clearing the data
371 * Releases a reference on the data pointed by @mem_block.
373 * If the reference was the last one, it will call @clear_func
374 * to clear the contents of @mem_block, and then will free the
375 * resources allocated for @mem_block.
380 g_rc_box_release_full (gpointer mem_block
,
381 GDestroyNotify clear_func
)
383 GRcBox
*real_box
= G_RC_BOX (mem_block
);
385 g_return_if_fail (mem_block
!= NULL
);
386 g_return_if_fail (clear_func
!= NULL
);
387 #ifndef G_DISABLE_ASSERT
388 g_return_if_fail (real_box
->magic
== G_BOX_MAGIC
);
391 if (g_ref_count_dec (&real_box
->ref_count
))
393 clear_func (mem_block
);