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#include <glib/glib-typeof.h>
27
28G_BEGIN_DECLS
29
30GLIB_AVAILABLE_IN_2_58
31gpointer g_rc_box_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
32GLIB_AVAILABLE_IN_2_58
33gpointer g_rc_box_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
34GLIB_AVAILABLE_IN_2_58
35gpointer g_rc_box_dup (gsize block_size,
36 gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
37GLIB_AVAILABLE_IN_2_58
38gpointer g_rc_box_acquire (gpointer mem_block);
39GLIB_AVAILABLE_IN_2_58
40void g_rc_box_release (gpointer mem_block);
41GLIB_AVAILABLE_IN_2_58
42void g_rc_box_release_full (gpointer mem_block,
43 GDestroyNotify clear_func);
44
45GLIB_AVAILABLE_IN_2_58
46gsize g_rc_box_get_size (gpointer mem_block);
47
48GLIB_AVAILABLE_IN_2_58
49gpointer g_atomic_rc_box_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
50GLIB_AVAILABLE_IN_2_58
51gpointer g_atomic_rc_box_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
52GLIB_AVAILABLE_IN_2_58
53gpointer g_atomic_rc_box_dup (gsize block_size,
54 gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
55GLIB_AVAILABLE_IN_2_58
56gpointer g_atomic_rc_box_acquire (gpointer mem_block);
57GLIB_AVAILABLE_IN_2_58
58void g_atomic_rc_box_release (gpointer mem_block);
59GLIB_AVAILABLE_IN_2_58
60void g_atomic_rc_box_release_full (gpointer mem_block,
61 GDestroyNotify clear_func);
62
63GLIB_AVAILABLE_IN_2_58
64gsize g_atomic_rc_box_get_size (gpointer mem_block);
65
66#define g_rc_box_new(type) \
67 ((type *) g_rc_box_alloc (sizeof (type)))
68#define g_rc_box_new0(type) \
69 ((type *) g_rc_box_alloc0 (sizeof (type)))
70#define g_atomic_rc_box_new(type) \
71 ((type *) g_atomic_rc_box_alloc (sizeof (type)))
72#define g_atomic_rc_box_new0(type) \
73 ((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
74
75#if defined(glib_typeof)
76/* Type check to avoid assigning references to different types */
77#define g_rc_box_acquire(mem_block) \
78 ((glib_typeof (mem_block)) (g_rc_box_acquire) (mem_block))
79#define g_atomic_rc_box_acquire(mem_block) \
80 ((glib_typeof (mem_block)) (g_atomic_rc_box_acquire) (mem_block))
81
82/* Type check to avoid duplicating data to different types */
83#define g_rc_box_dup(block_size, mem_block) \
84 ((glib_typeof (mem_block)) (g_rc_box_dup) (block_size, mem_block))
85#define g_atomic_rc_box_dup(block_size, mem_block) \
86 ((glib_typeof (mem_block)) (g_atomic_rc_box_dup) (block_size, mem_block))
87#endif
88
89G_END_DECLS
90

source code of include/glib-2.0/glib/grcbox.h