Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/glib/grcbox.c
Line
Count
Source
1
/* grcbox.c: 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
#include "config.h"
22
23
#include "grcboxprivate.h"
24
25
#include "gmessages.h"
26
#include "grefcount.h"
27
#include "gtestutils.h"
28
29
#ifdef ENABLE_VALGRIND
30
#include "valgrind.h"
31
#endif
32
33
#include "glib_trace.h"
34
35
#include <string.h>
36
37
/* We use the same alignment as GTypeInstance and GNU libc's malloc */
38
#define ALIGN_STRUCT(offset)    ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
39
40
0
#define G_RC_BOX(p)             (GRcBox *) (((char *) (p)) - G_RC_BOX_SIZE)
41
42
gpointer
43
g_rc_box_alloc_full (gsize    block_size,
44
                     gsize    alignment,
45
                     gboolean atomic,
46
                     gboolean clear)
47
0
{
48
  /* We don't do an (atomic ? G_ARC_BOX_SIZE : G_RC_BOX_SIZE) check, here
49
   * because we have a static assertion that sizeof(GArcBox) == sizeof(GRcBox)
50
   * inside grcboxprivate.h, and we don't want the compiler to unnecessarily
51
   * warn about both branches of the conditional yielding identical results
52
   */
53
0
  gsize private_size = G_ARC_BOX_SIZE;
54
0
  gsize private_offset = 0;
55
0
  gsize real_size;
56
0
  char *allocated;
57
58
0
  g_assert (alignment != 0);
59
60
  /* We need to ensure that the private data is aligned */
61
0
  if (private_size % alignment != 0)
62
0
    {
63
0
      private_offset = private_size % alignment;
64
0
      private_size += (alignment - private_offset);
65
0
    }
66
67
0
  if (block_size >= (G_MAXSIZE - private_size))
68
0
    {
69
0
      g_error ("%s: overflow allocating %s of %"G_GSIZE_FORMAT" bytes",
70
0
               G_STRLOC, atomic ? "GArcBox" : "GRcBox", block_size);
71
0
    }
72
0
  real_size = private_size + block_size;
73
74
  /* The real allocated size must be a multiple of @alignment, to
75
   * maintain the alignment of block_size
76
   */
77
0
  if (real_size % alignment != 0)
78
0
    {
79
0
      gsize offset = real_size % alignment;
80
0
      g_assert (real_size < (G_MAXSIZE - (alignment - offset)));
81
0
      real_size += (alignment - offset);
82
0
    }
83
84
#ifdef ENABLE_VALGRIND
85
  if (RUNNING_ON_VALGRIND)
86
    {
87
      /* When running under Valgrind we massage the memory allocation
88
       * to include a pointer at the tail end of the block; the pointer
89
       * is then set to the start of the block. This trick allows
90
       * Valgrind to keep track of the over-allocation and not be
91
       * confused when passing the pointer around
92
       */
93
      g_assert (private_size < (G_MAXSIZE - ALIGN_STRUCT (1)));
94
      private_size += ALIGN_STRUCT (1);
95
96
      if (clear)
97
        allocated = g_malloc0 (real_size + sizeof (gpointer));
98
      else
99
        allocated = g_malloc (real_size + sizeof (gpointer));
100
101
      *(gpointer *) (allocated + private_size + block_size) = allocated + ALIGN_STRUCT (1);
102
103
      VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, block_size + sizeof (gpointer), 0, TRUE);
104
      VALGRIND_MALLOCLIKE_BLOCK (allocated + ALIGN_STRUCT (1), private_size - ALIGN_STRUCT (1), 0, TRUE);
105
    }
106
  else
107
#endif /* ENABLE_VALGRIND */
108
0
    {
109
0
      if (clear)
110
0
        allocated = g_malloc0 (real_size);
111
0
      else
112
0
        allocated = g_malloc (real_size);
113
0
    }
114
115
0
  if (atomic)
116
0
    {
117
      /* We leave the alignment padding at the top of the allocation,
118
       * so we have an in memory layout of:
119
       *
120
       *  |[ offset ][ sizeof(GArcBox) ]||[ block_size ]|
121
       */
122
0
      GArcBox *real_box = (GArcBox *) (allocated + private_offset);
123
      /* Store the real size */
124
0
      real_box->mem_size = block_size;
125
      /* Store the alignment offset, to be used when freeing the
126
       * allocated block
127
       */
128
0
      real_box->private_offset = private_offset;
129
0
#ifndef G_DISABLE_ASSERT
130
0
      real_box->magic = G_BOX_MAGIC;
131
0
#endif
132
0
      g_atomic_ref_count_init (&real_box->ref_count);
133
0
    }
