[kdbus] sync with kdbus (kdbus.h - commit: 5ae1ecac44cb)
[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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
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/.
23  */
24
25 #ifndef __G_MEM_H__
26 #define __G_MEM_H__
27
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
31
32 #include <glib/gtypes.h>
33
34 G_BEGIN_DECLS
35
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.
44  * 
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.
48  */
49 typedef struct _GMemVTable GMemVTable;
50
51
52 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
53 /**
54  * G_MEM_ALIGN:
55  *
56  * Indicates the number of bytes to which memory will be aligned on the
57  * current platform.
58  */
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 */
63
64
65 /* Memory allocation functions
66  */
67
68 GLIB_AVAILABLE_IN_ALL
69 void     g_free           (gpointer      mem);
70
71 GLIB_AVAILABLE_IN_2_34
72 void     g_clear_pointer  (gpointer      *pp,
73                            GDestroyNotify destroy);
74
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;
89
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;
110
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);                      \
119                                                                                \
120     _p = *_pp;                                                                 \
121     if (_p)                                                                    \
122       {                                                                        \
123         *_pp = NULL;                                                           \
124         _destroy (_p);                                                         \
125       }                                                                        \
126   } G_STMT_END
127
128 /* Optimise: avoid the call to the (slower) _n function if we can
129  * determine at compile-time that no overflow happens.
130  */
131 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
132 #  define _G_NEW(struct_type, n_structs, func) \
133         (struct_type *) (G_GNUC_EXTENSION ({                    \
134           gsize __n = (gsize) (n_structs);                      \
135           gsize __s = sizeof (struct_type);                     \
136           gpointer __p;                                         \
137           if (__s == 1)                                         \
138             __p = g_##func (__n);                               \
139           else if (__builtin_constant_p (__n) &&                \
140                    (__s == 0 || __n <= G_MAXSIZE / __s))        \
141             __p = g_##func (__n * __s);                         \
142           else                                                  \
143             __p = g_##func##_n (__n, __s);                      \
144           __p;                                                  \
145         }))
146 #  define _G_RENEW(struct_type, mem, n_structs, func) \
147         (struct_type *) (G_GNUC_EXTENSION ({                    \
148           gsize __n = (gsize) (n_structs);                      \
149           gsize __s = sizeof (struct_type);                     \
150           gpointer __p = (gpointer) (mem);                      \
151           if (__s == 1)                                         \
152             __p = g_##func (__p, __n);                          \
153           else if (__builtin_constant_p (__n) &&                \
154                    (__s == 0 || __n <= G_MAXSIZE / __s))        \
155             __p = g_##func (__p, __n * __s);                    \
156           else                                                  \
157             __p = g_##func##_n (__p, __n, __s);                 \
158           __p;                                                  \
159         }))
160
161 #else
162
163 /* Unoptimised version: always call the _n() function. */
164
165 #define _G_NEW(struct_type, n_structs, func) \
166         ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
167 #define _G_RENEW(struct_type, mem, n_structs, func) \
168         ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
169
170 #endif
171
172 /**
173  * g_new:
174  * @struct_type: the type of the elements to allocate
175  * @n_structs: the number of elements to allocate
176  * 
177  * Allocates @n_structs elements of type @struct_type.
178  * The returned pointer is cast to a pointer to the given type.
179  * If @n_structs is 0 it returns %NULL.
180  * Care is taken to avoid overflow when calculating the size of the allocated block.
181  * 
182  * Since the returned pointer is already casted to the right type,
183  * it is normally unnecessary to cast it explicitly, and doing
184  * so might hide memory allocation errors.
185  * 
186  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
187  */
188 #define g_new(struct_type, n_structs)                   _G_NEW (struct_type, n_structs, malloc)
189 /**
190  * g_new0:
191  * @struct_type: the type of the elements to allocate.
192  * @n_structs: the number of elements to allocate.
193  * 
194  * Allocates @n_structs elements of type @struct_type, initialized to 0's.
195  * The returned pointer is cast to a pointer to the given type.
196  * If @n_structs is 0 it returns %NULL.
197  * Care is taken to avoid overflow when calculating the size of the allocated block.
198  * 
199  * Since the returned pointer is already casted to the right type,
200  * it is normally unnecessary to cast it explicitly, and doing
201  * so might hide memory allocation errors.
202  * 
203  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
204  */
205 #define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
206 /**
207  * g_renew:
208  * @struct_type: the type of the elements to allocate
209  * @mem: the currently allocated memory
210  * @n_structs: the number of elements to allocate
211  * 
212  * Reallocates the memory pointed to by @mem, so that it now has space for
213  * @n_structs elements of type @struct_type. It returns the new address of
214  * the memory, which may have been moved.
215  * Care is taken to avoid overflow when calculating the size of the allocated block.
216  * 
217  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
218  */
219 #define g_renew(struct_type, mem, n_structs)            _G_RENEW (struct_type, mem, n_structs, realloc)
220 /**
221  * g_try_new:
222  * @struct_type: the type of the elements to allocate
223  * @n_structs: the number of elements to allocate
224  * 
225  * Attempts to allocate @n_structs elements of type @struct_type, and returns
226  * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
227  * The returned pointer is cast to a pointer to the given type.
228  * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
229  * 
230  * Since: 2.8
231  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
232  */
233 #define g_try_new(struct_type, n_structs)               _G_NEW (struct_type, n_structs, try_malloc)
234 /**
235  * g_try_new0:
236  * @struct_type: the type of the elements to allocate
237  * @n_structs: the number of elements to allocate
238  * 
239  * Attempts to allocate @n_structs elements of type @struct_type, initialized
240  * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
241  * the program on failure.
242  * The returned pointer is cast to a pointer to the given type.
243  * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
244  * 
245  * Since: 2.8
246  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
247  */
248 #define g_try_new0(struct_type, n_structs)              _G_NEW (struct_type, n_structs, try_malloc0)
249 /**
250  * g_try_renew:
251  * @struct_type: the type of the elements to allocate
252  * @mem: the currently allocated memory
253  * @n_structs: the number of elements to allocate
254  * 
255  * Attempts to reallocate the memory pointed to by @mem, so that it now has
256  * space for @n_structs elements of type @struct_type, and returns %NULL on
257  * failure. Contrast with g_renew(), which aborts the program on failure.
258  * It returns the new address of the memory, which may have been moved.
259  * The function returns %NULL if an overflow occurs.
260  * 
261  * Since: 2.8
262  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
263  */
264 #define g_try_renew(struct_type, mem, n_structs)        _G_RENEW (struct_type, mem, n_structs, try_realloc)
265
266
267 /* Memory allocation virtualization for debugging purposes
268  * g_mem_set_vtable() has to be the very first GLib function called
269  * if being used
270  */
271 struct _GMemVTable {
272   gpointer (*malloc)      (gsize    n_bytes);
273   gpointer (*realloc)     (gpointer mem,
274                            gsize    n_bytes);
275   void     (*free)        (gpointer mem);
276   /* optional; set to NULL if not used ! */
277   gpointer (*calloc)      (gsize    n_blocks,
278                            gsize    n_block_bytes);
279   gpointer (*try_malloc)  (gsize    n_bytes);
280   gpointer (*try_realloc) (gpointer mem,
281                            gsize    n_bytes);
282 };
283 GLIB_AVAILABLE_IN_ALL
284 void     g_mem_set_vtable (GMemVTable   *vtable);
285 GLIB_AVAILABLE_IN_ALL
286 gboolean g_mem_is_system_malloc (void);
287
288 GLIB_VAR gboolean g_mem_gc_friendly;
289
290 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
291  */
292 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
293 GLIB_AVAILABLE_IN_ALL
294 void    g_mem_profile   (void);
295
296 G_END_DECLS
297
298 #endif /* __G_MEM_H__ */