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/gtypes.h>
34 typedef struct _GMemVTable GMemVTable;
37 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
38 # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
39 #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
40 # define G_MEM_ALIGN GLIB_SIZEOF_LONG
41 #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
44 /* Memory allocation functions
46 gpointer g_malloc (gulong n_bytes) G_GNUC_MALLOC;
47 gpointer g_malloc0 (gulong n_bytes) G_GNUC_MALLOC;
48 gpointer g_realloc (gpointer mem,
50 void g_free (gpointer mem);
51 gpointer g_try_malloc (gulong n_bytes) G_GNUC_MALLOC;
52 gpointer g_try_malloc0 (gulong n_bytes) G_GNUC_MALLOC;
53 gpointer g_try_realloc (gpointer mem,
57 /* Convenience memory allocators
59 #define g_new(struct_type, n_structs) \
60 ((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
61 #define g_new0(struct_type, n_structs) \
62 ((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
63 #define g_renew(struct_type, mem, n_structs) \
64 ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
66 #define g_try_new(struct_type, n_structs) \
67 ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
68 #define g_try_new0(struct_type, n_structs) \
69 ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
70 #define g_try_renew(struct_type, mem, n_structs) \
71 ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
74 /* Memory allocation virtualization for debugging purposes
75 * g_mem_set_vtable() has to be the very first GLib function called
80 gpointer (*malloc) (gsize n_bytes);
81 gpointer (*realloc) (gpointer mem,
83 void (*free) (gpointer mem);
84 /* optional; set to NULL if not used ! */
85 gpointer (*calloc) (gsize n_blocks,
87 gpointer (*try_malloc) (gsize n_bytes);
88 gpointer (*try_realloc) (gpointer mem,
91 void g_mem_set_vtable (GMemVTable *vtable);
92 gboolean g_mem_is_system_malloc (void);
94 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
96 GLIB_VAR GMemVTable *glib_mem_profiler_table;
97 void g_mem_profile (void);
99 /* slices - fast allocation/release of small memory blocks
101 gpointer g_slice_alloc (guint block_size);
102 gpointer g_slice_alloc0 (guint block_size);
103 void g_slice_free1 (guint block_size,
105 void g_slice_free_chain (guint block_size,
108 #define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type)))
109 #define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type)))
110 #define g_slice_free(type,mem) g_slice_free1 (sizeof (type), mem)
113 /* deprecated memchunks and allocators */
114 #if !defined G_DISABLE_DEPRECATED || 1
115 typedef struct _GAllocator GAllocator;
116 typedef struct _GMemChunk GMemChunk;
117 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
118 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
120 sizeof (type) * (pre_alloc), \
123 #define g_chunk_new(type, chunk) ( \
124 (type *) g_mem_chunk_alloc (chunk) \
126 #define g_chunk_new0(type, chunk) ( \
127 (type *) g_mem_chunk_alloc0 (chunk) \
129 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
130 g_mem_chunk_free ((mem_chunk), (mem)); \
132 #define G_ALLOC_ONLY 1
133 #define G_ALLOC_AND_FREE 2
134 GMemChunk* g_mem_chunk_new (const gchar *name,
138 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
139 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
140 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
141 void g_mem_chunk_free (GMemChunk *mem_chunk,
143 void g_mem_chunk_clean (GMemChunk *mem_chunk);
144 void g_mem_chunk_reset (GMemChunk *mem_chunk);
145 void g_mem_chunk_print (GMemChunk *mem_chunk);
146 void g_mem_chunk_info (void);
147 void g_blow_chunks (void);
148 GAllocator*g_allocator_new (const gchar *name,
150 void g_allocator_free (GAllocator *allocator);
151 #define G_ALLOCATOR_LIST (1)
152 #define G_ALLOCATOR_SLIST (2)
153 #define G_ALLOCATOR_NODE (3)
154 #endif /* G_DISABLE_DEPRECATED */
158 #endif /* __G_MEM_H__ */