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 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
27 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
34 #include <glib/gtypes.h>
40 * @malloc: function to use for allocating memory.
41 * @realloc: function to use for reallocating memory.
42 * @free: function to use to free memory.
43 * @calloc: function to use for allocating zero-filled memory.
44 * @try_malloc: function to use for allocating memory without a default error handler.
45 * @try_realloc: function to use for reallocating memory without a default error handler.
47 * A set of functions used to perform memory allocation. The same #GMemVTable must
48 * be used for all allocations in the same program; a call to g_mem_set_vtable(),
49 * if it exists, should be prior to any use of GLib.
51 typedef struct _GMemVTable GMemVTable;
54 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
58 * Indicates the number of bytes to which memory will be aligned on the
61 # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
62 #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
63 # define G_MEM_ALIGN GLIB_SIZEOF_LONG
64 #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
67 /* Memory allocation functions
70 void g_free (gpointer mem);
72 gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
73 gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
74 gpointer g_realloc (gpointer mem,
75 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
76 gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
77 gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
78 gpointer g_try_realloc (gpointer mem,
79 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
81 gpointer g_malloc_n (gsize n_blocks,
82 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
83 gpointer g_malloc0_n (gsize n_blocks,
84 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
85 gpointer g_realloc_n (gpointer mem,
87 gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
88 gpointer g_try_malloc_n (gsize n_blocks,
89 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
90 gpointer g_try_malloc0_n (gsize n_blocks,
91 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
92 gpointer g_try_realloc_n (gpointer mem,
94 gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
97 /* Optimise: avoid the call to the (slower) _n function if we can
98 * determine at compile-time that no overflow happens.
100 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
101 # define _G_NEW(struct_type, n_structs, func) \
102 (struct_type *) (G_GNUC_EXTENSION ({ \
103 gsize __n = (gsize) (n_structs); \
104 gsize __s = sizeof (struct_type); \
107 __p = g_##func (__n); \
108 else if (__builtin_constant_p (__n) && \
109 (__s == 0 || __n <= G_MAXSIZE / __s)) \
110 __p = g_##func (__n * __s); \
112 __p = g_##func##_n (__n, __s); \
115 # define _G_RENEW(struct_type, mem, n_structs, func) \
116 (struct_type *) (G_GNUC_EXTENSION ({ \
117 gsize __n = (gsize) (n_structs); \
118 gsize __s = sizeof (struct_type); \
119 gpointer __p = (gpointer) (mem); \
121 __p = g_##func (__p, __n); \
122 else if (__builtin_constant_p (__n) && \
123 (__s == 0 || __n <= G_MAXSIZE / __s)) \
124 __p = g_##func (__p, __n * __s); \
126 __p = g_##func##_n (__p, __n, __s); \
132 /* Unoptimised version: always call the _n() function. */
134 #define _G_NEW(struct_type, n_structs, func) \
135 ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
136 #define _G_RENEW(struct_type, mem, n_structs, func) \
137 ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
143 * @struct_type: the type of the elements to allocate
144 * @n_structs: the number of elements to allocate
146 * Allocates @n_structs elements of type @struct_type.
147 * The returned pointer is cast to a pointer to the given type.
148 * If @n_structs is 0 it returns %NULL.
149 * Care is taken to avoid overflow when calculating the size of the allocated block.
151 * Since the returned pointer is already casted to the right type,
152 * it is normally unnecessary to cast it explicitly, and doing
153 * so might hide memory allocation errors.
155 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
157 #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
160 * @struct_type: the type of the elements to allocate.
161 * @n_structs: the number of elements to allocate.
163 * Allocates @n_structs elements of type @struct_type, initialized to 0's.
164 * The returned pointer is cast to a pointer to the given type.
165 * If @n_structs is 0 it returns %NULL.
166 * Care is taken to avoid overflow when calculating the size of the allocated block.
168 * Since the returned pointer is already casted to the right type,
169 * it is normally unnecessary to cast it explicitly, and doing
170 * so might hide memory allocation errors.
172 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
174 #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
177 * @struct_type: the type of the elements to allocate
178 * @mem: the currently allocated memory
179 * @n_structs: the number of elements to allocate
181 * Reallocates the memory pointed to by @mem, so that it now has space for
182 * @n_structs elements of type @struct_type. It returns the new address of
183 * the memory, which may have been moved.
184 * Care is taken to avoid overflow when calculating the size of the allocated block.
186 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
188 #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
191 * @struct_type: the type of the elements to allocate
192 * @n_structs: the number of elements to allocate
194 * Attempts to allocate @n_structs elements of type @struct_type, and returns
195 * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
196 * The returned pointer is cast to a pointer to the given type.
197 * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
200 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
202 #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
205 * @struct_type: the type of the elements to allocate
206 * @n_structs: the number of elements to allocate
208 * Attempts to allocate @n_structs elements of type @struct_type, initialized
209 * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
210 * the program on failure.
211 * The returned pointer is cast to a pointer to the given type.
212 * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
215 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
217 #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
220 * @struct_type: the type of the elements to allocate
221 * @mem: the currently allocated memory
222 * @n_structs: the number of elements to allocate
224 * Attempts to reallocate the memory pointed to by @mem, so that it now has
225 * space for @n_structs elements of type @struct_type, and returns %NULL on
226 * failure. Contrast with g_renew(), which aborts the program on failure.
227 * It returns the new address of the memory, which may have been moved.
228 * The function returns %NULL if an overflow occurs.
231 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
233 #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
236 /* Memory allocation virtualization for debugging purposes
237 * g_mem_set_vtable() has to be the very first GLib function called
241 gpointer (*malloc) (gsize n_bytes);
242 gpointer (*realloc) (gpointer mem,
244 void (*free) (gpointer mem);
245 /* optional; set to NULL if not used ! */
246 gpointer (*calloc) (gsize n_blocks,
247 gsize n_block_bytes);
248 gpointer (*try_malloc) (gsize n_bytes);
249 gpointer (*try_realloc) (gpointer mem,
252 void g_mem_set_vtable (GMemVTable *vtable);
253 gboolean g_mem_is_system_malloc (void);
255 GLIB_VAR gboolean g_mem_gc_friendly;
257 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
259 GLIB_VAR GMemVTable *glib_mem_profiler_table;
260 void g_mem_profile (void);
264 #endif /* __G_MEM_H__ */