Coverage Report

Created: 2026-01-09 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/glib/gmem.c
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
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/*
19
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20
 * file for a list of people on the GLib Team.  See the ChangeLog
21
 * files for a list of changes.  These files are distributed with
22
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
23
 */
24
25
/* 
26
 * MT safe
27
 */
28
29
#include "config.h"
30
31
#include "gmem.h"
32
33
#include <stdlib.h>
34
#include <string.h>
35
#include <signal.h>
36
37
#include "gslice.h"
38
#include "gbacktrace.h"
39
#include "gtestutils.h"
40
#include "gthread.h"
41
#include "glib_trace.h"
42
43
/* notes on macros:
44
 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
45
 * g_mem_profile().
46
 * If g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
47
 */
48
49
/* --- variables --- */
50
static GMemVTable glib_mem_vtable = {
51
  malloc,
52
  realloc,
53
  free,
54
  calloc,
55
  malloc,
56
  realloc,
57
};
58
59
/**
60
 * SECTION:memory
61
 * @Short_Description: general memory-handling
62
 * @Title: Memory Allocation
63
 * 
64
 * These functions provide support for allocating and freeing memory.
65
 * 
66
 * If any call to allocate memory using functions g_new(), g_new0(), g_renew(),
67
 * g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n()
68
 * fails, the application is terminated. This also means that there is no
69
 * need to check if the call succeeded. On the other hand, the `g_try_...()` family
70
 * of functions returns %NULL on failure that can be used as a check
71
 * for unsuccessful memory allocation. The application is not terminated
72
 * in this case.
73
 *
74
 * As all GLib functions and data structures use `g_malloc()` internally, unless
75
 * otherwise specified, any allocation failure will result in the application
76
 * being terminated.
77
 *
78
 * It's important to match g_malloc() (and wrappers such as g_new()) with
79
 * g_free(), g_slice_alloc() (and wrappers such as g_slice_new()) with
80
 * g_slice_free(), plain malloc() with free(), and (if you're using C++)
81
 * new with delete and new[] with delete[]. Otherwise bad things can happen,
82
 * since these allocators may use different memory pools (and new/delete call
83
 * constructors and destructors).
84
 *
85
 * Since GLib 2.46 g_malloc() is hardcoded to always use the system malloc
86
 * implementation.
87
 */
88
89
/* --- functions --- */
90
/**
91
 * g_malloc:
92
 * @n_bytes: the number of bytes to allocate
93
 * 
94
 * Allocates @n_bytes bytes of memory.
95
 * If @n_bytes is 0 it returns %NULL.
96
 * 
97
 * Returns: a pointer to the allocated memory
98
 */
99
gpointer
100
g_malloc (gsize n_bytes)
101
149M
{
102
149M
  if (G_LIKELY (n_bytes))
103
149M
    {
104
149M
      gpointer mem;
105
106
149M
      mem = malloc (n_bytes);
107
149M
      TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 0));
108
149M
      if (mem)
109
149M
  return mem;
110
111
149M
      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
112
0
               G_STRLOC, n_bytes);
113
0
    }
114
115
0
  TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 0, 0));
116
117
0
  return NULL;
118
149M
}
119
120
/**
121
 * g_malloc0:
122
 * @n_bytes: the number of bytes to allocate
123
 * 
124
 * Allocates @n_bytes bytes of memory, initialized to 0's.
125
 * If @n_bytes is 0 it returns %NULL.
126
 * 
127
 * Returns: a pointer to the allocated memory
128
 */
129
gpointer
130
g_malloc0 (gsize n_bytes)
131
191M
{
132
191M
  if (G_LIKELY (n_bytes))
133
191M
    {
134
191M
      gpointer mem;
135
136
191M
      mem = calloc (1, n_bytes);
137
191M
      TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 1, 0));
138
191M
      if (mem)
139
191M
  return mem;
140
141
191M
      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
142
0
               G_STRLOC, n_bytes);
143
0
    }
144
145
0
  TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 1, 0));
146
147
0
  return NULL;
