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 /**
40  * GMemVTable:
41  * @malloc: function to use for allocating memory.
42  * @realloc: function to use for reallocating memory.
43  * @free: function to use to free memory.
44  * @calloc: function to use for allocating zero-filled memory.
45  * @try_malloc: function to use for allocating memory without a default error handler.
46  * @try_realloc: function to use for reallocating memory without a default error handler.
47  *
48  * A set of functions used to perform memory allocation. The same #GMemVTable must
49  * be used for all allocations in the same program; a call to g_mem_set_vtable(),
50  * if it exists, should be prior to any use of GLib.
51  */
52 typedef struct _GMemVTable GMemVTable;
53 
54 
55 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
56 /**
57  * G_MEM_ALIGN:
58  *
59  * Indicates the number of bytes to which memory will be aligned on the
60  * current platform.
61  */
62 #  define G_MEM_ALIGN	GLIB_SIZEOF_VOID_P
63 #else	/* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
64 #  define G_MEM_ALIGN	GLIB_SIZEOF_LONG
65 #endif	/* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
66 
67 
68 /* Memory allocation functions
69  */
70 
71 void	 g_free	          (gpointer	 mem);
72 
73 gpointer g_malloc         (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
74 gpointer g_malloc0        (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
75 gpointer g_realloc        (gpointer	 mem,
76 			   gsize	 n_bytes) G_GNUC_WARN_UNUSED_RESULT;
77 gpointer g_try_malloc     (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
78 gpointer g_try_malloc0    (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
79 gpointer g_try_realloc    (gpointer	 mem,
80 			   gsize	 n_bytes) G_GNUC_WARN_UNUSED_RESULT;
81 
82 gpointer g_malloc_n       (gsize	 n_blocks,
83 			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
84 gpointer g_malloc0_n      (gsize	 n_blocks,
85 			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
86 gpointer g_realloc_n      (gpointer	 mem,
87 			   gsize	 n_blocks,
88 			   gsize	 n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
89 gpointer g_try_malloc_n   (gsize	 n_blocks,
90 			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
91 gpointer g_try_malloc0_n  (gsize	 n_blocks,
92 			   gsize	 n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
93 gpointer g_try_realloc_n  (gpointer	 mem,
94 			   gsize	 n_blocks,
95 			   gsize	 n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
96 
97 
98 /* Optimise: avoid the call to the (slower) _n function if we can
99  * determine at compile-time that no overflow happens.
100  */
101 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
102 #  define _G_NEW(struct_type, n_structs, func) \
103 	(struct_type *) (G_GNUC_EXTENSION ({			\
104 	  gsize __n = (gsize) (n_structs);			\
105 	  gsize __s = sizeof (struct_type);			\
106 	  gpointer __p;						\
107 	  if (__s == 1)						\
108 	    __p = g_##func (__n);				\
109 	  else if (__builtin_constant_p (__n) &&		\
110 	           (__s == 0 || __n <= G_MAXSIZE / __s))	\
111 	    __p = g_##func (__n * __s);				\
112 	  else							\
113 	    __p = g_##func##_n (__n, __s);			\
114 	  __p;							\
115 	}))
116 #  define _G_RENEW(struct_type, mem, n_structs, func) \
117 	(struct_type *) (G_GNUC_EXTENSION ({			\
118 	  gsize __n = (gsize) (n_structs);			\
119 	  gsize __s = sizeof (struct_type);			\
120 	  gpointer __p = (gpointer) (mem);			\
121 	  if (__s == 1)						\
122 	    __p = g_##func (__p, __n);				\
123 	  else if (__builtin_constant_p (__n) &&		\
124 	           (__s == 0 || __n <= G_MAXSIZE / __s))	\
125 	    __p = g_##func (__p, __n * __s);			\
126 	  else							\
127 	    __p = g_##func##_n (__p, __n, __s);			\
128 	  __p;							\
129 	}))
130 
131 #else
132 
133 /* Unoptimised version: always call the _n() function. */
134 
135 #define _G_NEW(struct_type, n_structs, func) \
136         ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
137 #define _G_RENEW(struct_type, mem, n_structs, func) \
138         ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
139 
140 #endif
141 
142 /**
143  * g_new:
144  * @struct_type: the type of the elements to allocate
145  * @n_structs: the number of elements to allocate
146  *
147  * Allocates @n_structs elements of type @struct_type.
148  * The returned pointer is cast to a pointer to the given type.
149  * If @n_structs is 0 it returns %NULL.
150  * Care is taken to avoid overflow when calculating the size of the allocated block.
151  *
152  * Since the returned pointer is already casted to the right type,
153  * it is normally unnecessary to cast it explicitly, and doing
154  * so might hide memory allocation errors.
155  *
156  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
157  */
158 #define g_new(struct_type, n_structs)			_G_NEW (struct_type, n_structs, malloc)
159 /**
160  * g_new0:
161  * @struct_type: the type of the elements to allocate.
162  * @n_structs: the number of elements to allocate.
163  *
164  * Allocates @n_structs elements of type @struct_type, initialized to 0's.
165  * The returned pointer is cast to a pointer to the given type.
166  * If @n_structs is 0 it returns %NULL.
167  * Care is taken to avoid overflow when calculating the size of the allocated block.
168  *
169  * Since the returned pointer is already casted to the right type,
170  * it is normally unnecessary to cast it explicitly, and doing
171  * so might hide memory allocation errors.
172  *
173  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
174  */
175 #define g_new0(struct_type, n_structs)			_G_NEW (struct_type, n_structs, malloc0)
176 /**
177  * g_renew:
178  * @struct_type: the type of the elements to allocate
179  * @mem: the currently allocated memory
180  * @n_structs: the number of elements to allocate
181  *
182  * Reallocates the memory pointed to by @mem, so that it now has space for
183  * @n_structs elements of type @struct_type. It returns the new address of
184  * the memory, which may have been moved.
185  * Care is taken to avoid overflow when calculating the size of the allocated block.
186  *
187  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
188  */
189 #define g_renew(struct_type, mem, n_structs)		_G_RENEW (struct_type, mem, n_structs, realloc)
190 /**
191  * g_try_new:
192  * @struct_type: the type of the elements to allocate
193  * @n_structs: the number of elements to allocate
194  *
195  * Attempts to allocate @n_structs elements of type @struct_type, and returns
196  * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
197  * The returned pointer is cast to a pointer to the given type.
198  * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
199  *
200  * Since: 2.8
201  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
202  */
203 #define g_try_new(struct_type, n_structs)		_G_NEW (struct_type, n_structs, try_malloc)
204 /**
205  * g_try_new0:
206  * @struct_type: the type of the elements to allocate
207  * @n_structs: the number of elements to allocate
208  *
209  * Attempts to allocate @n_structs elements of type @struct_type, initialized
210  * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
211  * the program on failure.
212  * The returned pointer is cast to a pointer to the given type.
213  * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
214  *
215  * Since: 2.8
216  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
217  */
218 #define g_try_new0(struct_type, n_structs)		_G_NEW (struct_type, n_structs, try_malloc0)
219 /**
220  * g_try_renew:
221  * @struct_type: the type of the elements to allocate
222  * @mem: the currently allocated memory
223  * @n_structs: the number of elements to allocate
224  *
225  * Attempts to reallocate the memory pointed to by @mem, so that it now has
226  * space for @n_structs elements of type @struct_type, and returns %NULL on
227  * failure. Contrast with g_renew(), which aborts the program on failure.
228  * It returns the new address of the memory, which may have been moved.
229  * The function returns %NULL if an overflow occurs.
230  *
231  * Since: 2.8
232  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
233  */
234 #define g_try_renew(struct_type, mem, n_structs)	_G_RENEW (struct_type, mem, n_structs, try_realloc)
235 
236 
237 /* Memory allocation virtualization for debugging purposes
238  * g_mem_set_vtable() has to be the very first GLib function called
239  * if being used
240  */
241 struct _GMemVTable {
242   gpointer (*malloc)      (gsize    n_bytes);
243   gpointer (*realloc)     (gpointer mem,
244 			   gsize    n_bytes);
245   void     (*free)        (gpointer mem);
246   /* optional; set to NULL if not used ! */
247   gpointer (*calloc)      (gsize    n_blocks,
248 			   gsize    n_block_bytes);
249   gpointer (*try_malloc)  (gsize    n_bytes);
250   gpointer (*try_realloc) (gpointer mem,
251 			   gsize    n_bytes);
252 };
253 void	 g_mem_set_vtable (GMemVTable	*vtable);
254 gboolean g_mem_is_system_malloc (void);
255 
256 GLIB_VAR gboolean g_mem_gc_friendly;
257 
258 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
259  */
260 GLIB_VAR GMemVTable	*glib_mem_profiler_table;
261 void	g_mem_profile	(void);
262 
263 
264 /* deprecated memchunks and allocators */
265 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
266 typedef struct _GAllocator GAllocator;
267 typedef struct _GMemChunk  GMemChunk;
268 #define g_mem_chunk_create(type, pre_alloc, alloc_type)	( \
269   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
270 		   sizeof (type), \
271 		   sizeof (type) * (pre_alloc), \
272 		   (alloc_type)) \
273 )
274 #define g_chunk_new(type, chunk)	( \
275   (type *) g_mem_chunk_alloc (chunk) \
276 )
277 #define g_chunk_new0(type, chunk)	( \
278   (type *) g_mem_chunk_alloc0 (chunk) \
279 )
280 #define g_chunk_free(mem, mem_chunk)	G_STMT_START { \
281   g_mem_chunk_free ((mem_chunk), (mem)); \
282 } G_STMT_END
283 #define G_ALLOC_ONLY	  1
284 #define G_ALLOC_AND_FREE  2
285 GMemChunk* g_mem_chunk_new     (const gchar *name,
286 				gint         atom_size,
287 				gsize        area_size,
288 				gint         type);
289 void       g_mem_chunk_destroy (GMemChunk   *mem_chunk);
290 gpointer   g_mem_chunk_alloc   (GMemChunk   *mem_chunk);
291 gpointer   g_mem_chunk_alloc0  (GMemChunk   *mem_chunk);
292 void       g_mem_chunk_free    (GMemChunk   *mem_chunk,
293 				gpointer     mem);
294 void       g_mem_chunk_clean   (GMemChunk   *mem_chunk);
295 void       g_mem_chunk_reset   (GMemChunk   *mem_chunk);
296 void       g_mem_chunk_print   (GMemChunk   *mem_chunk);
297 void       g_mem_chunk_info    (void);
298 void	   g_blow_chunks       (void);
299 GAllocator*g_allocator_new     (const gchar  *name,
300 				guint         n_preallocs);
301 void       g_allocator_free    (GAllocator   *allocator);
302 #define	G_ALLOCATOR_LIST       (1)
303 #define	G_ALLOCATOR_SLIST      (2)
304 #define	G_ALLOCATOR_NODE       (3)
305 #endif /* G_DISABLE_DEPRECATED */
306 
307 G_END_DECLS
308 
309 #endif /* __G_MEM_H__ */
310