1/* grcbox.h: Reference counted data
2 *
3 * Copyright 2018 Emmanuele Bassi
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19#pragma once
20
21#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22#error "Only <glib.h> can be included directly."
23#endif
24
25#include <glib/gmem.h>
26
27G_BEGIN_DECLS
28
29GLIB_AVAILABLE_IN_2_58
30gpointer g_rc_box_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
31GLIB_AVAILABLE_IN_2_58
32gpointer g_rc_box_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
33GLIB_AVAILABLE_IN_2_58
34gpointer g_rc_box_dup (gsize block_size,
35 gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
36GLIB_AVAILABLE_IN_2_58
37gpointer g_rc_box_acquire (gpointer mem_block);
38GLIB_AVAILABLE_IN_2_58
39void g_rc_box_release (gpointer mem_block);
40GLIB_AVAILABLE_IN_2_58
41void g_rc_box_release_full (gpointer mem_block,
42 GDestroyNotify clear_func);
43
44GLIB_AVAILABLE_IN_2_58
45gsize g_rc_box_get_size (gpointer mem_block);
46
47GLIB_AVAILABLE_IN_2_58
48gpointer g_atomic_rc_box_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
49GLIB_AVAILABLE_IN_2_58
50gpointer g_atomic_rc_box_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
51GLIB_AVAILABLE_IN_2_58
52gpointer g_atomic_rc_box_dup (gsize block_size,
53 gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
54GLIB_AVAILABLE_IN_2_58
55gpointer g_atomic_rc_box_acquire (gpointer mem_block);
56GLIB_AVAILABLE_IN_2_58
57void g_atomic_rc_box_release (gpointer mem_block);
58GLIB_AVAILABLE_IN_2_58
59void g_atomic_rc_box_release_full (gpointer mem_block,
60 GDestroyNotify clear_func);
61
62GLIB_AVAILABLE_IN_2_58
63gsize g_atomic_rc_box_get_size (gpointer mem_block);
64
65#define g_rc_box_new(type) \
66 ((type *) g_rc_box_alloc (sizeof (type)))
67#define g_rc_box_new0(type) \
68 ((type *) g_rc_box_alloc0 (sizeof (type)))
69#define g_atomic_rc_box_new(type) \
70 ((type *) g_atomic_rc_box_alloc (sizeof (type)))
71#define g_atomic_rc_box_new0(type) \
72 ((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
73
74#ifdef g_has_typeof
75/* Type check to avoid assigning references to different types */
76# define g_rc_box_acquire(mem_block) \
77 ((__typeof__(mem_block)) (g_rc_box_acquire) (mem_block))
78# define g_atomic_rc_box_acquire(mem_block) \
79 ((__typeof__(mem_block)) (g_atomic_rc_box_acquire) (mem_block))
80
81/* Type check to avoid duplicating data to different types */
82# define g_rc_box_dup(block_size,mem_block) \
83 ((__typeof__(mem_block)) (g_rc_box_dup) (block_size,mem_block))
84# define g_atomic_rc_box_dup(block_size,mem_block) \
85 ((__typeof__(mem_block)) (g_atomic_rc_box_dup) (block_size,mem_block))
86#endif
87
88G_END_DECLS
89