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/.
30 #include <glib/gslice.h>
31 #include <glib/gtypes.h>
35 typedef struct _GMemVTable GMemVTable;
38 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
39 # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
40 #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
41 # define G_MEM_ALIGN GLIB_SIZEOF_LONG
42 #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
45 /* Memory allocation functions
47 gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC;
48 gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC;
49 gpointer g_realloc (gpointer mem,
50 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
51 void g_free (gpointer mem);
52 gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC;
53 gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC;
54 gpointer g_try_realloc (gpointer mem,
55 gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
58 /* Convenience memory allocators
60 #define g_new(struct_type, n_structs) \
61 ((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
62 #define g_new0(struct_type, n_structs) \
63 ((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
64 #define g_renew(struct_type, mem, n_structs) \
65 ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
67 #define g_try_new(struct_type, n_structs) \
68 ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
69 #define g_try_new0(struct_type, n_structs) \
70 ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
71 #define g_try_renew(struct_type, mem, n_structs) \
72 ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
75 /* Memory allocation virtualization for debugging purposes
76 * g_mem_set_vtable() has to be the very first GLib function called
81 gpointer (*malloc) (gsize n_bytes);
82 gpointer (*realloc) (gpointer mem,
84 void (*free) (gpointer mem);
85 /* optional; set to NULL if not used ! */
86 gpointer (*calloc) (gsize n_blocks,
88 gpointer (*try_malloc) (gsize n_bytes);
89 gpointer (*try_realloc) (gpointer mem,
92 void g_mem_set_vtable (GMemVTable *vtable);
93 gboolean g_mem_is_system_malloc (void);
95 GLIB_VAR gboolean g_mem_gc_friendly;
97 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
99 GLIB_VAR GMemVTable *glib_mem_profiler_table;
100 void g_mem_profile (void);
103 /* deprecated memchunks and allocators */
104 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
105 typedef struct _GAllocator GAllocator;
106 typedef struct _GMemChunk GMemChunk;
107 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
108 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
110 sizeof (type) * (pre_alloc), \
113 #define g_chunk_new(type, chunk) ( \
114 (type *) g_mem_chunk_alloc (chunk) \
116 #define g_chunk_new0(type, chunk) ( \
117 (type *) g_mem_chunk_alloc0 (chunk) \
119 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
120 g_mem_chunk_free ((mem_chunk), (mem)); \
122 #define G_ALLOC_ONLY 1
123 #define G_ALLOC_AND_FREE 2
124 GMemChunk* g_mem_chunk_new (const gchar *name,
128 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
129 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
130 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
131 void g_mem_chunk_free (GMemChunk *mem_chunk,
133 void g_mem_chunk_clean (GMemChunk *mem_chunk);
134 void g_mem_chunk_reset (GMemChunk *mem_chunk);
135 void g_mem_chunk_print (GMemChunk *mem_chunk);
136 void g_mem_chunk_info (void);
137 void g_blow_chunks (void);
138 GAllocator*g_allocator_new (const gchar *name,
140 void g_allocator_free (GAllocator *allocator);
141 #define G_ALLOCATOR_LIST (1)
142 #define G_ALLOCATOR_SLIST (2)
143 #define G_ALLOCATOR_NODE (3)
144 #endif /* G_DISABLE_DEPRECATED */
148 #endif /* __G_MEM_H__ */