gmem.h revision d3388eb4e8e0ae28985c59e88aa676665e651953
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
33G_BEGIN_DECLS
34
35typedef 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 */
47gpointer g_malloc         (gulong	 n_bytes) G_GNUC_MALLOC;
48gpointer g_malloc0        (gulong	 n_bytes) G_GNUC_MALLOC;
49gpointer g_realloc        (gpointer	 mem,
50			   gulong	 n_bytes) G_GNUC_WARN_UNUSED_RESULT;
51void	 g_free	          (gpointer	 mem);
52gpointer g_try_malloc     (gulong	 n_bytes) G_GNUC_MALLOC;
53gpointer g_try_malloc0    (gulong	 n_bytes) G_GNUC_MALLOC;
54gpointer g_try_realloc    (gpointer	 mem,
55			   gulong	 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 */
79struct _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};
92void	 g_mem_set_vtable (GMemVTable	*vtable);
93gboolean g_mem_is_system_malloc (void);
94
95/* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
96 */
97GLIB_VAR GMemVTable	*glib_mem_profiler_table;
98void	g_mem_profile	(void);
99
100
101/* deprecated memchunks and allocators */
102#if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
103typedef struct _GAllocator GAllocator;
104typedef struct _GMemChunk  GMemChunk;
105#define g_mem_chunk_create(type, pre_alloc, alloc_type)	( \
106  g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
107		   sizeof (type), \
108		   sizeof (type) * (pre_alloc), \
109		   (alloc_type)) \
110)
111#define g_chunk_new(type, chunk)	( \
112  (type *) g_mem_chunk_alloc (chunk) \
113)
114#define g_chunk_new0(type, chunk)	( \
115  (type *) g_mem_chunk_alloc0 (chunk) \
116)
117#define g_chunk_free(mem, mem_chunk)	G_STMT_START { \
118  g_mem_chunk_free ((mem_chunk), (mem)); \
119} G_STMT_END
120#define G_ALLOC_ONLY	  1
121#define G_ALLOC_AND_FREE  2
122GMemChunk* g_mem_chunk_new     (const gchar *name,
123				gint         atom_size,
124				gulong       area_size,
125				gint         type);
126void       g_mem_chunk_destroy (GMemChunk   *mem_chunk);
127gpointer   g_mem_chunk_alloc   (GMemChunk   *mem_chunk);
128gpointer   g_mem_chunk_alloc0  (GMemChunk   *mem_chunk);
129void       g_mem_chunk_free    (GMemChunk   *mem_chunk,
130				gpointer     mem);
131void       g_mem_chunk_clean   (GMemChunk   *mem_chunk);
132void       g_mem_chunk_reset   (GMemChunk   *mem_chunk);
133void       g_mem_chunk_print   (GMemChunk   *mem_chunk);
134void       g_mem_chunk_info    (void);
135void	   g_blow_chunks       (void);
136GAllocator*g_allocator_new     (const gchar  *name,
137				guint         n_preallocs);
138void       g_allocator_free    (GAllocator   *allocator);
139#define	G_ALLOCATOR_LIST       (1)
140#define	G_ALLOCATOR_SLIST      (2)
141#define	G_ALLOCATOR_NODE       (3)
142#endif /* G_DISABLE_DEPRECATED */
143
144G_END_DECLS
145
146#endif /* __G_MEM_H__ */
147