148
191M
}
149
150
/**
151
 * g_realloc:
152
 * @mem: (nullable): the memory to reallocate
153
 * @n_bytes: new size of the memory in bytes
154
 * 
155
 * Reallocates the memory pointed to by @mem, so that it now has space for
156
 * @n_bytes bytes of memory. It returns the new address of the memory, which may
157
 * have been moved. @mem may be %NULL, in which case it's considered to
158
 * have zero-length. @n_bytes may be 0, in which case %NULL will be returned
159
 * and @mem will be freed unless it is %NULL.
160
 * 
161
 * Returns: the new address of the allocated memory
162
 */
163
gpointer
164
g_realloc (gpointer mem,
165
     gsize    n_bytes)
166
271M
{
167
271M
  gpointer newmem;
168
169
271M
  if (G_LIKELY (n_bytes))
170
271M
    {
171
271M
      newmem = realloc (mem, n_bytes);
172
271M
      TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 0));
173
271M
      if (newmem)
174
271M
  return newmem;
175
176
271M
      g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
177
0
               G_STRLOC, n_bytes);
178
0
    }
179
180
0
  free (mem);
181
182
0
  TRACE (GLIB_MEM_REALLOC((void*) NULL, (void*)mem, 0, 0));
183
184
0
  return NULL;
185
271M
}
186
187
/**
188
 * g_free:
189
 * @mem: (nullable): the memory to free
190
 * 
191
 * Frees the memory pointed to by @mem.
192
 *
193
 * If @mem is %NULL it simply returns, so there is no need to check @mem
194
 * against %NULL before calling this function.
195
 */
196
void
197
g_free (gpointer mem)
198
646M
{
199
646M
  free (mem);
200
646M
  TRACE(GLIB_MEM_FREE((void*) mem));
201
646M
}
202
203
/**
204
 * g_clear_pointer: (skip)
205
 * @pp: (not nullable): a pointer to a variable, struct member etc. holding a
206
 *    pointer
207
 * @destroy: a function to which a gpointer can be passed, to destroy *@pp
208
 *
209
 * Clears a reference to a variable.
210
 *
211
 * @pp must not be %NULL.
212
 *
213
 * If the reference is %NULL then this function does nothing.
214
 * Otherwise, the variable is destroyed using @destroy and the
215
 * pointer is set to %NULL.
216
 *
217
 * A macro is also included that allows this function to be used without
218
 * pointer casts. This will mask any warnings about incompatible function types
219
 * or calling conventions, so you must ensure that your @destroy function is
220
 * compatible with being called as `GDestroyNotify` using the standard calling
221
 * convention for the platform that GLib was compiled for; otherwise the program
222
 * will experience undefined behaviour.
223
 *
224
 * Since: 2.34
225
 **/
226
#undef g_clear_pointer
227
void
228
g_clear_pointer (gpointer      *pp,
229
                 GDestroyNotify destroy)
230
0
{
231
0
  gpointer _p;
232
233
0
  _p = *pp;
234
0
  if (_p)
235
0
    {
236
0
      *pp = NULL;
237
0
      destroy (_p);
238
0
    }
239
0
}
240
241
/**
242
 * g_try_malloc:
243
 * @n_bytes: number of bytes to allocate.
244
 * 
245
 * Attempts to allocate @n_bytes, and returns %NULL on failure.
246
 * Contrast with g_malloc(), which aborts the program on failure.
247
 * 
248
 * Returns: the allocated memory, or %NULL.
249
 */
250
gpointer
251
g_try_malloc (gsize n_bytes)
252
12.3k
{
253
12.3k
  gpointer mem;
254
255
12.3k
  if (G_LIKELY (n_bytes))
256
12.3k
    mem = malloc (n_bytes);
257
0
  else
258
0
    mem = NULL;
259
260
12.3k
  TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1));
261
262
12.3k
  return mem;
263
12.3k
}
264
265
/**
266
 * g_try_malloc0:
267
 * @n_bytes: number of bytes to allocate
268
 * 
269
 * Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
270
 * failure. Contrast with g_malloc0(), which aborts the program on failure.
271
 * 
272
 * Since: 2.8
273
 * Returns: the allocated memory, or %NULL
274
 */