134
0
  else
135
0
    {
136
      /* We leave the alignment padding at the top of the allocation,
137
       * so we have an in memory layout of:
138
       *
139
       *  |[ offset ][ sizeof(GRcBox) ]||[ block_size ]|
140
       */
141
0
      GRcBox *real_box = (GRcBox *) (allocated + private_offset);
142
      /* Store the real size */
143
0
      real_box->mem_size = block_size;
144
      /* Store the alignment offset, to be used when freeing the
145
       * allocated block
146
       */
147
0
      real_box->private_offset = private_offset;
148
0
#ifndef G_DISABLE_ASSERT
149
0
      real_box->magic = G_BOX_MAGIC;
150
0
#endif
151
0
      g_ref_count_init (&real_box->ref_count);
152
0
    }
153
154
0
  TRACE (GLIB_RCBOX_ALLOC (allocated, block_size, atomic, clear));
155
156
0
  return allocated + private_size;
157
0
}
158
159
/**
160
 * g_rc_box_alloc:
161
 * @block_size: the size of the allocation, must be greater than 0
162
 *
163
 * Allocates @block_size bytes of memory, and adds reference
164
 * counting semantics to it.
165
 *
166
 * The data will be freed when its reference count drops to
167
 * zero.
168
 *
169
 * The allocated data is guaranteed to be suitably aligned for any
170
 * built-in type.
171
 *
172
 * Returns: (transfer full) (not nullable): a pointer to the allocated memory
173
 *
174
 * Since: 2.58
175
 */
176
gpointer
177
g_rc_box_alloc (gsize block_size)
178
0
{
179
0
  g_return_val_if_fail (block_size > 0, NULL);
180
181
0
  return g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, FALSE, FALSE);
182
0
}
183
184
/**
185
 * g_rc_box_alloc0:
186
 * @block_size: the size of the allocation, must be greater than 0
187
 *
188
 * Allocates @block_size bytes of memory, and adds reference
189
 * counting semantics to it.
190
 *
191
 * The contents of the returned data is set to zero.
192
 *
193
 * The data will be freed when its reference count drops to
194
 * zero.
195
 *
196
 * The allocated data is guaranteed to be suitably aligned for any
197
 * built-in type.
198
 *
199
 * Returns: (transfer full) (not nullable): a pointer to the allocated memory
200
 *
201
 * Since: 2.58
202
 */
203
gpointer
204
g_rc_box_alloc0 (gsize block_size)
205
0
{
206
0
  g_return_val_if_fail (block_size > 0, NULL);
207
208
0
  return g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, FALSE, TRUE);
209
0
}
210
211
/**
212
 * g_rc_box_new:
213
 * @type: the type to allocate, typically a structure name
214
 *
215
 * A convenience macro to allocate reference counted data with
216
 * the size of the given @type.
217
 *
218
 * This macro calls g_rc_box_alloc() with `sizeof (@type)` and
219
 * casts the returned pointer to a pointer of the given @type,
220
 * avoiding a type cast in the source code.
221
 *
222
 * Returns: (transfer full) (not nullable): a pointer to the
223
 *   allocated memory, cast to a pointer for the given @type
224
 *
225
 * Since: 2.58
226
 */
227
228
/**
229
 * g_rc_box_new0:
230
 * @type: the type to allocate, typically a structure name
231
 *
232
 * A convenience macro to allocate reference counted data with
233
 * the size of the given @type, and set its contents to zero.
234
 *
235
 * This macro calls g_rc_box_alloc0() with `sizeof (@type)` and
236
 * casts the returned pointer to a pointer of the given @type,
237
 * avoiding a type cast in the source code.
238
 *
239
 * Returns: (transfer full) (not nullable): a pointer to the
240
 *   allocated memory, cast to a pointer for the given @type
241
 *
242
 * Since: 2.58
243
 */
