Call setlocale initially
[glib.git] / glib / gmem.h
blob2fef76603d9900be900508af1f16e49264dc3960
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
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
31 #ifndef __G_MEM_H__
32 #define __G_MEM_H__
34 #include <glib/gslice.h>
35 #include <glib/gtypes.h>
37 G_BEGIN_DECLS
39 typedef struct _GMemVTable GMemVTable;
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 */
49 /* Memory allocation functions
52 void g_free (gpointer mem);
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;
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;
79 /* Optimise: avoid the call to the (slower) _n function if we can
80 * determine at compile-time that no overflow happens.
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 gsize __s = sizeof (struct_type); \
87 gpointer __p; \
88 if (__s == 1) \
89 __p = g_##func (__n); \
90 else if (__builtin_constant_p (__n) && \
91 __n <= G_MAXSIZE / __s) \
92 __p = g_##func (__n * __s); \
93 else \
94 __p = g_##func##_n (__n, __s); \
95 __p; \
96 }))
97 # define _G_RENEW(struct_type, mem, n_structs, func) \
98 (struct_type *) (__extension__ ({ \
99 gsize __n = (gsize) (n_structs); \
100 gsize __s = sizeof (struct_type); \
101 gpointer __p = (gpointer) (mem); \
102 if (__s == 1) \
103 __p = g_##func (__p, __n); \
104 else if (__builtin_constant_p (__n) && \
105 __n <= G_MAXSIZE / __s) \
106 __p = g_##func (__p, __n * __s); \
107 else \
108 __p = g_##func##_n (__p, __n, __s); \
109 __p; \
112 #else
114 /* Unoptimised version: always call the _n() function. */
116 #define _G_NEW(struct_type, n_structs, func) \
117 ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
118 #define _G_RENEW(struct_type, mem, n_structs, func) \
119 ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
121 #endif
123 #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
124 #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
125 #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
126 #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
127 #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
128 #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
131 /* Memory allocation virtualization for debugging purposes
132 * g_mem_set_vtable() has to be the very first GLib function called
133 * if being used
135 struct _GMemVTable {
136 gpointer (*malloc) (gsize n_bytes);
137 gpointer (*realloc) (gpointer mem,
138 gsize n_bytes);
139 void (*free) (gpointer mem);
140 /* optional; set to NULL if not used ! */
141 gpointer (*calloc) (gsize n_blocks,
142 gsize n_block_bytes);
143 gpointer (*try_malloc) (gsize n_bytes);
144 gpointer (*try_realloc) (gpointer mem,
145 gsize n_bytes);
147 void g_mem_set_vtable (GMemVTable *vtable);
148 gboolean g_mem_is_system_malloc (void);
150 GLIB_VAR gboolean g_mem_gc_friendly;
152 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
154 GLIB_VAR GMemVTable *glib_mem_profiler_table;
155 void g_mem_profile (void);
158 /* deprecated memchunks and allocators */
159 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
160 typedef struct _GAllocator GAllocator;
161 typedef struct _GMemChunk GMemChunk;
162 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
163 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
164 sizeof (type), \
165 sizeof (type) * (pre_alloc), \
166 (alloc_type)) \
168 #define g_chunk_new(type, chunk) ( \
169 (type *) g_mem_chunk_alloc (chunk) \
171 #define g_chunk_new0(type, chunk) ( \
172 (type *) g_mem_chunk_alloc0 (chunk) \
174 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
175 g_mem_chunk_free ((mem_chunk), (mem)); \
176 } G_STMT_END
177 #define G_ALLOC_ONLY 1
178 #define G_ALLOC_AND_FREE 2
179 GMemChunk* g_mem_chunk_new (const gchar *name,
180 gint atom_size,
181 gsize area_size,
182 gint type);
183 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
184 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
185 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
186 void g_mem_chunk_free (GMemChunk *mem_chunk,
187 gpointer mem);
188 void g_mem_chunk_clean (GMemChunk *mem_chunk);
189 void g_mem_chunk_reset (GMemChunk *mem_chunk);
190 void g_mem_chunk_print (GMemChunk *mem_chunk);
191 void g_mem_chunk_info (void);
192 void g_blow_chunks (void);
193 GAllocator*g_allocator_new (const gchar *name,
194 guint n_preallocs);
195 void g_allocator_free (GAllocator *allocator);
196 #define G_ALLOCATOR_LIST (1)
197 #define G_ALLOCATOR_SLIST (2)
198 #define G_ALLOCATOR_NODE (3)
199 #endif /* G_DISABLE_DEPRECATED */
201 G_END_DECLS
203 #endif /* __G_MEM_H__ */