69a0befcf78c258c9e52f6eccd28a91c109cb3cc
[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 #ifndef __G_MEM_H__
28 #define __G_MEM_H__
29
30 #include <glib/gslice.h>
31 #include <glib/gtypes.h>
32
33 G_BEGIN_DECLS
34
35 typedef struct _GMemVTable GMemVTable;
36
37
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 */
43
44
45 /* Memory allocation functions
46  */
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;
56
57
58 /* Convenience memory allocators
59  */
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))))
66
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))))
73
74
75 /* Memory allocation virtualization for debugging purposes
76  * g_mem_set_vtable() has to be the very first GLib function called
77  * if being used
78  */
79 struct _GMemVTable
80 {
81   gpointer (*malloc)      (gsize    n_bytes);
82   gpointer (*realloc)     (gpointer mem,
83                            gsize    n_bytes);
84   void     (*free)        (gpointer mem);
85   /* optional; set to NULL if not used ! */
86   gpointer (*calloc)      (gsize    n_blocks,
87                            gsize    n_block_bytes);
88   gpointer (*try_malloc)  (gsize    n_bytes);
89   gpointer (*try_realloc) (gpointer mem,
90                            gsize    n_bytes);
91 };
92 void     g_mem_set_vtable (GMemVTable   *vtable);
93 gboolean g_mem_is_system_malloc (void);
94
95 GLIB_VAR gboolean g_mem_gc_friendly;
96
97 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
98  */
99 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
100 void    g_mem_profile   (void);
101
102
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 ")", \
109                    sizeof (type), \
110                    sizeof (type) * (pre_alloc), \
111                    (alloc_type)) \
112 )
113 #define g_chunk_new(type, chunk)        ( \
114   (type *) g_mem_chunk_alloc (chunk) \
115 )
116 #define g_chunk_new0(type, chunk)       ( \
117   (type *) g_mem_chunk_alloc0 (chunk) \
118 )
119 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
120   g_mem_chunk_free ((mem_chunk), (mem)); \
121 } G_STMT_END
122 #define G_ALLOC_ONLY      1
123 #define G_ALLOC_AND_FREE  2
124 GMemChunk* g_mem_chunk_new     (const gchar *name,
125                                 gint         atom_size,
126                                 gsize        area_size,
127                                 gint         type);
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,
132                                 gpointer     mem);
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,
139                                 guint         n_preallocs);
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 */
145
146 G_END_DECLS
147
148 #endif /* __G_MEM_H__ */