275
gpointer
276
g_try_malloc0 (gsize n_bytes)
277
0
{
278
0
  gpointer mem;
279
280
0
  if (G_LIKELY (n_bytes))
281
0
    mem = calloc (1, n_bytes);
282
0
  else
283
0
    mem = NULL;
284
285
0
  return mem;
286
0
}
287
288
/**
289
 * g_try_realloc:
290
 * @mem: (nullable): previously-allocated memory, or %NULL.
291
 * @n_bytes: number of bytes to allocate.
292
 * 
293
 * Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
294
 * on failure. Contrast with g_realloc(), which aborts the program
295
 * on failure.
296
 *
297
 * If @mem is %NULL, behaves the same as g_try_malloc().
298
 * 
299
 * Returns: the allocated memory, or %NULL.
300
 */
301
gpointer
302
g_try_realloc (gpointer mem,
303
         gsize    n_bytes)
304
0
{
305
0
  gpointer newmem;
306
307
0
  if (G_LIKELY (n_bytes))
308
0
    newmem = realloc (mem, n_bytes);
309
0
  else
310
0
    {
311
0
      newmem = NULL;
312
0
      free (mem);
313
0
    }
314
315
0
  TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 1));
316
317
0
  return newmem;
318
0
}
319
320
321
335k
#define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((b) > 0 && (a) > G_MAXSIZE / (b)))
322
323
/**
324
 * g_malloc_n:
325
 * @n_blocks: the number of blocks to allocate
326
 * @n_block_bytes: the size of each block in bytes
327
 * 
328
 * This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
329
 * but care is taken to detect possible overflow during multiplication.
330
 * 
331
 * Since: 2.24
332
 * Returns: a pointer to the allocated memory
333
 */
334
gpointer
335
g_malloc_n (gsize n_blocks,
336
      gsize n_block_bytes)
337
22.8k
{
338
22.8k
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
339
0
    {
340
0
      g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
341
0
               G_STRLOC, n_blocks, n_block_bytes);
342
0
    }
343
344
22.8k
  return g_malloc (n_blocks * n_block_bytes);
345
22.8k
}
346
347
/**
348
 * g_malloc0_n:
349
 * @n_blocks: the number of blocks to allocate
350
 * @n_block_bytes: the size of each block in bytes
351
 * 
352
 * This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
353
 * but care is taken to detect possible overflow during multiplication.
354
 * 
355
 * Since: 2.24
356
 * Returns: a pointer to the allocated memory
357
 */
358
gpointer
359
g_malloc0_n (gsize n_blocks,
360
       gsize n_block_bytes)
361
290k
{
362
290k
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
363
0
    {
364
0
      g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
365
0
               G_STRLOC, n_blocks, n_block_bytes);
366
0
    }
367
368
290k
  return g_malloc0 (n_blocks * n_block_bytes);
369
290k
}
370
371
/**
372
 * g_realloc_n:
373
 * @mem: (nullable): the memory to reallocate
374
 * @n_blocks: the number of blocks to allocate
375
 * @n_block_bytes: the size of each block in bytes
376
 * 
377
 * This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
378
 * but care is taken to detect possible overflow during multiplication.
379
 * 
380
 * Since: 2.24
381
 * Returns: the new address of the allocated memory
382
 */
383
gpointer
384
g_realloc_n (gpointer mem,
385
       gsize    n_blocks,
386
       gsize    n_block_bytes)
387
10.0k
{
388
10.0k
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
389
0
    {
390
0
      g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
391
0
               G_STRLOC, n_blocks, n_block_bytes);
392
0
    }
393
394
10.0k
  return g_realloc (mem, n_blocks * n_block_bytes);
395
10.0k
}
396
397
/**
398
 * g_try_malloc_n:
399
 * @n_blocks: the number of blocks to allocate
400
 * @n_block_bytes: the size of each block in bytes
401
 * 
402
 * This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
403
 * but care is taken to detect possible overflow during multiplication.
404
 * 
405
 * Since: 2.24
406
 * Returns: the allocated memory, or %NULL.
407
 */
