Bug 608196 - Overflow-safe g_new family
[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(G_DISABLE_SINGLE_INCLUDES) && !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/gslice.h>
35 #include <glib/gtypes.h>
36
37 G_BEGIN_DECLS
38
39 typedef struct _GMemVTable GMemVTable;
40
41
42 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
43 #  define G_MEM_ALIGN   GLIB_SIZEOF_VOID_P
44 #else   /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
45 #  define G_MEM_ALIGN   GLIB_SIZEOF_LONG
46 #endif  /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
47
48
49 /* Memory allocation functions
50  */
51
52 void     g_free           (gpointer      mem);
53
54 gpointer g_malloc         (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
55 gpointer g_malloc0        (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
56 gpointer g_realloc        (gpointer      mem,
57                            gsize         n_bytes) G_GNUC_WARN_UNUSED_RESULT;
58 gpointer g_try_malloc     (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
59 gpointer g_try_malloc0    (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
60 gpointer g_try_realloc    (gpointer      mem,
61                            gsize         n_bytes) G_GNUC_WARN_UNUSED_RESULT;
62
63 gpointer g_malloc_n       (gsize         n_blocks,
64                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
65 gpointer g_malloc0_n      (gsize         n_blocks,
66                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
67 gpointer g_realloc_n      (gpointer      mem,
68                            gsize         n_blocks,
69                            gsize         n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
70 gpointer g_try_malloc_n   (gsize         n_blocks,
71                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
72 gpointer g_try_malloc0_n  (gsize         n_blocks,
73                            gsize         n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
74 gpointer g_try_realloc_n  (gpointer      mem,
75                            gsize         n_blocks,
76                            gsize         n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
77
78
79 /* Optimise: avoid the call to the (slower) _n function if we can
80  * determine at compile-time that no overflow happens.
81  */
82 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
83 #  define _G_NEW(struct_type, n_structs, func) \
84         (struct_type *) (__extension__ ({                       \
85           gsize __n = (gsize) (n_structs);                      \
86           gpointer __p;                                         \
87           if (sizeof (struct_type) == 1)                        \
88             __p = g_##func (__n);                               \
89           else if (__builtin_constant_p (__n) &&                \
90                    __n <= G_MAXSIZE / sizeof (struct_type))     \
91             __p = g_##func (__n * sizeof (struct_type));        \
92           else                                                  \
93             __p = g_##func##_n (__n, sizeof (struct_type));     \
94           __p;                                                  \
95         }))
96 #  define _G_RENEW(struct_type, mem, n_structs, func) \
97         (struct_type *) (__extension__ ({                       \
98           gsize __n = (gsize) (n_structs);                      \
99           gpointer __p = (gpointer) (mem);                      \
100           if (sizeof (struct_type) == 1)                        \
101             __p = g_##func (__p, __n);                          \
102           else if (__builtin_constant_p (__n) &&                \
103                    __n <= G_MAXSIZE / sizeof (struct_type))     \
104             __p = g_##func (__p, __n * sizeof (struct_type));   \
105           else                                                  \
106             __p = g_##func##_n (__p, __n, sizeof (struct_type));\
107           __p;                                                  \
108         }))
109
110 #else
111
112 /* Unoptimised version: always call the _n() function. */
113
114 #define _G_NEW(struct_type, n_structs, func) \
115         ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
116 #define _G_RENEW(struct_type, mem, n_structs, func) \
117         ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
118
119 #endif
120
121 #define g_new(struct_type, n_structs)                   _G_NEW (struct_type, n_structs, malloc)
122 #define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
123 #define g_renew(struct_type, mem, n_structs)            _G_RENEW (struct_type, mem, n_structs, realloc)
124 #define g_try_new(struct_type, n_structs)               _G_NEW (struct_type, n_structs, try_malloc)
125 #define g_try_new0(struct_type, n_structs)              _G_NEW (struct_type, n_structs, try_malloc0)
126 #define g_try_renew(struct_type, mem, n_structs)        _G_RENEW (struct_type, mem, n_structs, try_realloc)
127
128
129 /* Memory allocation virtualization for debugging purposes
130  * g_mem_set_vtable() has to be the very first GLib function called
131  * if being used
132  */
133 struct _GMemVTable {
134   gpointer (*malloc)      (gsize    n_bytes);
135   gpointer (*realloc)     (gpointer mem,
136                            gsize    n_bytes);
137   void     (*free)        (gpointer mem);
138   /* optional; set to NULL if not used ! */
139   gpointer (*calloc)      (gsize    n_blocks,
140                            gsize    n_block_bytes);
141   gpointer (*try_malloc)  (gsize    n_bytes);
142   gpointer (*try_realloc) (gpointer mem,
143                            gsize    n_bytes);
144 };
145 void     g_mem_set_vtable (GMemVTable   *vtable);
146 gboolean g_mem_is_system_malloc (void);
147
148 GLIB_VAR gboolean g_mem_gc_friendly;
149
150 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
151  */
152 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
153 void    g_mem_profile   (void);
154
155
156 /* deprecated memchunks and allocators */
157 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
158 typedef struct _GAllocator GAllocator;
159 typedef struct _GMemChunk  GMemChunk;
160 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
161   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
162                    sizeof (type), \
163                    sizeof (type) * (pre_alloc), \
164                    (alloc_type)) \
165 )
166 #define g_chunk_new(type, chunk)        ( \
167   (type *) g_mem_chunk_alloc (chunk) \
168 )
169 #define g_chunk_new0(type, chunk)       ( \
170   (type *) g_mem_chunk_alloc0 (chunk) \
171 )
172 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
173   g_mem_chunk_free ((mem_chunk), (mem)); \
174 } G_STMT_END
175 #define G_ALLOC_ONLY      1
176 #define G_ALLOC_AND_FREE  2
177 GMemChunk* g_mem_chunk_new     (const gchar *name,
178                                 gint         atom_size,
179                                 gsize        area_size,
180                                 gint         type);
181 void       g_mem_chunk_destroy (GMemChunk   *mem_chunk);
182 gpointer   g_mem_chunk_alloc   (GMemChunk   *mem_chunk);
183 gpointer   g_mem_chunk_alloc0  (GMemChunk   *mem_chunk);
184 void       g_mem_chunk_free    (GMemChunk   *mem_chunk,
185                                 gpointer     mem);
186 void       g_mem_chunk_clean   (GMemChunk   *mem_chunk);
187 void       g_mem_chunk_reset   (GMemChunk   *mem_chunk);
188 void       g_mem_chunk_print   (GMemChunk   *mem_chunk);
189 void       g_mem_chunk_info    (void);
190 void       g_blow_chunks       (void);
191 GAllocator*g_allocator_new     (const gchar  *name,
192                                 guint         n_preallocs);
193 void       g_allocator_free    (GAllocator   *allocator);
194 #define G_ALLOCATOR_LIST       (1)
195 #define G_ALLOCATOR_SLIST      (2)
196 #define G_ALLOCATOR_NODE       (3)
197 #endif /* G_DISABLE_DEPRECATED */
198
199 G_END_DECLS
200
201 #endif /* __G_MEM_H__ */