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