1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
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/.
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
34 #include <glib/gutils.h>
35 #include <glib/glib-typeof.h>
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.
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.
52 * This functions related to this has been deprecated in 2.46, and no longer work.
54 typedef struct _GMemVTable GMemVTable;
57 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
61 * Indicates the number of bytes to which memory will be aligned on the
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 */
70 /* Memory allocation functions
74 void g_free (gpointer mem);
76 GLIB_AVAILABLE_IN_2_34
77 void g_clear_pointer (gpointer *pp,
78 GDestroyNotify destroy);
81 gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
83 gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
85 gpointer g_realloc (gpointer mem,
86 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
88 gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
90 gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
92 gpointer g_try_realloc (gpointer mem,
93 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
96 gpointer g_malloc_n (gsize n_blocks,
97 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
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,
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,
114 gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
116 GLIB_AVAILABLE_IN_2_72
117 gpointer g_aligned_alloc (gsize n_blocks,
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,
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);
127 #if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
128 #define g_clear_pointer(pp, destroy) \
131 G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
132 glib_typeof ((pp)) _pp = (pp); \
133 glib_typeof (*(pp)) _ptr = *_pp; \
139 GLIB_AVAILABLE_MACRO_IN_2_34
141 #define g_clear_pointer(pp, destroy) \
143 G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
144 /* Only one access, please; work around type aliasing */ \
145 union { char *in; gpointer *out; } _pp; \
147 /* This assignment is needed to avoid a gcc warning */ \
148 GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
150 _pp.in = (char *) (pp); \
158 GLIB_AVAILABLE_MACRO_IN_2_34
159 #endif /* __GNUC__ */
163 * @pp: (not nullable): a pointer to a pointer
165 * Sets @pp to %NULL, returning the value that was there before.
167 * Conceptually, this transfers the ownership of the pointer from the
168 * referenced variable to the "caller" of the macro (ie: "steals" the
171 * The return value will be properly typed, according to the type of
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):
180 * create_object (void)
182 * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
184 * if (early_error_case)
187 * return g_steal_pointer (&obj);
191 * It can also be used in similar ways for 'out' parameters and is
192 * particularly useful for dealing with optional out parameters:
196 * get_object (GObject **obj_out)
198 * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
200 * if (early_error_case)
204 * *obj_out = g_steal_pointer (&obj);
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
216 GLIB_AVAILABLE_STATIC_INLINE_IN_2_44
217 static inline gpointer
218 g_steal_pointer (gpointer pp)
220 gpointer *ptr = (gpointer *) pp;
230 #if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
231 #define g_steal_pointer(pp) ((glib_typeof (*pp)) (g_steal_pointer) (pp))
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__ */
239 /* Optimise: avoid the call to the (slower) _n function if we can
240 * determine at compile-time that no overflow happens.
242 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
243 # define _G_NEW(struct_type, n_structs, func) \
244 (struct_type *) (G_GNUC_EXTENSION ({ \
245 gsize __n = (gsize) (n_structs); \
246 gsize __s = sizeof (struct_type); \
249 __p = g_##func (__n); \
250 else if (__builtin_constant_p (__n) && \
251 (__s == 0 || __n <= G_MAXSIZE / __s)) \
252 __p = g_##func (__n * __s); \
254 __p = g_##func##_n (__n, __s); \
257 # define _G_RENEW(struct_type, mem, n_structs, func) \
258 (struct_type *) (G_GNUC_EXTENSION ({ \
259 gsize __n = (gsize) (n_structs); \
260 gsize __s = sizeof (struct_type); \
261 gpointer __p = (gpointer) (mem); \
263 __p = g_##func (__p, __n); \
264 else if (__builtin_constant_p (__n) && \
265 (__s == 0 || __n <= G_MAXSIZE / __s)) \
266 __p = g_##func (__p, __n * __s); \
268 __p = g_##func##_n (__p, __n, __s); \
274 /* Unoptimised version: always call the _n() function. */
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)))
285 * @struct_type: the type of the elements to allocate
286 * @n_structs: the number of elements to allocate
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.
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.
297 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
299 #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
302 * @struct_type: the type of the elements to allocate.
303 * @n_structs: the number of elements to allocate.
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.
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.
314 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
316 #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
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
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.
328 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
330 #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
333 * @struct_type: the type of the elements to allocate
334 * @n_structs: the number of elements to allocate
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.
342 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
344 #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
347 * @struct_type: the type of the elements to allocate
348 * @n_structs: the number of elements to allocate
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.
357 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
359 #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
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
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.
373 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
375 #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
378 /* Memory allocation virtualization for debugging purposes
379 * g_mem_set_vtable() has to be the very first GLib function called
383 gpointer (*malloc) (gsize n_bytes);
384 gpointer (*realloc) (gpointer mem,
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,
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);
399 GLIB_VAR gboolean g_mem_gc_friendly;
401 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
403 GLIB_VAR GMemVTable *glib_mem_profiler_table;
404 GLIB_DEPRECATED_IN_2_46
405 void g_mem_profile (void);
409 #endif /* __G_MEM_H__ */