408
gpointer
409
g_try_malloc_n (gsize n_blocks,
410
    gsize n_block_bytes)
411
12.3k
{
412
12.3k
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
413
0
    return NULL;
414
415
12.3k
  return g_try_malloc (n_blocks * n_block_bytes);
416
12.3k
}
417
418
/**
419
 * g_try_malloc0_n:
420
 * @n_blocks: the number of blocks to allocate
421
 * @n_block_bytes: the size of each block in bytes
422
 * 
423
 * This function is similar to g_try_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
424
 * but care is taken to detect possible overflow during multiplication.
425
 * 
426
 * Since: 2.24
427
 * Returns: the allocated memory, or %NULL
428
 */
429
gpointer
430
g_try_malloc0_n (gsize n_blocks,
431
     gsize n_block_bytes)
432
0
{
433
0
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
434
0
    return NULL;
435
436
0
  return g_try_malloc0 (n_blocks * n_block_bytes);
437
0
}
438
439
/**
440
 * g_try_realloc_n:
441
 * @mem: (nullable): previously-allocated memory, or %NULL.
442
 * @n_blocks: the number of blocks to allocate
443
 * @n_block_bytes: the size of each block in bytes
444
 * 
445
 * This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
446
 * but care is taken to detect possible overflow during multiplication.
447
 * 
448
 * Since: 2.24
449
 * Returns: the allocated memory, or %NULL.
450
 */
451
gpointer
452
g_try_realloc_n (gpointer mem,
453
     gsize    n_blocks,
454
     gsize    n_block_bytes)
455
0
{
456
0
  if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
457
0
    return NULL;
458
459
0
  return g_try_realloc (mem, n_blocks * n_block_bytes);
460
0
}
461
462
/**
463
 * g_mem_is_system_malloc:
464
 * 
465
 * Checks whether the allocator used by g_malloc() is the system's
466
 * malloc implementation. If it returns %TRUE memory allocated with
467
 * malloc() can be used interchangeably with memory allocated using g_malloc().
468
 * This function is useful for avoiding an extra copy of allocated memory returned
469
 * by a non-GLib-based API.
470
 *
471
 * Returns: if %TRUE, malloc() and g_malloc() can be mixed.
472
 *
473
 * Deprecated: 2.46: GLib always uses the system malloc, so this function always
474
 * returns %TRUE.
475
 **/
476
gboolean
477
g_mem_is_system_malloc (void)
478
0
{
479
0
  return TRUE;
480
0
}
481
482
/**
483
 * g_mem_set_vtable:
484
 * @vtable: table of memory allocation routines.
485
 * 
486
 * This function used to let you override the memory allocation function.
487
 * However, its use was incompatible with the use of global constructors
488
 * in GLib and GIO, because those use the GLib allocators before main is
489
 * reached. Therefore this function is now deprecated and is just a stub.
490
 *
491
 * Deprecated: 2.46: This function now does nothing. Use other memory
492
 * profiling tools instead
493
 */
494
void
495
g_mem_set_vtable (GMemVTable *vtable)
496
0
{
497
0
  g_warning (G_STRLOC ": custom memory allocation vtable not supported");
498
0
}
499
500
501
/**
502
 * glib_mem_profiler_table:
503
 *
504
 * Used to be a #GMemVTable containing profiling variants of the memory
505
 * allocation functions, but this variable shouldn't be modified anymore.
506
 *
507
 * Deprecated: 2.46: Use other memory profiling tools instead
508
 */
509
GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
510
511
/**
512
 * g_mem_profile:
513
 *
514
 * GLib used to support some tools for memory profiling, but this
515
 * no longer works. There are many other useful tools for memory
516
 * profiling these days which can be used instead.
517
 *
518
 * Deprecated: 2.46: Use other memory profiling tools instead
519
 */
520
void
521
g_mem_profile (void)
522
0
{
523
0
  g_warning (G_STRLOC ": memory profiling not supported");
524
0
}