GSimpleProxyResolver: convert docs to markdown
[glib.git] / glib / gmem.h
blobe1d88d5a19d84af1d4fd92681a53c97cb7dda10c
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, 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/.
25 #ifndef __G_MEM_H__
26 #define __G_MEM_H__
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
32 #include <glib/gtypes.h>
34 G_BEGIN_DECLS
36 /**
37 * GMemVTable:
38 * @malloc: function to use for allocating memory.
39 * @realloc: function to use for reallocating memory.
40 * @free: function to use to free memory.
41 * @calloc: function to use for allocating zero-filled memory.
42 * @try_malloc: function to use for allocating memory without a default error handler.
43 * @try_realloc: function to use for reallocating memory without a default error handler.
45 * A set of functions used to perform memory allocation. The same #GMemVTable must
46 * be used for all allocations in the same program; a call to g_mem_set_vtable(),
47 * if it exists, should be prior to any use of GLib.
49 typedef struct _GMemVTable GMemVTable;
52 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
53 /**
54 * G_MEM_ALIGN:
56 * Indicates the number of bytes to which memory will be aligned on the
57 * current platform.
59 # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
60 #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
61 # define G_MEM_ALIGN GLIB_SIZEOF_LONG
62 #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
65 /* Memory allocation functions
68 GLIB_AVAILABLE_IN_ALL
69 void g_free (gpointer mem);
71 GLIB_AVAILABLE_IN_2_34
72 void g_clear_pointer (gpointer *pp,
73 GDestroyNotify destroy);
75 GLIB_AVAILABLE_IN_ALL
76 gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
77 GLIB_AVAILABLE_IN_ALL
78 gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
79 GLIB_AVAILABLE_IN_ALL
80 gpointer g_realloc (gpointer mem,
81 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
82 GLIB_AVAILABLE_IN_ALL
83 gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
84 GLIB_AVAILABLE_IN_ALL
85 gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
86 GLIB_AVAILABLE_IN_ALL
87 gpointer g_try_realloc (gpointer mem,
88 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
90 GLIB_AVAILABLE_IN_ALL
91 gpointer g_malloc_n (gsize n_blocks,
92 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
93 GLIB_AVAILABLE_IN_ALL
94 gpointer g_malloc0_n (gsize n_blocks,
95 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
96 GLIB_AVAILABLE_IN_ALL
97 gpointer g_realloc_n (gpointer mem,
98 gsize n_blocks,
99 gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
100 GLIB_AVAILABLE_IN_ALL
101 gpointer g_try_malloc_n (gsize n_blocks,
102 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
103 GLIB_AVAILABLE_IN_ALL
104 gpointer g_try_malloc0_n (gsize n_blocks,
105 gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
106 GLIB_AVAILABLE_IN_ALL
107 gpointer g_try_realloc_n (gpointer mem,
108 gsize n_blocks,
109 gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
111 #define g_clear_pointer(pp, destroy) \
112 G_STMT_START { \
113 G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
114 /* Only one access, please */ \
115 gpointer *_pp = (gpointer *) (pp); \
116 gpointer _p; \
117 /* This assignment is needed to avoid a gcc warning */ \
118 GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
120 (void) (0 ? (gpointer) *(pp) : 0); \
121 do \
122 _p = g_atomic_pointer_get (_pp); \
123 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_pp, _p, NULL)); \
125 if (_p) \
126 _destroy (_p); \
127 } G_STMT_END
129 /* Optimise: avoid the call to the (slower) _n function if we can
130 * determine at compile-time that no overflow happens.
132 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
133 # define _G_NEW(struct_type, n_structs, func) \
134 (struct_type *) (G_GNUC_EXTENSION ({ \
135 gsize __n = (gsize) (n_structs); \
136 gsize __s = sizeof (struct_type); \
137 gpointer __p; \
138 if (__s == 1) \
139 __p = g_##func (__n); \
140 else if (__builtin_constant_p (__n) && \
141 (__s == 0 || __n <= G_MAXSIZE / __s)) \
142 __p = g_##func (__n * __s); \
143 else \
144 __p = g_##func##_n (__n, __s); \
145 __p; \
147 # define _G_RENEW(struct_type, mem, n_structs, func) \
148 (struct_type *) (G_GNUC_EXTENSION ({ \
149 gsize __n = (gsize) (n_structs); \
150 gsize __s = sizeof (struct_type); \
151 gpointer __p = (gpointer) (mem); \
152 if (__s == 1) \
153 __p = g_##func (__p, __n); \
154 else if (__builtin_constant_p (__n) && \
155 (__s == 0 || __n <= G_MAXSIZE / __s)) \
156 __p = g_##func (__p, __n * __s); \
157 else \
158 __p = g_##func##_n (__p, __n, __s); \
159 __p; \
162 #else
164 /* Unoptimised version: always call the _n() function. */
166 #define _G_NEW(struct_type, n_structs, func) \
167 ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
168 #define _G_RENEW(struct_type, mem, n_structs, func) \
169 ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
171 #endif
174 * g_new:
175 * @struct_type: the type of the elements to allocate
176 * @n_structs: the number of elements to allocate
178 * Allocates @n_structs elements of type @struct_type.
179 * The returned pointer is cast to a pointer to the given type.
180 * If @n_structs is 0 it returns %NULL.
181 * Care is taken to avoid overflow when calculating the size of the allocated block.
183 * Since the returned pointer is already casted to the right type,
184 * it is normally unnecessary to cast it explicitly, and doing
185 * so might hide memory allocation errors.
187 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
189 #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
191 * g_new0:
192 * @struct_type: the type of the elements to allocate.
193 * @n_structs: the number of elements to allocate.
195 * Allocates @n_structs elements of type @struct_type, initialized to 0's.
196 * The returned pointer is cast to a pointer to the given type.
197 * If @n_structs is 0 it returns %NULL.
198 * Care is taken to avoid overflow when calculating the size of the allocated block.
200 * Since the returned pointer is already casted to the right type,
201 * it is normally unnecessary to cast it explicitly, and doing
202 * so might hide memory allocation errors.
204 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
206 #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
208 * g_renew:
209 * @struct_type: the type of the elements to allocate
210 * @mem: the currently allocated memory
211 * @n_structs: the number of elements to allocate
213 * Reallocates the memory pointed to by @mem, so that it now has space for
214 * @n_structs elements of type @struct_type. It returns the new address of
215 * the memory, which may have been moved.
216 * Care is taken to avoid overflow when calculating the size of the allocated block.
218 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
220 #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
222 * g_try_new:
223 * @struct_type: the type of the elements to allocate
224 * @n_structs: the number of elements to allocate
226 * Attempts to allocate @n_structs elements of type @struct_type, and returns
227 * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
228 * The returned pointer is cast to a pointer to the given type.
229 * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
231 * Since: 2.8
232 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
234 #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
236 * g_try_new0:
237 * @struct_type: the type of the elements to allocate
238 * @n_structs: the number of elements to allocate
240 * Attempts to allocate @n_structs elements of type @struct_type, initialized
241 * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
242 * the program on failure.
243 * The returned pointer is cast to a pointer to the given type.
244 * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
246 * Since: 2.8
247 * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
249 #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
251 * g_try_renew:
252 * @struct_type: the type of the elements to allocate
253 * @mem: the currently allocated memory
254 * @n_structs: the number of elements to allocate
256 * Attempts to reallocate the memory pointed to by @mem, so that it now has
257 * space for @n_structs elements of type @struct_type, and returns %NULL on
258 * failure. Contrast with g_renew(), which aborts the program on failure.
259 * It returns the new address of the memory, which may have been moved.
260 * The function returns %NULL if an overflow occurs.
262 * Since: 2.8
263 * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
265 #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
268 /* Memory allocation virtualization for debugging purposes
269 * g_mem_set_vtable() has to be the very first GLib function called
270 * if being used
272 struct _GMemVTable {
273 gpointer (*malloc) (gsize n_bytes);
274 gpointer (*realloc) (gpointer mem,
275 gsize n_bytes);
276 void (*free) (gpointer mem);
277 /* optional; set to NULL if not used ! */
278 gpointer (*calloc) (gsize n_blocks,
279 gsize n_block_bytes);
280 gpointer (*try_malloc) (gsize n_bytes);
281 gpointer (*try_realloc) (gpointer mem,
282 gsize n_bytes);
284 GLIB_AVAILABLE_IN_ALL
285 void g_mem_set_vtable (GMemVTable *vtable);
286 GLIB_AVAILABLE_IN_ALL
287 gboolean g_mem_is_system_malloc (void);
289 GLIB_VAR gboolean g_mem_gc_friendly;
291 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
293 GLIB_VAR GMemVTable *glib_mem_profiler_table;
294 GLIB_AVAILABLE_IN_ALL
295 void g_mem_profile (void);
297 G_END_DECLS
299 #endif /* __G_MEM_H__ */