1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
38 #include "gbacktrace.h"
39 #include "gtestutils.h"
41 #include "glib_trace.h"
44 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
46 * If g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
49 /* --- variables --- */
50 static GMemVTable glib_mem_vtable
= {
61 * @Short_Description: general memory-handling
62 * @Title: Memory Allocation
64 * These functions provide support for allocating and freeing memory.
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, 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
74 * It's important to match g_malloc() (and wrappers such as g_new()) with
75 * g_free(), g_slice_alloc() (and wrappers such as g_slice_new()) with
76 * g_slice_free(), plain malloc() with free(), and (if you're using C++)
77 * new with delete and new[] with delete[]. Otherwise bad things can happen,
78 * since these allocators may use different memory pools (and new/delete call
79 * constructors and destructors).
82 /* --- functions --- */
85 * @n_bytes: the number of bytes to allocate
87 * Allocates @n_bytes bytes of memory.
88 * If @n_bytes is 0 it returns %NULL.
90 * Returns: a pointer to the allocated memory
93 g_malloc (gsize n_bytes
)
95 if (G_LIKELY (n_bytes
))
99 mem
= malloc (n_bytes
);
100 TRACE (GLIB_MEM_ALLOC((void*) mem
, (unsigned int) n_bytes
, 0, 0));
104 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
108 TRACE(GLIB_MEM_ALLOC((void*) NULL
, (int) n_bytes
, 0, 0));
115 * @n_bytes: the number of bytes to allocate
117 * Allocates @n_bytes bytes of memory, initialized to 0's.
118 * If @n_bytes is 0 it returns %NULL.
120 * Returns: a pointer to the allocated memory
123 g_malloc0 (gsize n_bytes
)
125 if (G_LIKELY (n_bytes
))
129 mem
= calloc (1, n_bytes
);
130 TRACE (GLIB_MEM_ALLOC((void*) mem
, (unsigned int) n_bytes
, 1, 0));
134 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
138 TRACE(GLIB_MEM_ALLOC((void*) NULL
, (int) n_bytes
, 1, 0));
145 * @mem: (nullable): the memory to reallocate
146 * @n_bytes: new size of the memory in bytes
148 * Reallocates the memory pointed to by @mem, so that it now has space for
149 * @n_bytes bytes of memory. It returns the new address of the memory, which may
150 * have been moved. @mem may be %NULL, in which case it's considered to
151 * have zero-length. @n_bytes may be 0, in which case %NULL will be returned
152 * and @mem will be freed unless it is %NULL.
154 * Returns: the new address of the allocated memory
157 g_realloc (gpointer mem
,
162 if (G_LIKELY (n_bytes
))
164 newmem
= realloc (mem
, n_bytes
);
165 TRACE (GLIB_MEM_REALLOC((void*) newmem
, (void*)mem
, (unsigned int) n_bytes
, 0));
169 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
176 TRACE (GLIB_MEM_REALLOC((void*) NULL
, (void*)mem
, 0, 0));
183 * @mem: (nullable): the memory to free
185 * Frees the memory pointed to by @mem.
187 * If @mem is %NULL it simply returns, so there is no need to check @mem
188 * against %NULL before calling this function.
191 g_free (gpointer mem
)
195 TRACE(GLIB_MEM_FREE((void*) mem
));
199 * g_clear_pointer: (skip)
200 * @pp: (not nullable): a pointer to a variable, struct member etc. holding a
202 * @destroy: a function to which a gpointer can be passed, to destroy *@pp
204 * Clears a reference to a variable.
206 * @pp must not be %NULL.
208 * If the reference is %NULL then this function does nothing.
209 * Otherwise, the variable is destroyed using @destroy and the
210 * pointer is set to %NULL.
212 * A macro is also included that allows this function to be used without
217 #undef g_clear_pointer
219 g_clear_pointer (gpointer
*pp
,
220 GDestroyNotify destroy
)
234 * @n_bytes: number of bytes to allocate.
236 * Attempts to allocate @n_bytes, and returns %NULL on failure.
237 * Contrast with g_malloc(), which aborts the program on failure.
239 * Returns: the allocated memory, or %NULL.
242 g_try_malloc (gsize n_bytes
)
246 if (G_LIKELY (n_bytes
))
247 mem
= malloc (n_bytes
);
251 TRACE (GLIB_MEM_ALLOC((void*) mem
, (unsigned int) n_bytes
, 0, 1));
258 * @n_bytes: number of bytes to allocate
260 * Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
261 * failure. Contrast with g_malloc0(), which aborts the program on failure.
264 * Returns: the allocated memory, or %NULL
267 g_try_malloc0 (gsize n_bytes
)
271 if (G_LIKELY (n_bytes
))
272 mem
= calloc (1, n_bytes
);
281 * @mem: (nullable): previously-allocated memory, or %NULL.
282 * @n_bytes: number of bytes to allocate.
284 * Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
285 * on failure. Contrast with g_realloc(), which aborts the program
288 * If @mem is %NULL, behaves the same as g_try_malloc().
290 * Returns: the allocated memory, or %NULL.
293 g_try_realloc (gpointer mem
,
298 if (G_LIKELY (n_bytes
))
299 newmem
= realloc (mem
, n_bytes
);
307 TRACE (GLIB_MEM_REALLOC((void*) newmem
, (void*)mem
, (unsigned int) n_bytes
, 1));
313 #define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((b) > 0 && (a) > G_MAXSIZE / (b)))
317 * @n_blocks: the number of blocks to allocate
318 * @n_block_bytes: the size of each block in bytes
320 * This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
321 * but care is taken to detect possible overflow during multiplication.
324 * Returns: a pointer to the allocated memory
327 g_malloc_n (gsize n_blocks
,
330 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
332 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT
"*%"G_GSIZE_FORMAT
" bytes",
333 G_STRLOC
, n_blocks
, n_block_bytes
);
336 return g_malloc (n_blocks
* n_block_bytes
);
341 * @n_blocks: the number of blocks to allocate
342 * @n_block_bytes: the size of each block in bytes
344 * This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
345 * but care is taken to detect possible overflow during multiplication.
348 * Returns: a pointer to the allocated memory
351 g_malloc0_n (gsize n_blocks
,
354 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
356 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT
"*%"G_GSIZE_FORMAT
" bytes",
357 G_STRLOC
, n_blocks
, n_block_bytes
);
360 return g_malloc0 (n_blocks
* n_block_bytes
);
365 * @mem: (nullable): the memory to reallocate
366 * @n_blocks: the number of blocks to allocate
367 * @n_block_bytes: the size of each block in bytes
369 * This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
370 * but care is taken to detect possible overflow during multiplication.
373 * Returns: the new address of the allocated memory
376 g_realloc_n (gpointer mem
,
380 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
382 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT
"*%"G_GSIZE_FORMAT
" bytes",
383 G_STRLOC
, n_blocks
, n_block_bytes
);
386 return g_realloc (mem
, n_blocks
* n_block_bytes
);
391 * @n_blocks: the number of blocks to allocate
392 * @n_block_bytes: the size of each block in bytes
394 * This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
395 * but care is taken to detect possible overflow during multiplication.
398 * Returns: the allocated memory, or %NULL.
401 g_try_malloc_n (gsize n_blocks
,
404 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
407 return g_try_malloc (n_blocks
* n_block_bytes
);
412 * @n_blocks: the number of blocks to allocate
413 * @n_block_bytes: the size of each block in bytes
415 * This function is similar to g_try_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
416 * but care is taken to detect possible overflow during multiplication.
419 * Returns: the allocated memory, or %NULL
422 g_try_malloc0_n (gsize n_blocks
,
425 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
428 return g_try_malloc0 (n_blocks
* n_block_bytes
);
433 * @mem: (nullable): previously-allocated memory, or %NULL.
434 * @n_blocks: the number of blocks to allocate
435 * @n_block_bytes: the size of each block in bytes
437 * This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
438 * but care is taken to detect possible overflow during multiplication.
441 * Returns: the allocated memory, or %NULL.
444 g_try_realloc_n (gpointer mem
,
448 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
451 return g_try_realloc (mem
, n_blocks
* n_block_bytes
);
455 * g_mem_is_system_malloc:
457 * Checks whether the allocator used by g_malloc() is the system's
458 * malloc implementation. If it returns %TRUE memory allocated with
459 * malloc() can be used interchangeable with memory allocated using g_malloc().
460 * This function is useful for avoiding an extra copy of allocated memory returned
461 * by a non-GLib-based API.
463 * Returns: if %TRUE, malloc() and g_malloc() can be mixed.
465 * Deprecated: 2.46: GLib always uses the system malloc, so this function always
469 g_mem_is_system_malloc (void)
476 * @vtable: table of memory allocation routines.
478 * This function used to let you override the memory allocation function.
479 * However, its use was incompatible with the use of global constructors
480 * in GLib and GIO, because those use the GLib allocators before main is
481 * reached. Therefore this function is now deprecated and is just a stub.
483 * Deprecated: 2.46: This function now does nothing. Use other memory
484 * profiling tools instead
487 g_mem_set_vtable (GMemVTable
*vtable
)
489 g_warning (G_STRLOC
": custom memory allocation vtable not supported");
494 * glib_mem_profiler_table:
496 * Used to be a #GMemVTable containing profiling variants of the memory
497 * allocation functions, but this variable shouldn't be modified anymore.
499 * Deprecated: 2.46: Use other memory profiling tools instead
501 GMemVTable
*glib_mem_profiler_table
= &glib_mem_vtable
;
506 * GLib used to support some tools for memory profiling, but this
507 * no longer works. There are many other useful tools for memory
508 * profiling these days which can be used instead.
510 * Deprecated: 2.46: Use other memory profiling tools instead
515 g_warning (G_STRLOC
": memory profiling not supported");