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 /* avoid the overflow check if we can determine at compile-time that no
80  * overflow happens. */
81 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
82 #  define _G_MALLOC_N(n_blocks, n_block_bytes, func_1, func_n) \
83         (__extension__ ({                                       \
84           gsize __a = (gsize) (n_blocks);                       \
85           gsize __b = (gsize) (n_block_bytes);                  \
86           gpointer __p;                                         \
87           if (__builtin_constant_p (__a) && __a == 1)           \
88             __p = func_1 (__b);                                 \
89           else if (__builtin_constant_p (__b) && __b == 1)      \
90             __p = func_1 (__a);                                 \
91           else if (__builtin_constant_p (__a) &&                \
92                    __builtin_constant_p (__b) &&                \
93                    __a <= G_MAXSIZE / __b)                      \
94             __p = func_1 (__a * __b);                           \
95           else                                                  \
96             __p = func_n (__a, __b);                            \
97           __p;                                                  \
98         }))
99 #  define _G_REALLOC_N(mem, n_blocks, n_block_bytes, func_1, func_n) \
100         (__extension__ ({                                       \
101           gsize __a = (gsize) (n_blocks);                       \
102           gsize __b = (gsize) (n_block_bytes);                  \
103           gpointer __p = (gpointer) (mem);                      \
104           if (__builtin_constant_p (__a) && __a == 1)           \
105             __p = func_1 (__p, __b);                            \
106           else if (__builtin_constant_p (__b) && __b == 1)      \
107             __p = func_1 (__p, __a);                            \
108           else if (__builtin_constant_p (__a) &&                \
109                    __builtin_constant_p (__b) &&                \
110                    __a <= G_MAXSIZE / __b)                      \
111             __p = func_1 (__p, __a * __b);                      \
112           else                                                  \
113             __p = func_n (__p, __a, __b);                       \
114           __p;                                                  \
115         }))
116
117 #  define g_malloc_n(n_blocks,n_block_bytes)            _G_MALLOC_N (n_blocks, n_block_bytes, g_malloc, g_malloc_n)
118 #  define g_malloc0_n(n_blocks,n_block_bytes)           _G_MALLOC_N (n_blocks, n_block_bytes, g_malloc0, g_malloc0_n)
119 #  define g_realloc_n(mem,n_blocks,n_block_bytes)       _G_REALLOC_N (mem, n_blocks, n_block_bytes, g_realloc, g_realloc_n)
120 #  define g_try_malloc_n(n_blocks,n_block_bytes)        _G_MALLOC_N (n_blocks, n_block_bytes, g_try_malloc, g_try_malloc_n)
121 #  define g_try_malloc0_n(n_blocks,n_block_bytes)       _G_MALLOC_N (n_blocks, n_block_bytes, g_try_malloc0, g_try_malloc0_n)
122 #  define g_try_realloc_n(mem,n_blocks,n_block_bytes)   _G_REALLOC_N (mem, n_blocks, n_block_bytes, g_try_realloc, g_try_realloc_n)
123 #endif
124
125
126 /* Convenience memory allocators
127  */
128
129 #define _G_NEW(struct_type, n_structs, _g_malloc_n)     \
130         ((struct_type *) _g_malloc_n ((n_structs), sizeof (struct_type)))
131 #define _G_RENEW(struct_type, mem, n_structs, _g_realloc_n)     \
132         ((struct_type *) _g_realloc_n ((mem), (n_structs), sizeof (struct_type)))
133
134 #define g_new(struct_type, n_structs)                   _G_NEW (struct_type, n_structs, g_malloc_n)
135 #define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, g_malloc0_n)
136 #define g_renew(struct_type, mem, n_structs)            _G_RENEW (struct_type, mem, n_structs, g_realloc_n)
137 #define g_try_new(struct_type, n_structs)               _G_NEW (struct_type, n_structs, g_try_malloc_n)
138 #define g_try_new0(struct_type, n_structs)              _G_NEW (struct_type, n_structs, g_try_malloc0_n)
139 #define g_try_renew(struct_type, mem, n_structs)        _G_RENEW (struct_type, mem, n_structs, g_try_realloc_n)
140
141
142 /* Memory allocation virtualization for debugging purposes
143  * g_mem_set_vtable() has to be the very first GLib function called
144  * if being used
145  */
146 struct _GMemVTable {
147   gpointer (*malloc)      (gsize    n_bytes);
148   gpointer (*realloc)     (gpointer mem,
149                            gsize    n_bytes);
150   void     (*free)        (gpointer mem);
151   /* optional; set to NULL if not used ! */
152   gpointer (*calloc)      (gsize    n_blocks,
153                            gsize    n_block_bytes);
154   gpointer (*try_malloc)  (gsize    n_bytes);
155   gpointer (*try_realloc) (gpointer mem,
156                            gsize    n_bytes);
157 };
158 void     g_mem_set_vtable (GMemVTable   *vtable);
159 gboolean g_mem_is_system_malloc (void);
160
161 GLIB_VAR gboolean g_mem_gc_friendly;
162
163 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
164  */
165 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
166 void    g_mem_profile   (void);
167
168
169 /* deprecated memchunks and allocators */
170 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
171 typedef struct _GAllocator GAllocator;
172 typedef struct _GMemChunk  GMemChunk;
173 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
174   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
175                    sizeof (type), \
176                    sizeof (type) * (pre_alloc), \
177                    (alloc_type)) \
178 )
179 #define g_chunk_new(type, chunk)        ( \
180   (type *) g_mem_chunk_alloc (chunk) \
181 )
182 #define g_chunk_new0(type, chunk)       ( \
183   (type *) g_mem_chunk_alloc0 (chunk) \
184 )
185 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
186   g_mem_chunk_free ((mem_chunk), (mem)); \
187 } G_STMT_END
188 #define G_ALLOC_ONLY      1
189 #define G_ALLOC_AND_FREE  2
190 GMemChunk* g_mem_chunk_new     (const gchar *name,
191                                 gint         atom_size,
192                                 gsize        area_size,
193                                 gint         type);
194 void       g_mem_chunk_destroy (GMemChunk   *mem_chunk);
195 gpointer   g_mem_chunk_alloc   (GMemChunk   *mem_chunk);
196 gpointer   g_mem_chunk_alloc0  (GMemChunk   *mem_chunk);
197 void       g_mem_chunk_free    (GMemChunk   *mem_chunk,
198                                 gpointer     mem);
199 void       g_mem_chunk_clean   (GMemChunk   *mem_chunk);
200 void       g_mem_chunk_reset   (GMemChunk   *mem_chunk);
201 void       g_mem_chunk_print   (GMemChunk   *mem_chunk);
202 void       g_mem_chunk_info    (void);
203 void       g_blow_chunks       (void);
204 GAllocator*g_allocator_new     (const gchar  *name,
205                                 guint         n_preallocs);
206 void       g_allocator_free    (GAllocator   *allocator);
207 #define G_ALLOCATOR_LIST       (1)
208 #define G_ALLOCATOR_SLIST      (2)
209 #define G_ALLOCATOR_NODE       (3)
210 #endif /* G_DISABLE_DEPRECATED */
211
212 G_END_DECLS
213
214 #endif /* __G_MEM_H__ */