Coverage Report

Created: 2025-12-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/subprojects/glib-2.74.3/glib/gmem.h
Line
Count
Source
1
/* GLIB - Library of useful routines for C programming
2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/*
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22
 * file for a list of people on the GLib Team.  See the ChangeLog
23
 * files for a list of changes.  These files are distributed with
24
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25
 */
26
27
#ifndef __G_MEM_H__
28
#define __G_MEM_H__
29
30
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31
#error "Only <glib.h> can be included directly."
32
#endif
33
34
#include <glib/gutils.h>
35
#include <glib/glib-typeof.h>
36
37
G_BEGIN_DECLS
38
39
/**
40
 * GMemVTable:
41
 * @malloc: function to use for allocating memory.
42
 * @realloc: function to use for reallocating memory.
43
 * @free: function to use to free memory.
44
 * @calloc: function to use for allocating zero-filled memory.
45
 * @try_malloc: function to use for allocating memory without a default error handler.
46
 * @try_realloc: function to use for reallocating memory without a default error handler.
47
 * 
48
 * A set of functions used to perform memory allocation. The same #GMemVTable must
49
 * be used for all allocations in the same program; a call to g_mem_set_vtable(),
50
 * if it exists, should be prior to any use of GLib.
51
 *
52
 * This functions related to this has been deprecated in 2.46, and no longer work.
53
 */
54
typedef struct _GMemVTable GMemVTable;
55
56
57
#if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
58
/**
59
 * G_MEM_ALIGN:
60
 *
61
 * Indicates the number of bytes to which memory will be aligned on the
62
 * current platform.
63
 */
64
#  define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
65
#else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
66
#  define G_MEM_ALIGN GLIB_SIZEOF_LONG
67
#endif  /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
68
69
70
/* Memory allocation functions
71
 */
72
73
GLIB_AVAILABLE_IN_ALL
74
void   g_free           (gpointer  mem);
75
76
GLIB_AVAILABLE_IN_2_34
77
void     g_clear_pointer  (gpointer      *pp,
78
                           GDestroyNotify destroy);
