Fix gccisms (pointer arithmetic on void pointer, label without statement
[glib.git] / gmem.h
blob72e7b28773ebf7032894f9ce5de9e9702e55570a
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/.
27 #ifndef __G_MEM_H__
28 #define __G_MEM_H__
30 #include <gtypes.h>
32 /* optionally feature DMALLOC memory allocation debugger
34 #ifdef USE_DMALLOC
35 #include "dmalloc.h"
36 #endif
38 G_BEGIN_DECLS
40 typedef struct _GAllocator GAllocator;
41 typedef struct _GMemChunk GMemChunk;
43 /* Provide macros for easily allocating memory. The macros
44 * will cast the allocated memory to the specified type
45 * in order to avoid compiler warnings. (Makes the code neater).
48 #ifdef __DMALLOC_H__
49 # define g_new(type, count) (ALLOC (type, count))
50 # define g_new0(type, count) (CALLOC (type, count))
51 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
52 #else /* __DMALLOC_H__ */
53 # define g_new(type, count) \
54 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
55 # define g_new0(type, count) \
56 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
57 # define g_renew(type, mem, count) \
58 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
59 #endif /* __DMALLOC_H__ */
61 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
62 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
63 sizeof (type), \
64 sizeof (type) * (pre_alloc), \
65 (alloc_type)) \
67 #define g_chunk_new(type, chunk) ( \
68 (type *) g_mem_chunk_alloc (chunk) \
70 #define g_chunk_new0(type, chunk) ( \
71 (type *) g_mem_chunk_alloc0 (chunk) \
73 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
74 g_mem_chunk_free ((mem_chunk), (mem)); \
75 } G_STMT_END
77 /* Memory allocation and debugging
79 #ifdef USE_DMALLOC
81 #define g_malloc(size) ((gpointer) MALLOC (size))
82 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
83 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
84 #define g_free(mem) FREE (mem)
86 #else /* !USE_DMALLOC */
88 gpointer g_malloc (gulong size);
89 gpointer g_malloc0 (gulong size);
90 gpointer g_realloc (gpointer mem,
91 gulong size);
92 void g_free (gpointer mem);
94 #endif /* !USE_DMALLOC */
96 void g_mem_profile (void);
97 void g_mem_check (gpointer mem);
99 /* Generic allocators
101 GAllocator* g_allocator_new (const gchar *name,
102 guint n_preallocs);
103 void g_allocator_free (GAllocator *allocator);
105 #define G_ALLOCATOR_LIST (1)
106 #define G_ALLOCATOR_SLIST (2)
107 #define G_ALLOCATOR_NODE (3)
109 /* "g_mem_chunk_new" creates a new memory chunk.
110 * Memory chunks are used to allocate pieces of memory which are
111 * always the same size. Lists are a good example of such a data type.
112 * The memory chunk allocates and frees blocks of memory as needed.
113 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
114 * allocated in a mem chunk. ("g_free" will most likely cause a seg
115 * fault...somewhere).
117 * Oh yeah, GMemChunk is an opaque data type. (You don't really
118 * want to know what's going on inside do you?)
121 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
122 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
123 * atom. (They are also useful for lists which use MemChunk to allocate
124 * memory but are also part of the MemChunk implementation).
125 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
128 #define G_ALLOC_ONLY 1
129 #define G_ALLOC_AND_FREE 2
131 GMemChunk* g_mem_chunk_new (gchar *name,
132 gint atom_size,
133 gulong area_size,
134 gint type);
135 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
136 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
137 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
138 void g_mem_chunk_free (GMemChunk *mem_chunk,
139 gpointer mem);
140 void g_mem_chunk_clean (GMemChunk *mem_chunk);
141 void g_mem_chunk_reset (GMemChunk *mem_chunk);
142 void g_mem_chunk_print (GMemChunk *mem_chunk);
143 void g_mem_chunk_info (void);
145 /* Ah yes...we have a "g_blow_chunks" function.
146 * "g_blow_chunks" simply compresses all the chunks. This operation
147 * consists of freeing every memory area that should be freed (but
148 * which we haven't gotten around to doing yet). And, no,
149 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
150 * much better name than "g_mem_chunk_clean_all" or something
151 * similar.
153 void g_blow_chunks (void);
155 G_END_DECLS
157 #endif /* __G_MEM_H__ */