1 /* grcbox.h: 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 "grefcount.h"
26 #ifdef ENABLE_VALGRIND
34 * @Title: Reference counted data
35 * @Short_description: Allocated memory with reference counting semantics
37 * A "reference counted box", or "RcBox", is an opaque wrapper data type
38 * that is guaranteed to be as big as the size of a given data type, and
39 * which augments the given data type with reference counting semantics
40 * for its memory management.
42 * RcBox is useful if you have a plain old data type, like a structure
43 * typically placed on the stack, and you wish to provide additional API
44 * to use it on the heap, without necessarily implementing copy/free
45 * semantics, or your own reference counting.
49 * |[<!-- language="C" -->
55 * point_new (float x, float y)
57 * Point *res = g_rc_box_new (Point);
66 * Every time you wish to acquire a reference on the memory, you should
67 * call g_rc_box_acquire(); similarly, when you wish to release a reference
68 * you should call g_rc_box_release():
70 * |[<!-- language="C" -->
72 * point_ref (Point *p)
74 * return g_rc_box_acquire (p);
78 * point_unref (Point *p)
80 * g_rc_box_release (p);
84 * If you have additional memory allocated inside the structure, you can
85 * use g_rc_box_release_full(), which takes a function pointer, which
86 * will be called if the reference released was the last:
88 * |[<!-- language="C" -->
98 * person_clear (Person *p)
101 * g_free (p->address);
107 * person_unref (Person *p)
109 * g_rc_box_release_full (p, (GDestroyNotify) person_clear);
113 * If you wish to transfer the ownership of a reference counted data
114 * type without increasing the reference count, you can use g_steal_pointer():
116 * |[<!-- language="C" -->
117 * Person *p = g_rc_box_new (Person);
119 * fill_person_details (p);
121 * add_person_to_database (db, g_steal_pointer (&p));
124 * The reference counting operations on data allocated using g_rc_box_alloc(),
125 * g_rc_box_new(), and g_rc_box_dup() are not thread safe; it is your code's
126 * responsibility to ensure that references are acquired are released on the
137 #ifndef G_DISABLE_ASSERT
138 /* A "magic" number, used to perform additional integrity
139 * checks on the allocated data
145 #define G_RC_BOX_MAGIC 0x44ae2bf0
146 #define G_RC_BOX_SIZE sizeof (GRcBox)
147 #define G_RC_BOX(p) (GRcBox *) (((char *) (p)) - G_RC_BOX_SIZE)
149 /* We use the same alignment as GTypeInstance and GNU libc's malloc */
150 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
151 #define ALIGN_STRUCT(offset) ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
154 g_rc_box_alloc_full (gsize block_size
,
157 gsize private_size
= G_RC_BOX_SIZE
;
158 gsize real_size
= private_size
+ block_size
;
162 #ifdef ENABLE_VALGRIND
163 if (RUNNING_ON_VALGRIND
)
165 /* When running under Valgrind we massage the memory allocation
166 * to include a pointer at the tail end of the block; the pointer
167 * is then set to the start of the block. This trick allows
168 * Valgrind to keep track of the over-allocation and not be
169 * confused when passing the pointer around
171 private_size
+= ALIGN_STRUCT (1);
174 allocated
= g_malloc0 (real_size
+ sizeof (gpointer
));
176 allocated
= g_malloc (real_size
+ sizeof (gpointer
));
178 *(gpointer
*) (allocated
+ private_size
+ block_size
) = allocated
+ ALIGN_STRUCT (1);
180 VALGRIND_MALLOCLIKE_BLOCK (allocated
+ private_size
, block_size
+ sizeof (gpointer
), 0, TRUE
);
181 VALGRIND_MALLOCLIKE_BLOCK (allocated
+ ALIGN_STRUCT (1), private_size
- ALIGN_STRUCT (1), 0, TRUE
);
184 #endif /* ENABLE_VALGRIND */
187 allocated
= g_malloc0 (real_size
);
189 allocated
= g_malloc (real_size
);
192 real_box
= (GRcBox
*) allocated
;
193 real_box
->mem_size
= block_size
;
194 #ifndef G_DISABLE_ASSERT
195 real_box
->magic
= G_RC_BOX_MAGIC
;
197 g_ref_count_init (&real_box
->ref_count
);
199 return allocated
+ private_size
;
204 * @block_size: the size of the allocation
206 * Allocates @block_size bytes of memory, and adds reference
207 * counting semantics to it.
209 * The data will be freed when its reference count drops to
212 * Returns: a pointer to the allocated memory
217 g_rc_box_alloc (gsize block_size
)
219 g_return_val_if_fail (block_size
> 0, NULL
);
221 return g_rc_box_alloc_full (block_size
, FALSE
);
226 * @block_size: the size of the allocation
228 * Allocates @block_size bytes of memory, and adds reference
229 * counting semantics to it.
231 * The contents of the returned data is set to 0's.
233 * The data will be freed when its reference count drops to
236 * Returns: a pointer to the allocated memory
241 g_rc_box_alloc0 (gsize block_size
)
243 g_return_val_if_fail (block_size
> 0, NULL
);
245 return g_rc_box_alloc_full (block_size
, TRUE
);
250 * @type: the type to allocate, typically a structure name
252 * A convenience macro to allocate reference counted data with
253 * the size of the given @type.
255 * This macro calls g_rc_box_alloc() with `sizeof (@type)` and
256 * casts the returned pointer to a pointer of the given @type,
257 * avoiding a type cast in the source code.
259 * This macro cannot return %NULL, as the minimum allocation
260 * size from `sizeof (@type)` is 1 byte.
262 * Returns: (not nullable): a pointer to the allocated memory,
263 * cast to a pointer for the given @type
270 * @type: the type to allocate, typically a structure name
272 * A convenience macro to allocate reference counted data with
273 * the size of the given @type, and set its contents to 0.
275 * This macro calls g_rc_box_alloc0() with `sizeof (@type)` and
276 * casts the returned pointer to a pointer of the given @type,
277 * avoiding a type cast in the source code.
279 * This macro cannot return %NULL, as the minimum allocation
280 * size from `sizeof (@type)` is 1 byte.
282 * Returns: (not nullable): a pointer to the allocated memory,
283 * cast to a pointer for the given @type
290 * @mem_block: (not nullable): a pointer to reference counted data
292 * Allocates a new block of data with reference counting
293 * semantics, and copies the contents of @mem_block into
296 * Returns: (not nullable): a pointer to the allocated memory
301 (g_rc_box_dup
) (gpointer mem_block
)
303 GRcBox
*real_box
= G_RC_BOX (mem_block
);
306 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
307 #ifndef G_DISABLE_ASSERT
308 g_return_val_if_fail (real_box
->magic
== G_RC_BOX_MAGIC
, NULL
);
311 res
= g_rc_box_alloc_full (real_box
->mem_size
, FALSE
);
312 memcpy (res
, mem_block
, real_box
->mem_size
);
319 * @mem_block: (not nullable): a pointer to reference counted data
321 * Acquires a reference on the data pointed by @mem_block.
323 * Returns: (not nullabl): a pointer to the data, with its reference
329 (g_rc_box_acquire
) (gpointer mem_block
)
331 GRcBox
*real_box
= G_RC_BOX (mem_block
);
333 g_return_val_if_fail (mem_block
!= NULL
, NULL
);
334 #ifndef G_DISABLE_ASSERT
335 g_return_val_if_fail (real_box
->magic
== G_RC_BOX_MAGIC
, NULL
);
338 g_ref_count_inc (&real_box
->ref_count
);
345 * @mem_block: (not nullable): a pointer to reference counted data
347 * Releases a reference on the data pointed by @mem_block.
349 * If the reference was the last one, it will free the
350 * resources allocated for @mem_block.
355 g_rc_box_release (gpointer mem_block
)
357 GRcBox
*real_box
= G_RC_BOX (mem_block
);
359 g_return_if_fail (mem_block
!= NULL
);
360 #ifndef G_DISABLE_ASSERT
361 g_return_if_fail (real_box
->magic
== G_RC_BOX_MAGIC
);
364 if (g_ref_count_dec (&real_box
->ref_count
))
369 * g_rc_box_release_full:
370 * @mem_block: (not nullabl): a pointer to reference counted data
371 * @clear_func: (not nullable): a function to call when clearing the data
373 * Releases a reference on the data pointed by @mem_block.
375 * If the reference was the last one, it will call @clear_func
376 * to clear the contents of @mem_block, and then will free the
377 * resources allocated for @mem_block.
382 g_rc_box_release_full (gpointer mem_block
,
383 GDestroyNotify clear_func
)
385 GRcBox
*real_box
= G_RC_BOX (mem_block
);
387 g_return_if_fail (mem_block
!= NULL
);
388 g_return_if_fail (clear_func
!= NULL
);
389 #ifndef G_DISABLE_ASSERT
390 g_return_if_fail (real_box
->magic
== G_RC_BOX_MAGIC
);
393 if (g_ref_count_dec (&real_box
->ref_count
))
395 clear_func (mem_block
);