1 /* grcboxprivate.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 #include "gtypes.h"
22 #include "grcbox.h"
23 
24 G_BEGIN_DECLS
25 
26 typedef struct {
27   grefcount ref_count;
28 
29   gsize mem_size;
30   gsize private_offset;
31 
32 #ifndef G_DISABLE_ASSERT
33   /* A "magic" number, used to perform additional integrity
34    * checks on the allocated data
35    */
36   guint32 magic;
37 #endif
38 } GRcBox;
39 
40 typedef struct {
41   gatomicrefcount ref_count;
42 
43   gsize mem_size;
44   gsize private_offset;
45 
46 #ifndef G_DISABLE_ASSERT
47   guint32 magic;
48 #endif
49 } GArcBox;
50 
51 #define G_BOX_MAGIC             0x44ae2bf0
52 
53 /* Keep the two refcounted boxes identical in size */
54 G_STATIC_ASSERT (sizeof (GRcBox) == sizeof (GArcBox));
55 
56 /* This is the default alignment we use when allocating the
57  * refcounted memory blocks; it's similar to the alignment
58  * guaranteed by the malloc() in GNU's libc and by the GSlice
59  * allocator
60  */
61 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
62 
63 #define G_RC_BOX_SIZE sizeof (GRcBox)
64 #define G_ARC_BOX_SIZE sizeof (GArcBox)
65 
66 gpointer        g_rc_box_alloc_full     (gsize    block_size,
67                                          gsize    alignment,
68                                          gboolean atomic,
69                                          gboolean clear);
70 
71 G_END_DECLS
72