Imported Upstream version 2.74.3
[platform/upstream/glib.git] / glib / grcbox.h
1 /* grcbox.h: Reference counted data
2  *
3  * Copyright 2018  Emmanuele Bassi
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #pragma once
22
23 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
24 #error "Only <glib.h> can be included directly."
25 #endif
26
27 #include <glib/gmem.h>
28 #include <glib/glib-typeof.h>
29
30 G_BEGIN_DECLS
31
32 GLIB_AVAILABLE_IN_2_58
33 gpointer        g_rc_box_alloc                  (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
34 GLIB_AVAILABLE_IN_2_58
35 gpointer        g_rc_box_alloc0                 (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
36 GLIB_AVAILABLE_IN_2_58
37 gpointer        g_rc_box_dup                    (gsize           block_size,
38                                                  gconstpointer   mem_block) G_GNUC_ALLOC_SIZE(1);
39 GLIB_AVAILABLE_IN_2_58
40 gpointer        g_rc_box_acquire                (gpointer        mem_block);
41 GLIB_AVAILABLE_IN_2_58
42 void            g_rc_box_release                (gpointer        mem_block);
43 GLIB_AVAILABLE_IN_2_58
44 void            g_rc_box_release_full           (gpointer        mem_block,
45                                                  GDestroyNotify  clear_func);
46
47 GLIB_AVAILABLE_IN_2_58
48 gsize           g_rc_box_get_size               (gpointer        mem_block);
49
50 GLIB_AVAILABLE_IN_2_58
51 gpointer        g_atomic_rc_box_alloc           (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
52 GLIB_AVAILABLE_IN_2_58
53 gpointer        g_atomic_rc_box_alloc0          (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
54 GLIB_AVAILABLE_IN_2_58
55 gpointer        g_atomic_rc_box_dup             (gsize           block_size,
56                                                  gconstpointer   mem_block) G_GNUC_ALLOC_SIZE(1);
57 GLIB_AVAILABLE_IN_2_58
58 gpointer        g_atomic_rc_box_acquire         (gpointer        mem_block);
59 GLIB_AVAILABLE_IN_2_58
60 void            g_atomic_rc_box_release         (gpointer        mem_block);
61 GLIB_AVAILABLE_IN_2_58
62 void            g_atomic_rc_box_release_full    (gpointer        mem_block,
63                                                  GDestroyNotify  clear_func);
64
65 GLIB_AVAILABLE_IN_2_58
66 gsize           g_atomic_rc_box_get_size        (gpointer        mem_block);
67
68 #define g_rc_box_new(type) \
69   ((type *) g_rc_box_alloc (sizeof (type)))
70 #define g_rc_box_new0(type) \
71   ((type *) g_rc_box_alloc0 (sizeof (type)))
72 #define g_atomic_rc_box_new(type) \
73   ((type *) g_atomic_rc_box_alloc (sizeof (type)))
74 #define g_atomic_rc_box_new0(type) \
75   ((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
76
77 #if defined(glib_typeof)
78 /* Type check to avoid assigning references to different types */
79 #define g_rc_box_acquire(mem_block) \
80   ((glib_typeof (mem_block)) (g_rc_box_acquire) (mem_block))
81 #define g_atomic_rc_box_acquire(mem_block) \
82   ((glib_typeof (mem_block)) (g_atomic_rc_box_acquire) (mem_block))
83
84 /* Type check to avoid duplicating data to different types */
85 #define g_rc_box_dup(block_size, mem_block) \
86   ((glib_typeof (mem_block)) (g_rc_box_dup) (block_size, mem_block))
87 #define g_atomic_rc_box_dup(block_size, mem_block) \
88   ((glib_typeof (mem_block)) (g_atomic_rc_box_dup) (block_size, mem_block))
89 #endif
90
91 G_END_DECLS