Moving files to packaging and extracing new tarball.
[profile/ivi/glib2.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 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;
80
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,
86                            gsize         n_blocks,
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,
93                            gsize         n_blocks,
94                            gsize         n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
95
96
97 /* Optimise: avoid the call to the (slower) _n function if we can
98  * determine at compile-time that no overflow happens.
99  */
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);                     \
105           gpointer __p;                                         \
106           if (__s == 1)                                         \
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);                         \
111           else                                                  \
112             __p = g_##func##_n (__n, __s);                      \
113           __p;                                                  \
114         }))
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);                      \
120           if (__s == 1)                                         \
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);                    \
125           else                                                  \
126             __p = g_##func##_n (__p, __n, __s);                 \
127           __p;                                                  \
128         }))
129
130 #else
131
132 /* Unoptimised version: always call the _n() function. */
133
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)))
138
139 #endif
140
141 /**
142  * g_new:
143  * @struct_type: the type of the elements to allocate
144  * @n_structs: the number of elements to allocate
145  * 
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.
150  * 
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.
154  * 
155  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
156  */
157 #define g_new(struct_type, n_structs)                   _G_NEW (struct_type, n_structs, malloc)
158 /**
159  * g_new0:
160  * @struct_type: the type of the elements to allocate.
161  * @n_structs: the number of elements to allocate.
162  * 
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.
167  * 
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.
171  * 
172  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
173  */
174 #define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
175 /**
176  * g_renew:
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
180  * 
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.
185  * 
186  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
187  */
188 #define g_renew(struct_type, mem, n_structs)            _G_RENEW (struct_type, mem, n_structs, realloc)
189 /**
190  * g_try_new:
191  * @struct_type: the type of the elements to allocate
192  * @n_structs: the number of elements to allocate
193  * 
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.
198  * 
199  * Since: 2.8
200  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
201  */
202 #define g_try_new(struct_type, n_structs)               _G_NEW (struct_type, n_structs, try_malloc)
203 /**
204  * g_try_new0:
205  * @struct_type: the type of the elements to allocate
206  * @n_structs: the number of elements to allocate
207  * 
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.
213  * 
214  * Since: 2.8
215  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
216  */
217 #define g_try_new0(struct_type, n_structs)              _G_NEW (struct_type, n_structs, try_malloc0)
218 /**
219  * g_try_renew:
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
223  * 
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.
229  * 
230  * Since: 2.8
231  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
232  */
233 #define g_try_renew(struct_type, mem, n_structs)        _G_RENEW (struct_type, mem, n_structs, try_realloc)
234
235
236 /* Memory allocation virtualization for debugging purposes
237  * g_mem_set_vtable() has to be the very first GLib function called
238  * if being used
239  */
240 struct _GMemVTable {
241   gpointer (*malloc)      (gsize    n_bytes);
242   gpointer (*realloc)     (gpointer mem,
243                            gsize    n_bytes);
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,
250                            gsize    n_bytes);
251 };
252 void     g_mem_set_vtable (GMemVTable   *vtable);
253 gboolean g_mem_is_system_malloc (void);
254
255 GLIB_VAR gboolean g_mem_gc_friendly;
256
257 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
258  */
259 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
260 void    g_mem_profile   (void);
261
262 G_END_DECLS
263
264 #endif /* __G_MEM_H__ */