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 fails, the application is terminated.
67 * This also means that there is no need to check if the call succeeded.
69 * It's important to match g_malloc() (and wrappers such as g_new()) with
70 * g_free(), g_slice_alloc() (and wrappers such as g_slice_new()) with
71 * g_slice_free(), plain malloc() with free(), and (if you're using C++)
72 * new with delete and new[] with delete[]. Otherwise bad things can happen,
73 * since these allocators may use different memory pools (and new/delete call
74 * constructors and destructors).
77 /* --- functions --- */
80 * @n_bytes: the number of bytes to allocate
82 * Allocates @n_bytes bytes of memory.
83 * If @n_bytes is 0 it returns %NULL.
85 * Returns: a pointer to the allocated memory
88 g_malloc (gsize n_bytes
)
90 if (G_LIKELY (n_bytes
))
94 mem
= malloc (n_bytes
);
95 TRACE (GLIB_MEM_ALLOC((void*) mem
, (unsigned int) n_bytes
, 0, 0));
99 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
103 TRACE(GLIB_MEM_ALLOC((void*) NULL
, (int) n_bytes
, 0, 0));
110 * @n_bytes: the number of bytes to allocate
112 * Allocates @n_bytes bytes of memory, initialized to 0's.
113 * If @n_bytes is 0 it returns %NULL.
115 * Returns: a pointer to the allocated memory
118 g_malloc0 (gsize n_bytes
)
120 if (G_LIKELY (n_bytes
))
124 mem
= calloc (1, n_bytes
);
125 TRACE (GLIB_MEM_ALLOC((void*) mem
, (unsigned int) n_bytes
, 1, 0));
129 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
133 TRACE(GLIB_MEM_ALLOC((void*) NULL
, (int) n_bytes
, 1, 0));
140 * @mem: (nullable): the memory to reallocate
141 * @n_bytes: new size of the memory in bytes
143 * Reallocates the memory pointed to by @mem, so that it now has space for
144 * @n_bytes bytes of memory. It returns the new address of the memory, which may
145 * have been moved. @mem may be %NULL, in which case it's considered to
146 * have zero-length. @n_bytes may be 0, in which case %NULL will be returned
147 * and @mem will be freed unless it is %NULL.
149 * Returns: the new address of the allocated memory
152 g_realloc (gpointer mem
,
157 if (G_LIKELY (n_bytes
))
159 newmem
= realloc (mem
, n_bytes
);
160 TRACE (GLIB_MEM_REALLOC((void*) newmem
, (void*)mem
, (unsigned int) n_bytes
, 0));
164 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
171 TRACE (GLIB_MEM_REALLOC((void*) NULL
, (void*)mem
, 0, 0));
178 * @mem: (nullable): the memory to free
180 * Frees the memory pointed to by @mem.
182 * If @mem is %NULL it simply returns, so there is no need to check @mem
183 * against %NULL before calling this function.
186 g_free (gpointer mem
)
190 TRACE(GLIB_MEM_FREE((void*) mem
));
194 * g_clear_pointer: (skip)
195 * @pp: (not nullable): a pointer to a variable, struct member etc. holding a
197 * @destroy: a function to which a gpointer can be passed, to destroy *@pp
199 * Clears a reference to a variable.
201 * @pp must not be %NULL.
203 * If the reference is %NULL then this function does nothing.
204 * Otherwise, the variable is destroyed using @destroy and the
205 * pointer is set to %NULL.
207 * A macro is also included that allows this function to be used without
212 #undef g_clear_pointer
214 g_clear_pointer (gpointer
*pp
,
215 GDestroyNotify destroy
)
229 * @n_bytes: number of bytes to allocate.
231 * Attempts to allocate @n_bytes, and returns %NULL on failure.
232 * Contrast with g_malloc(), which aborts the program on failure.
234 * Returns: the allocated memory, or %NULL.
237 g_try_malloc (gsize n_bytes
)
241 if (G_LIKELY (n_bytes
))
242 mem
= malloc (n_bytes
);
246 TRACE (GLIB_MEM_ALLOC((void*) mem
, (unsigned int) n_bytes
, 0, 1));
253 * @n_bytes: number of bytes to allocate
255 * Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
256 * failure. Contrast with g_malloc0(), which aborts the program on failure.
259 * Returns: the allocated memory, or %NULL
262 g_try_malloc0 (gsize n_bytes
)
266 if (G_LIKELY (n_bytes
))
267 mem
= calloc (1, n_bytes
);
276 * @mem: (nullable): previously-allocated memory, or %NULL.
277 * @n_bytes: number of bytes to allocate.
279 * Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
280 * on failure. Contrast with g_realloc(), which aborts the program
283 * If @mem is %NULL, behaves the same as g_try_malloc().
285 * Returns: the allocated memory, or %NULL.
288 g_try_realloc (gpointer mem
,
293 if (G_LIKELY (n_bytes
))
294 newmem
= realloc (mem
, n_bytes
);
302 TRACE (GLIB_MEM_REALLOC((void*) newmem
, (void*)mem
, (unsigned int) n_bytes
, 1));
308 #define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((b) > 0 && (a) > G_MAXSIZE / (b)))
312 * @n_blocks: the number of blocks to allocate
313 * @n_block_bytes: the size of each block in bytes
315 * This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
316 * but care is taken to detect possible overflow during multiplication.
319 * Returns: a pointer to the allocated memory
322 g_malloc_n (gsize n_blocks
,
325 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
327 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT
"*%"G_GSIZE_FORMAT
" bytes",
328 G_STRLOC
, n_blocks
, n_block_bytes
);
331 return g_malloc (n_blocks
* n_block_bytes
);
336 * @n_blocks: the number of blocks to allocate
337 * @n_block_bytes: the size of each block in bytes
339 * This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
340 * but care is taken to detect possible overflow during multiplication.
343 * Returns: a pointer to the allocated memory
346 g_malloc0_n (gsize n_blocks
,
349 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
351 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT
"*%"G_GSIZE_FORMAT
" bytes",
352 G_STRLOC
, n_blocks
, n_block_bytes
);
355 return g_malloc0 (n_blocks
* n_block_bytes
);
360 * @mem: (nullable): the memory to reallocate
361 * @n_blocks: the number of blocks to allocate
362 * @n_block_bytes: the size of each block in bytes
364 * This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
365 * but care is taken to detect possible overflow during multiplication.
368 * Returns: the new address of the allocated memory
371 g_realloc_n (gpointer mem
,
375 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
377 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT
"*%"G_GSIZE_FORMAT
" bytes",
378 G_STRLOC
, n_blocks
, n_block_bytes
);
381 return g_realloc (mem
, n_blocks
* n_block_bytes
);
386 * @n_blocks: the number of blocks to allocate
387 * @n_block_bytes: the size of each block in bytes
389 * This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
390 * but care is taken to detect possible overflow during multiplication.
393 * Returns: the allocated memory, or %NULL.
396 g_try_malloc_n (gsize n_blocks
,
399 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
402 return g_try_malloc (n_blocks
* n_block_bytes
);
407 * @n_blocks: the number of blocks to allocate
408 * @n_block_bytes: the size of each block in bytes
410 * This function is similar to g_try_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
411 * but care is taken to detect possible overflow during multiplication.
414 * Returns: the allocated memory, or %NULL
417 g_try_malloc0_n (gsize n_blocks
,
420 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
423 return g_try_malloc0 (n_blocks
* n_block_bytes
);
428 * @mem: (nullable): previously-allocated memory, or %NULL.
429 * @n_blocks: the number of blocks to allocate
430 * @n_block_bytes: the size of each block in bytes
432 * This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
433 * but care is taken to detect possible overflow during multiplication.
436 * Returns: the allocated memory, or %NULL.
439 g_try_realloc_n (gpointer mem
,
443 if (SIZE_OVERFLOWS (n_blocks
, n_block_bytes
))
446 return g_try_realloc (mem
, n_blocks
* n_block_bytes
);
450 * g_mem_is_system_malloc:
452 * Checks whether the allocator used by g_malloc() is the system's
453 * malloc implementation. If it returns %TRUE memory allocated with
454 * malloc() can be used interchangeable with memory allocated using g_malloc().
455 * This function is useful for avoiding an extra copy of allocated memory returned
456 * by a non-GLib-based API.
458 * Returns: if %TRUE, malloc() and g_malloc() can be mixed.
460 * Deprecated: 2.46: GLib always uses the system malloc, so this function always
464 g_mem_is_system_malloc (void)
471 * @vtable: table of memory allocation routines.
473 * This function used to let you override the memory allocation function.
474 * However, its use was incompatible with the use of global constructors
475 * in GLib and GIO, because those use the GLib allocators before main is
476 * reached. Therefore this function is now deprecated and is just a stub.
478 * Deprecated: 2.46: This function now does nothing. Use other memory
479 * profiling tools instead
482 g_mem_set_vtable (GMemVTable
*vtable
)
484 g_warning (G_STRLOC
": custom memory allocation vtable not supported");
489 * glib_mem_profiler_table:
491 * Used to be a #GMemVTable containing profiling variants of the memory
492 * allocation functions, but this variable shouldn't be modified anymore.
494 * Deprecated: 2.46: Use other memory profiling tools instead
496 GMemVTable
*glib_mem_profiler_table
= &glib_mem_vtable
;
501 * GLib used to support some tools for memory profiling, but this
502 * no longer works. There are many other useful tools for memory
503 * profiling these days which can be used instead.
505 * Deprecated: 2.46: Use other memory profiling tools instead
510 g_warning (G_STRLOC
": memory profiling not supported");