79
80
GLIB_AVAILABLE_IN_ALL
81
gpointer g_malloc         (gsize   n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
82
GLIB_AVAILABLE_IN_ALL
83
gpointer g_malloc0        (gsize   n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
84
GLIB_AVAILABLE_IN_ALL
85
gpointer g_realloc        (gpointer  mem,
86
         gsize   n_bytes) G_GNUC_WARN_UNUSED_RESULT;
87
GLIB_AVAILABLE_IN_ALL
88
gpointer g_try_malloc     (gsize   n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
89
GLIB_AVAILABLE_IN_ALL
90
gpointer g_try_malloc0    (gsize   n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
91
GLIB_AVAILABLE_IN_ALL
92
gpointer g_try_realloc    (gpointer  mem,
93
         gsize   n_bytes) G_GNUC_WARN_UNUSED_RESULT;
94
95
GLIB_AVAILABLE_IN_ALL
96
gpointer g_malloc_n       (gsize   n_blocks,
97
         gsize   n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
98
GLIB_AVAILABLE_IN_ALL
99
gpointer g_malloc0_n      (gsize   n_blocks,
100
         gsize   n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
101
GLIB_AVAILABLE_IN_ALL
102
gpointer g_realloc_n      (gpointer  mem,
103
         gsize   n_blocks,
104
         gsize   n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
105
GLIB_AVAILABLE_IN_ALL
106
gpointer g_try_malloc_n   (gsize   n_blocks,
107
         gsize   n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
108
GLIB_AVAILABLE_IN_ALL
109
gpointer g_try_malloc0_n  (gsize   n_blocks,
110
         gsize   n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
111
GLIB_AVAILABLE_IN_ALL
112
gpointer g_try_realloc_n  (gpointer  mem,
113
         gsize   n_blocks,
114
         gsize   n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
115
116
GLIB_AVAILABLE_IN_2_72
117
gpointer g_aligned_alloc  (gsize         n_blocks,
118
                           gsize         n_block_bytes,
119
                           gsize         alignment) G_GNUC_WARN_UNUSED_RESULT G_GNUC_ALLOC_SIZE2(1,2);
120
GLIB_AVAILABLE_IN_2_72
121
gpointer g_aligned_alloc0 (gsize         n_blocks,
122
                           gsize         n_block_bytes,
123
                           gsize         alignment) G_GNUC_WARN_UNUSED_RESULT G_GNUC_ALLOC_SIZE2(1,2);
124
GLIB_AVAILABLE_IN_2_72
125
void     g_aligned_free   (gpointer      mem);
126
127
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
128
#define g_clear_pointer(pp, destroy)                     \
129
515
  G_STMT_START                                           \
130
515
  {                                                      \
131
515
    G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
132
515
    glib_typeof ((pp)) _pp = (pp);                       \
133
515
    glib_typeof (*(pp)) _ptr = *_pp;                     \
134
515
    *_pp = NULL;                                         \
135
515
    if (_ptr)                                            \
136
515
      (destroy) (_ptr);                                  \
137
515
  }                                                      \
138
515
  G_STMT_END                                             \
139
521
  GLIB_AVAILABLE_MACRO_IN_2_34
140
#else /* __GNUC__ */
141
#define g_clear_pointer(pp, destroy) \
142
  G_STMT_START {                                                               \
143
    G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));                       \
144
    /* Only one access, please; work around type aliasing */                   \
145
    union { char *in; gpointer *out; } _pp;                                    \
146
    gpointer _p;                                                               \
147
    /* This assignment is needed to avoid a gcc warning */                     \
148
    GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
149
                                                                               \
150
    _pp.in = (char *) (pp);                                                    \
151
    _p = *_pp.out;                                                             \
152
    if (_p)                        \
153
      {                        \
154
        *_pp.out = NULL;                                                       \
155
        _destroy (_p);                                                         \
156
      }                                                                        \
157
  } G_STMT_END                                                                 \
158
  GLIB_AVAILABLE_MACRO_IN_2_34
159
#endif /* __GNUC__ */
160
161
/**
162
 * g_steal_pointer:
163
 * @pp: (not nullable): a pointer to a pointer
164
 *
165
 * Sets @pp to %NULL, returning the value that was there before.
166
 *
167
 * Conceptually, this transfers the ownership of the pointer from the
168
 * referenced variable to the "caller" of the macro (ie: "steals" the
169
 * reference).
170
 *
171
 * The return value will be properly typed, according to the type of
172
 * @pp.
173
 *
174
 * This can be very useful when combined with g_autoptr() to prevent the
175
 * return value of a function from being automatically freed.  Consider
176
 * the following example (which only works on GCC and clang):
177
 *
178
 * |[
179
 * GObject *
180
 * create_object (void)
181
 * {
182
 *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
183
 *
184
 *   if (early_error_case)
185
 *     return NULL;
186
 *
187
 *   return g_steal_pointer (&obj);
188
 * }
189
 * ]|
190
 *
191
 * It can also be used in similar ways for 'out' parameters and is
192
 * particularly useful for dealing with optional out parameters:
193
 *
194
 * |[
195
 * gboolean
196
 * get_object (GObject **obj_out)
197
 * {
198
 *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
199
 *
200
 *   if (early_error_case)
201
 *     return FALSE;
202
 *
203
 *   if (obj_out)
204
 *     *obj_out = g_steal_pointer (&obj);
205
 *
206
 *   return TRUE;
207
 * }
208
 * ]|
209
 *
210
 * In the above example, the object will be automatically freed in the
211
 * early error case and also in the case that %NULL was given for
212
 * @obj_out.
213
 *
214
 * Since: 2.44
215
 */
216
GLIB_AVAILABLE_STATIC_INLINE_IN_2_44
217
static inline gpointer
218
g_steal_pointer (gpointer pp)
219
1.59k
{
220
1.59k
  gpointer *ptr = (gpointer *) pp;
221
1.59k
  gpointer ref;
222
223
1.59k
  ref = *ptr;
224
1.59k
  *ptr = NULL;
225
226
1.59k
  return ref;
227
1.59k
}
Unexecuted instantiation: gcharset.c:g_steal_pointer
Unexecuted instantiation: gconvert.c:g_steal_pointer
Unexecuted instantiation: gdatetime.c:g_steal_pointer
Unexecuted instantiation: genviron.c:g_steal_pointer
Unexecuted instantiation: gerror.c:g_steal_pointer
Unexecuted instantiation: gfileutils.c:g_steal_pointer
Unexecuted instantiation: ggettext.c:g_steal_pointer
ghash.c:g_steal_pointer
Line
Count
Source
219
1.56k
{
220
1.56k
  gpointer *ptr = (gpointer *) pp;
221
1.56k
  gpointer ref;
222
223
1.56k
  ref = *ptr;
224
1.56k
  *ptr = NULL;
225
226
1.56k
  return ref;
227
1.56k
}
Unexecuted instantiation: giochannel.c:g_steal_pointer
Unexecuted instantiation: glist.c:g_steal_pointer
Unexecuted instantiation: gmain.c:g_steal_pointer
Unexecuted instantiation: gmem.c:g_steal_pointer
Unexecuted instantiation: gmessages.c:g_steal_pointer
Unexecuted instantiation: goption.c:g_steal_pointer
Unexecuted instantiation: gpattern.c:g_steal_pointer
Unexecuted instantiation: gpoll.c:g_steal_pointer
Unexecuted instantiation: gquark.c:g_steal_pointer
Unexecuted instantiation: gqueue.c:g_steal_pointer
Unexecuted instantiation: grefstring.c:g_steal_pointer
Unexecuted instantiation: gregex.c:g_steal_pointer
Unexecuted instantiation: gsequence.c:g_steal_pointer
Unexecuted instantiation: gslice.c:g_steal_pointer
Unexecuted instantiation: gslist.c:g_steal_pointer
Unexecuted instantiation: gstdio.c:g_steal_pointer
Unexecuted instantiation: gstrfuncs.c:g_steal_pointer
Unexecuted instantiation: gstring.c:g_steal_pointer
Unexecuted instantiation: gtestutils.c:g_steal_pointer
gthread.c:g_steal_pointer
Line
Count
Source
219
16
{
220
16
  gpointer *ptr = (gpointer *) pp;
221
16
  gpointer ref;
222
223
16
  ref = *ptr;
224
16
  *ptr = NULL;
225
226
16
  return ref;
227
16
}
Unexecuted instantiation: gtimer.c:g_steal_pointer
Unexecuted instantiation: gtimezone.c:g_steal_pointer
Unexecuted instantiation: gtranslit.c:g_steal_pointer
Unexecuted instantiation: gtree.c:g_steal_pointer
Unexecuted instantiation: guniprop.c:g_steal_pointer
Unexecuted instantiation: gutf8.c:g_steal_pointer
Unexecuted instantiation: gunidecomp.c:g_steal_pointer
Unexecuted instantiation: guri.c:g_steal_pointer
gutils.c:g_steal_pointer
Line
Count
Source
219
6
{
220
6
  gpointer *ptr = (gpointer *) pp;
221
6
  gpointer ref;
222
223
6
  ref = *ptr;
224
6
  *ptr = NULL;
225
226
6
  return ref;
227
6
}
Unexecuted instantiation: gvariant.c:g_steal_pointer
Unexecuted instantiation: gvariant-core.c:g_steal_pointer
Unexecuted instantiation: gvariant-serialiser.c:g_steal_pointer
Unexecuted instantiation: gvarianttypeinfo.c:g_steal_pointer
Unexecuted instantiation: gvarianttype.c:g_steal_pointer
Unexecuted instantiation: gwakeup.c:g_steal_pointer
Unexecuted instantiation: gprintf.c:g_steal_pointer
Unexecuted instantiation: glib-unix.c:g_steal_pointer
Unexecuted instantiation: gspawn.c:g_steal_pointer
Unexecuted instantiation: giounix.c:g_steal_pointer
Unexecuted instantiation: gthread-posix.c:g_steal_pointer
Unexecuted instantiation: garcbox.c:g_steal_pointer
Unexecuted instantiation: garray.c:g_steal_pointer
Unexecuted instantiation: gbitlock.c:g_steal_pointer
Unexecuted instantiation: gbytes.c:g_steal_pointer
Unexecuted instantiation: gdate.c:g_steal_pointer
Unexecuted instantiation: gdir.c:g_steal_pointer
Unexecuted instantiation: ghostutils.c:g_steal_pointer
Unexecuted instantiation: glib-init.c:g_steal_pointer
Unexecuted instantiation: gmappedfile.c:g_steal_pointer
Unexecuted instantiation: gqsort.c:g_steal_pointer
Unexecuted instantiation: grand.c:g_steal_pointer
Unexecuted instantiation: grcbox.c:g_steal_pointer
Unexecuted instantiation: gshell.c:g_steal_pointer
Unexecuted instantiation: gscanner.c:g_steal_pointer
Unexecuted instantiation: gdataset.c:g_steal_pointer
Unexecuted instantiation: gmodule.c:g_steal_pointer
Unexecuted instantiation: gbase64.c:g_steal_pointer
228
229
/* type safety */
230
#if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
231
1.59k
#define g_steal_pointer(pp) ((glib_typeof (*pp)) (g_steal_pointer) (pp))
232
#else  /* __GNUC__ */
233
/* This version does not depend on gcc extensions, but gcc does not warn
234
 * about incompatible-pointer-types: */
235
#define g_steal_pointer(pp) \
236
  (0 ? (*(pp)) : (g_steal_pointer) (pp))
237
#endif /* __GNUC__ */
238
239
/* Optimise: avoid the call to the (slower) _n function if we can
240
 * determine at compile-time that no overflow happens.
241
 */
242
#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
243
#  define _G_NEW(struct_type, n_structs, func) \
244
59.1M
  (struct_type *) (G_GNUC_EXTENSION ({     \
245
59.1M
    gsize __n = (gsize) (n_structs);      \
246
59.1M
    gsize __s = sizeof (struct_type);     \
247
59.1M
    gpointer __p;           \
248
59.1M
    if (__s == 1)           \
249
59.1M
      __p = g_##func (__n);       \
250
59.1M
    else if (__builtin_constant_p (__n) &&   \
251
132k
             (__s == 0 || __n <= G_MAXSIZE / __s)) \
252
132k
      __p = g_##func (__n * __s);       \
253
132k
    else              \
254
132k
      __p = g_##func##_n (__n, __s);     \
255
59.1M
    __p;              \
256
59.1M
  }))
257
#  define _G_RENEW(struct_type, mem, n_structs, func) \
258
21.8k
  (struct_type *) (G_GNUC_EXTENSION ({     \
259
21.8k
    gsize __n = (gsize) (n_structs);      \
260
21.8k
    gsize __s = sizeof (struct_type);     \
261
21.8k
    gpointer __p = (gpointer) (mem);      \
262
21.8k
    if (__s == 1)           \
263
21.8k
      __p = g_##func (__p, __n);       \
264
21.8k
    else if (__builtin_constant_p (__n) &&   \
265
21.8k
             (__s == 0 || __n <= G_MAXSIZE / __s)) \
266
21.8k
      __p = g_##func (__p, __n * __s);     \
267
21.8k
    else              \
268
21.8k
      __p = g_##func##_n (__p, __n, __s);     \
269
21.8k
    __p;              \
270
21.8k
  }))
271
272
#else
273
274
/* Unoptimised version: always call the _n() function. */
275
276
#define _G_NEW(struct_type, n_structs, func) \
277
        ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
278
#define _G_RENEW(struct_type, mem, n_structs, func) \
279
        ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
280
281
#endif
282
283
/**
284
 * g_new:
285
 * @struct_type: the type of the elements to allocate
286
 * @n_structs: the number of elements to allocate
287
 * 
288
 * Allocates @n_structs elements of type @struct_type.
289
 * The returned pointer is cast to a pointer to the given type.
290
 * If @n_structs is 0 it returns %NULL.
291
 * Care is taken to avoid overflow when calculating the size of the allocated block.
292
 * 
293
 * Since the returned pointer is already casted to the right type,
294
 * it is normally unnecessary to cast it explicitly, and doing
295
 * so might hide memory allocation errors.
296
 * 
297
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
298
 */
299
58.8M
#define g_new(struct_type, n_structs)     _G_NEW (struct_type, n_structs, malloc)
300
/**
301
 * g_new0:
302
 * @struct_type: the type of the elements to allocate.
303
 * @n_structs: the number of elements to allocate.
304
 * 
305
 * Allocates @n_structs elements of type @struct_type, initialized to 0's.
306
 * The returned pointer is cast to a pointer to the given type.
307
 * If @n_structs is 0 it returns %NULL.
308
 * Care is taken to avoid overflow when calculating the size of the allocated block.
309
 * 
310
 * Since the returned pointer is already casted to the right type,
311
 * it is normally unnecessary to cast it explicitly, and doing
312
 * so might hide memory allocation errors.
313
 * 
314
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
315
 */
316
298k
#define g_new0(struct_type, n_structs)      _G_NEW (struct_type, n_structs, malloc0)
317
/**
318
 * g_renew:
319
 * @struct_type: the type of the elements to allocate
320
 * @mem: the currently allocated memory
321
 * @n_structs: the number of elements to allocate
322
 * 
323
 * Reallocates the memory pointed to by @mem, so that it now has space for
324
 * @n_structs elements of type @struct_type. It returns the new address of
325
 * the memory, which may have been moved.
326
 * Care is taken to avoid overflow when calculating the size of the allocated block.
327
 * 
328
 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
329
 */
330
21.8k
#define g_renew(struct_type, mem, n_structs)    _G_RENEW (struct_type, mem, n_structs, realloc)
331
/**
332
 * g_try_new:
333
 * @struct_type: the type of the elements to allocate
334
 * @n_structs: the number of elements to allocate
335
 * 
336
 * Attempts to allocate @n_structs elements of type @struct_type, and returns
337
 * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
338
 * The returned pointer is cast to a pointer to the given type.
339
 * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
340
 * 
341
 * Since: 2.8
342
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
343
 */
344
#define g_try_new(struct_type, n_structs)   _G_NEW (struct_type, n_structs, try_malloc)
345
/**
346
 * g_try_new0:
347
 * @struct_type: the type of the elements to allocate
348
 * @n_structs: the number of elements to allocate
349
 * 
350
 * Attempts to allocate @n_structs elements of type @struct_type, initialized
351
 * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
352
 * the program on failure.
353
 * The returned pointer is cast to a pointer to the given type.
354
 * The function returns %NULL when @n_structs is 0 or if an overflow occurs.
355
 * 
356
 * Since: 2.8
357
 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
358
 */
359
#define g_try_new0(struct_type, n_structs)    _G_NEW (struct_type, n_structs, try_malloc0)
360
/**
361
 * g_try_renew:
362
 * @struct_type: the type of the elements to allocate
363
 * @mem: the currently allocated memory
364
 * @n_structs: the number of elements to allocate
365
 * 
366
 * Attempts to reallocate the memory pointed to by @mem, so that it now has
367
 * space for @n_structs elements of type @struct_type, and returns %NULL on
368
 * failure. Contrast with g_renew(), which aborts the program on failure.
369
 * It returns the new address of the memory, which may have been moved.
370
 * The function returns %NULL if an overflow occurs.
371
 * 
372
 * Since: 2.8
373
 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
374
 */
375
#define g_try_renew(struct_type, mem, n_structs)  _G_RENEW (struct_type, mem, n_structs, try_realloc)
376
377
378
/* Memory allocation virtualization for debugging purposes
379
 * g_mem_set_vtable() has to be the very first GLib function called
380
 * if being used
381
 */
382
struct _GMemVTable {
383
  gpointer (*malloc)      (gsize    n_bytes);
384
  gpointer (*realloc)     (gpointer mem,
385
         gsize    n_bytes);
386
  void     (*free)        (gpointer mem);
387
  /* optional; set to NULL if not used ! */
388
  gpointer (*calloc)      (gsize    n_blocks,
389
         gsize    n_block_bytes);
390
  gpointer (*try_malloc)  (gsize    n_bytes);
391
  gpointer (*try_realloc) (gpointer mem,
392
         gsize    n_bytes);
393
};
394
GLIB_DEPRECATED_IN_2_46
395
void   g_mem_set_vtable (GMemVTable *vtable);
396
GLIB_DEPRECATED_IN_2_46
397
gboolean g_mem_is_system_malloc (void);
398
399
GLIB_VAR gboolean g_mem_gc_friendly;
400
401
/* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
402
 */
403
GLIB_VAR GMemVTable *glib_mem_profiler_table;
404
GLIB_DEPRECATED_IN_2_46
405
void  g_mem_profile (void);
406
407
G_END_DECLS
408
409
#endif /* __G_MEM_H__ */