244
245
/**
246
 * g_rc_box_dup:
247
 * @block_size: the number of bytes to copy, must be greater than 0
248
 * @mem_block: (not nullable): the memory to copy
249
 *
250
 * Allocates a new block of data with reference counting
251
 * semantics, and copies @block_size bytes of @mem_block
252
 * into it.
253
 *
254
 * Returns: (transfer full) (not nullable): a pointer to the allocated
255
 *   memory
256
 *
257
 * Since: 2.58
258
 */
259
gpointer
260
(g_rc_box_dup) (gsize         block_size,
261
                gconstpointer mem_block)
262
0
{
263
0
  gpointer res;
264
265
0
  g_return_val_if_fail (block_size > 0, NULL);
266
0
  g_return_val_if_fail (mem_block != NULL, NULL);
267
268
0
  res = g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, FALSE, FALSE);
269
0
  memcpy (res, mem_block, block_size);
270
271
0
  return res;
272
0
}
273
274
/**
275
 * g_rc_box_acquire:
276
 * @mem_block: (not nullable): a pointer to reference counted data
277
 *
278
 * Acquires a reference on the data pointed by @mem_block.
279
 *
280
 * Returns: (transfer full) (not nullable): a pointer to the data,
281
 *   with its reference count increased
282
 *
283
 * Since: 2.58
284
 */
285
gpointer
286
(g_rc_box_acquire) (gpointer mem_block)
287
0
{
288
0
  GRcBox *real_box = G_RC_BOX (mem_block);
289
290
0
  g_return_val_if_fail (mem_block != NULL, NULL);
291
0
#ifndef G_DISABLE_ASSERT
292
0
  g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, NULL);
293
0
#endif
294
295
0
  g_ref_count_inc (&real_box->ref_count);
296
297
0
  TRACE (GLIB_RCBOX_ACQUIRE (mem_block, 0));
298
299
0
  return mem_block;
300
0
}
301
302
/**
303
 * g_rc_box_release:
304
 * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
305
 *
306
 * Releases a reference on the data pointed by @mem_block.
307
 *
308
 * If the reference was the last one, it will free the
309
 * resources allocated for @mem_block.
310
 *
311
 * Since: 2.58
312
 */
313
void
314
g_rc_box_release (gpointer mem_block)
315
0
{
316
0
  g_rc_box_release_full (mem_block, NULL);
317
0
}
318
319
/**
320
 * g_rc_box_release_full:
321
 * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
322
 * @clear_func: (not nullable): a function to call when clearing the data
323
 *
324
 * Releases a reference on the data pointed by @mem_block.
325
 *
326
 * If the reference was the last one, it will call @clear_func
327
 * to clear the contents of @mem_block, and then will free the
328
 * resources allocated for @mem_block.
329
 *
330
 * Since: 2.58
331
 */
332
void
333
g_rc_box_release_full (gpointer       mem_block,
334
                       GDestroyNotify clear_func)
335
0
{
336
0
  GRcBox *real_box = G_RC_BOX (mem_block);
337
338
0
  g_return_if_fail (mem_block != NULL);
339
0
#ifndef G_DISABLE_ASSERT
340
0
  g_return_if_fail (real_box->magic == G_BOX_MAGIC);
341
0
#endif
342
343
0
  if (g_ref_count_dec (&real_box->ref_count))
344
0
    {
345
0
      char *real_mem = (char *) real_box - real_box->private_offset;
346
347
0
      TRACE (GLIB_RCBOX_RELEASE (mem_block, 0));
348
349
0
      if (clear_func != NULL)
350
0
        clear_func (mem_block);
351
352
0
      TRACE (GLIB_RCBOX_FREE (mem_block));
353
0
      g_free (real_mem);
354
0
    }
355
0
}
356
357
/**
358
 * g_rc_box_get_size:
359
 * @mem_block: (not nullable): a pointer to reference counted data
360
 *
361
 * Retrieves the size of the reference counted data pointed by @mem_block.
362
 *
363
 * Returns: the size of the data, in bytes
364
 *
365
 * Since: 2.58
366
 */
367
gsize
368
g_rc_box_get_size (gpointer mem_block)
369
0
{
370
0
  GRcBox *real_box = G_RC_BOX (mem_block);
371
372
0
  g_return_val_if_fail (mem_block != NULL, 0);
373
0
#ifndef G_DISABLE_ASSERT
374
0
  g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, 0);
375
0
#endif
376
377
0
  return real_box->mem_size;
378
0
}