Add g_try_new, g_try_new0, g_try_renew and g_try_malloc0. (#169611, Stefan
[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 _GAllocator GAllocator;
35 typedef struct _GMemChunk  GMemChunk;
36 typedef struct _GMemVTable GMemVTable;
37
38
39 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
40 #  define G_MEM_ALIGN   GLIB_SIZEOF_VOID_P
41 #else   /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
42 #  define G_MEM_ALIGN   GLIB_SIZEOF_LONG
43 #endif  /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
44
45
46 /* Memory allocation functions
47  */
48 gpointer g_malloc         (gulong        n_bytes) G_GNUC_MALLOC;
49 gpointer g_malloc0        (gulong        n_bytes) G_GNUC_MALLOC;
50 gpointer g_realloc        (gpointer      mem,
51                            gulong        n_bytes);
52 void     g_free           (gpointer      mem);
53 gpointer g_try_malloc     (gulong        n_bytes) G_GNUC_MALLOC;
54 gpointer g_try_malloc0    (gulong        n_bytes) G_GNUC_MALLOC;
55 gpointer g_try_realloc    (gpointer      mem,
56                            gulong        n_bytes);
57
58
59 /* Convenience memory allocators
60  */
61 #define g_new(struct_type, n_structs)           \
62     ((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
63 #define g_new0(struct_type, n_structs)          \
64     ((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
65 #define g_renew(struct_type, mem, n_structs)    \
66     ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
67
68 #define g_try_new(struct_type, n_structs)               \
69     ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
70 #define g_try_new0(struct_type, n_structs)              \
71     ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
72 #define g_try_renew(struct_type, mem, n_structs)        \
73     ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
74
75
76 /* Memory allocation virtualization for debugging purposes
77  * g_mem_set_vtable() has to be the very first GLib function called
78  * if being used
79  */
80 struct _GMemVTable
81 {
82   gpointer (*malloc)      (gsize    n_bytes);
83   gpointer (*realloc)     (gpointer mem,
84                            gsize    n_bytes);
85   void     (*free)        (gpointer mem);
86   /* optional; set to NULL if not used ! */
87   gpointer (*calloc)      (gsize    n_blocks,
88                            gsize    n_block_bytes);
89   gpointer (*try_malloc)  (gsize    n_bytes);
90   gpointer (*try_realloc) (gpointer mem,
91                            gsize    n_bytes);
92 };
93 void     g_mem_set_vtable (GMemVTable   *vtable);
94 gboolean g_mem_is_system_malloc (void);
95
96 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
97  */
98 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
99 void    g_mem_profile   (void);
100
101
102 /* Memchunk convenience functions
103  */
104 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
105   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
106                    sizeof (type), \
107                    sizeof (type) * (pre_alloc), \
108                    (alloc_type)) \
109 )
110 #define g_chunk_new(type, chunk)        ( \
111   (type *) g_mem_chunk_alloc (chunk) \
112 )
113 #define g_chunk_new0(type, chunk)       ( \
114   (type *) g_mem_chunk_alloc0 (chunk) \
115 )
116 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
117   g_mem_chunk_free ((mem_chunk), (mem)); \
118 } G_STMT_END
119
120
121 /* "g_mem_chunk_new" creates a new memory chunk.
122  * Memory chunks are used to allocate pieces of memory which are
123  *  always the same size. Lists are a good example of such a data type.
124  * The memory chunk allocates and frees blocks of memory as needed.
125  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
126  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
127  *  fault...somewhere).
128  *
129  * Oh yeah, GMemChunk is an opaque data type. (You don't really
130  *  want to know what's going on inside do you?)
131  */
132
133 /* ALLOC_ONLY MemChunks can only allocate memory. The free operation
134  *  is interpreted as a no op. ALLOC_ONLY MemChunks save 4 bytes per
135  *  atom. (They are also useful for lists which use MemChunk to allocate
136  *  memory but are also part of the MemChunk implementation).
137  * ALLOC_AND_FREE MemChunks can allocate and free memory.
138  */
139
140 #define G_ALLOC_ONLY      1
141 #define G_ALLOC_AND_FREE  2
142
143 GMemChunk* g_mem_chunk_new     (const gchar *name,
144                                 gint         atom_size,
145                                 gulong       area_size,
146                                 gint         type);
147 void       g_mem_chunk_destroy (GMemChunk   *mem_chunk);
148 gpointer   g_mem_chunk_alloc   (GMemChunk   *mem_chunk);
149 gpointer   g_mem_chunk_alloc0  (GMemChunk   *mem_chunk);
150 void       g_mem_chunk_free    (GMemChunk   *mem_chunk,
151                                 gpointer     mem);
152 void       g_mem_chunk_clean   (GMemChunk   *mem_chunk);
153 void       g_mem_chunk_reset   (GMemChunk   *mem_chunk);
154 void       g_mem_chunk_print   (GMemChunk   *mem_chunk);
155 void       g_mem_chunk_info    (void);
156
157 /* Ah yes...we have a "g_blow_chunks" function.
158  * "g_blow_chunks" simply compresses all the chunks. This operation
159  *  consists of freeing every memory area that should be freed (but
160  *  which we haven't gotten around to doing yet). And, no,
161  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
162  *  much better name than "g_mem_chunk_clean_all" or something
163  *  similar.
164  */
165 void       g_blow_chunks (void);
166
167
168 /* Generic allocators
169  */
170 GAllocator* g_allocator_new   (const gchar  *name,
171                                guint         n_preallocs);
172 void        g_allocator_free  (GAllocator   *allocator);
173
174 /* internal */
175 #define G_ALLOCATOR_LIST        (1)
176 #define G_ALLOCATOR_SLIST       (2)
177 #define G_ALLOCATOR_NODE        (3)
178
179
180 G_END_DECLS
181
182 #endif /* __G_MEM_H__ */