g_clear_pointer: work around gcc helpfulness
[platform/upstream/glib.git] / glib / gmem.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_MEM_H__
32 #define __G_MEM_H__
33
34 #include <glib/gtypes.h>
35
36 G_BEGIN_DECLS
37
38 /**
39  * GMemVTable:
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.
46  * 
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.
50  */
51 typedef struct _GMemVTable GMemVTable;
52
53
54 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
55 /**
56  * G_MEM_ALIGN:
57  *
58  * Indicates the number of bytes to which memory will be aligned on the
59  * current platform.
60  */
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 */
65
66
67 /* Memory allocation functions
68  */
69
70 void     g_free           (gpointer      mem);
71
72 GLIB_AVAILABLE_IN_2_34
73 void     g_clear_pointer  (gpointer      *pp,
74                            GDestroyNotify destroy);
75
76 gpointer g_malloc         (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
77 gpointer g_malloc0        (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
78 gpointer g_realloc        (gpointer      mem,
79                            gsize         n_bytes) G_GNUC_WARN_UNUSED_RESULT;
80 gpointer g_try_malloc     (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
81 gpointer g_try_malloc0    (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
82 gpointer g_try_realloc    (gpointer      mem,
83                            gsize         n_bytes) G_GNUC_WARN_UNUSED_RESULT;
84
85 gpointer g_malloc_n       (gsize         n_blocks,
86                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
87 gpointer g_malloc0_n      (gsize         n_blocks,
88                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
89 gpointer g_realloc_n      (gpointer      mem,
90                            gsize         n_blocks,
91                            gsize         n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
92 gpointer g_try_malloc_n   (gsize         n_blocks,
93                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
94 gpointer g_try_malloc0_n  (gsize         n_blocks,
95                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
96 gpointer g_try_realloc_n  (gpointer      mem,
97                            gsize         n_blocks,
98                            gsize         n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
99
100 #define g_clear_pointer(pp, destroy) \
101   G_STMT_START {                                                               \
102     G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));                       \
103     /* Only one access, please */                                              \
104     gpointer *_pp = (gpointer *) (pp);                                         \
105     gpointer _p;                                                               \
106     /* This assignment is needed to avoid a gcc warning */                     \
107     GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
108                                                                                \
109     (void) (0 ? (gpointer) *(pp) : 0);                                         \
110     do                                                                         \
111       _p = g_atomic_pointer_get (_pp);                                         \
112     while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_pp, _p, NULL)); \
113                                                                                \
114     if (_p)                                                                    \
115       _destroy (_p);                                                           \
116   } G_STMT_END
117
118 /* Optimise: avoid the call to the (slower) _n function if we can
119  * determine at compile-time that no overflow happens.
120  */
121 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
122 #  define _G_NEW(struct_type, n_structs, func) \
123         (struct_type *) (G_GNUC_EXTENSION ({                    \
124           gsize __n = (gsize) (n_structs);                      \
125           gsize __s = sizeof (struct_type);                     \
126           gpointer __p;                                         \
127           if (__s == 1)                                         \
128             __p = g_##func (__n);                               \
129           else if (__builtin_constant_p (__n) &&                \
130                    (__s == 0 || __n <= G_MAXSIZE / __s))        \
131             __p = g_##func (__n * __s);                         \
132           else                                                  \
133             __p = g_##func##_n (__n, __s);                      \
134           __p;                                                  \
135         }))
136 #  define _G_RENEW(struct_type, mem, n_structs, func) \
137         (struct_type *) (G_GNUC_EXTENSION ({                    \
138           gsize __n = (gsize) (n_structs);                      \
139           gsize __s = sizeof (struct_type);                     \
140           gpointer __p = (gpointer) (mem);                      \
141           if (__s == 1)                                         \
142             __p = g_##func (__p, __n);                          \
143           else if (__builtin_constant_p (__n) &&                \
144                    (__s == 0 || __n <= G_MAXSIZE / __s))        \
145             __p = g_##func (__p, __n * __s);                    \
146           else                                                  \
147             __p = g_##func##_n (__p, __n, __s);                 \
148           __p;                                                  \
149         }))
150
151 #else
152
153 /* Unoptimised version: always call the _n() function. */
154
155 #define _G_NEW(struct_type, n_structs, func) \
156         ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
157 #define _G_RENEW(struct_type, mem, n_structs, func) \
158         ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
159
160 #endif
161
162 /**
163  * g_new:
164  * @struct_type: the type of the elements to allocate
165  * @n_structs: the number of elements to allocate
166  * 
167  * Allocates @n_structs elements of type @struct_type.
168  * The returned pointer is cast to a pointer to the given type.
169  * If @n_structs is 0 it returns %NULL.
170  * Care is taken to avoid overflow when calculating the size of the allocated block.
171  * 
172  * Since the returned pointer is already casted to the right type,
173  * it is normally unnecessary to cast it explicitly, and doing
174  * so might hide memory allocation errors.
175  * 
176  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
177  */
178 #define g_new(struct_type, n_structs)                   _G_NEW (struct_type, n_structs, malloc)
179 /**
180  * g_new0:
181  * @struct_type: the type of the elements to allocate.
182  * @n_structs: the number of elements to allocate.
183  * 
184  * Allocates @n_structs elements of type @struct_type, initialized to 0's.
185  * The returned pointer is cast to a pointer to the given type.
186  * If @n_structs is 0 it returns %NULL.
187  * Care is taken to avoid overflow when calculating the size of the allocated block.
188  * 
189  * Since the returned pointer is already casted to the right type,
190  * it is normally unnecessary to cast it explicitly, and doing
191  * so might hide memory allocation errors.
192  * 
193  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
194  */
195 #define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
196 /**
197  * g_renew:
198  * @struct_type: the type of the elements to allocate
199  * @mem: the currently allocated memory
200  * @n_structs: the number of elements to allocate
201  * 
202  * Reallocates the memory pointed to by @mem, so that it now has space for
203  * @n_structs elements of type @struct_type. It returns the new address of
204  * the memory, which may have been moved.
205  * Care is taken to avoid overflow when calculating the size of the allocated block.
206  * 
207  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
208  */
209 #define g_renew(struct_type, mem, n_structs)            _G_RENEW (struct_type, mem, n_structs, realloc)
210 /**
211  * g_try_new:
212  * @struct_type: the type of the elements to allocate
213  * @n_structs: the number of elements to allocate
214  * 
215  * Attempts to allocate @n_structs elements of type @struct_type, and returns
216  * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
217  * The returned pointer is cast to a pointer to the given type.
218  * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
219  * 
220  * Since: 2.8
221  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
222  */
223 #define g_try_new(struct_type, n_structs)               _G_NEW (struct_type, n_structs, try_malloc)
224 /**
225  * g_try_new0:
226  * @struct_type: the type of the elements to allocate
227  * @n_structs: the number of elements to allocate
228  * 
229  * Attempts to allocate @n_structs elements of type @struct_type, initialized
230  * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
231  * the program on failure.
232  * The returned pointer is cast to a pointer to the given type.
233  * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
234  * 
235  * Since: 2.8
236  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
237  */
238 #define g_try_new0(struct_type, n_structs)              _G_NEW (struct_type, n_structs, try_malloc0)
239 /**
240  * g_try_renew:
241  * @struct_type: the type of the elements to allocate
242  * @mem: the currently allocated memory
243  * @n_structs: the number of elements to allocate
244  * 
245  * Attempts to reallocate the memory pointed to by @mem, so that it now has
246  * space for @n_structs elements of type @struct_type, and returns %NULL on
247  * failure. Contrast with g_renew(), which aborts the program on failure.
248  * It returns the new address of the memory, which may have been moved.
249  * The function returns %NULL if an overflow occurs.
250  * 
251  * Since: 2.8
252  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
253  */
254 #define g_try_renew(struct_type, mem, n_structs)        _G_RENEW (struct_type, mem, n_structs, try_realloc)
255
256
257 /* Memory allocation virtualization for debugging purposes
258  * g_mem_set_vtable() has to be the very first GLib function called
259  * if being used
260  */
261 struct _GMemVTable {
262   gpointer (*malloc)      (gsize    n_bytes);
263   gpointer (*realloc)     (gpointer mem,
264                            gsize    n_bytes);
265   void     (*free)        (gpointer mem);
266   /* optional; set to NULL if not used ! */
267   gpointer (*calloc)      (gsize    n_blocks,
268                            gsize    n_block_bytes);
269   gpointer (*try_malloc)  (gsize    n_bytes);
270   gpointer (*try_realloc) (gpointer mem,
271                            gsize    n_bytes);
272 };
273 void     g_mem_set_vtable (GMemVTable   *vtable);
274 gboolean g_mem_is_system_malloc (void);
275
276 GLIB_VAR gboolean g_mem_gc_friendly;
277
278 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
279  */
280 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
281 void    g_mem_profile   (void);
282
283 G_END_DECLS
284
285 #endif /* __G_MEM_H__ */