6e1773ed2a7d63b7799e33c4012d9bc8603ca6d5
[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/gtypes.h>
31
32 G_BEGIN_DECLS
33
34 typedef struct _GMemVTable GMemVTable;
35
36
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 */
42
43
44 /* Memory allocation functions
45  */
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,
49                            gulong        n_bytes);
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,
54                            gulong        n_bytes);
55
56
57 /* Convenience memory allocators
58  */
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))))
65
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))))
72
73
74 /* Memory allocation virtualization for debugging purposes
75  * g_mem_set_vtable() has to be the very first GLib function called
76  * if being used
77  */
78 struct _GMemVTable
79 {
80   gpointer (*malloc)      (gsize    n_bytes);
81   gpointer (*realloc)     (gpointer mem,
82                            gsize    n_bytes);
83   void     (*free)        (gpointer mem);
84   /* optional; set to NULL if not used ! */
85   gpointer (*calloc)      (gsize    n_blocks,
86                            gsize    n_block_bytes);
87   gpointer (*try_malloc)  (gsize    n_bytes);
88   gpointer (*try_realloc) (gpointer mem,
89                            gsize    n_bytes);
90 };
91 void     g_mem_set_vtable (GMemVTable   *vtable);
92 gboolean g_mem_is_system_malloc (void);
93
94 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
95  */
96 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
97 void    g_mem_profile   (void);
98
99 /* slices - fast allocation/release of small memory blocks
100  */
101 gpointer g_slice_alloc          (guint    block_size);
102 gpointer g_slice_alloc0         (guint    block_size);
103 void     g_slice_free1          (guint    block_size,
104                                  gpointer mem_block);
105 void     g_slice_free_chain     (guint    block_size,
106                                  gpointer mem_chain,
107                                  guint    next_offset);
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)
111
112
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 ")", \
119                    sizeof (type), \
120                    sizeof (type) * (pre_alloc), \
121                    (alloc_type)) \
122 )
123 #define g_chunk_new(type, chunk)        ( \
124   (type *) g_mem_chunk_alloc (chunk) \
125 )
126 #define g_chunk_new0(type, chunk)       ( \
127   (type *) g_mem_chunk_alloc0 (chunk) \
128 )
129 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
130   g_mem_chunk_free ((mem_chunk), (mem)); \
131 } G_STMT_END
132 #define G_ALLOC_ONLY      1
133 #define G_ALLOC_AND_FREE  2
134 GMemChunk* g_mem_chunk_new     (const gchar *name,
135                                 gint         atom_size,
136                                 gulong       area_size,
137                                 gint         type);
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,
142                                 gpointer     mem);
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,
149                                 guint         n_preallocs);
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 */
155
156 G_END_DECLS
157
158 #endif /* __G_MEM_H__ */