• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30 
31 #ifndef __G_MEM_H__
32 #define __G_MEM_H__
33 
34 #include <glib/gslice.h>
35 #include <glib/gtypes.h>
36 
37 G_BEGIN_DECLS
38 
39 typedef struct _GMemVTable GMemVTable;
40 
41 
42 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
43 #  define G_MEM_ALIGN	GLIB_SIZEOF_VOID_P
44 #else	/* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
45 #  define G_MEM_ALIGN	GLIB_SIZEOF_LONG
46 #endif	/* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
47 
48 
49 /* Memory allocation functions
50  */
51 gpointer g_malloc         (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
52 gpointer g_malloc0        (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
53 gpointer g_realloc        (gpointer	 mem,
54 			   gsize	 n_bytes) G_GNUC_WARN_UNUSED_RESULT;
55 void	 g_free	          (gpointer	 mem);
56 gpointer g_try_malloc     (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
57 gpointer g_try_malloc0    (gsize	 n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
58 gpointer g_try_realloc    (gpointer	 mem,
59 			   gsize	 n_bytes) G_GNUC_WARN_UNUSED_RESULT;
60 
61 
62 /* Convenience memory allocators
63  */
64 #define g_new(struct_type, n_structs)		\
65     ((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
66 #define g_new0(struct_type, n_structs)		\
67     ((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
68 #define g_renew(struct_type, mem, n_structs)	\
69     ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
70 
71 #define g_try_new(struct_type, n_structs)		\
72     ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
73 #define g_try_new0(struct_type, n_structs)		\
74     ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
75 #define g_try_renew(struct_type, mem, n_structs)	\
76     ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
77 
78 
79 /* Memory allocation virtualization for debugging purposes
80  * g_mem_set_vtable() has to be the very first GLib function called
81  * if being used
82  */
83 struct _GMemVTable
84 {
85   gpointer (*malloc)      (gsize    n_bytes);
86   gpointer (*realloc)     (gpointer mem,
87 			   gsize    n_bytes);
88   void     (*free)        (gpointer mem);
89   /* optional; set to NULL if not used ! */
90   gpointer (*calloc)      (gsize    n_blocks,
91 			   gsize    n_block_bytes);
92   gpointer (*try_malloc)  (gsize    n_bytes);
93   gpointer (*try_realloc) (gpointer mem,
94 			   gsize    n_bytes);
95 };
96 void	 g_mem_set_vtable (GMemVTable	*vtable);
97 gboolean g_mem_is_system_malloc (void);
98 
99 GLIB_VAR gboolean g_mem_gc_friendly;
100 
101 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
102  */
103 GLIB_VAR GMemVTable	*glib_mem_profiler_table;
104 void	g_mem_profile	(void);
105 
106 
107 /* deprecated memchunks and allocators */
108 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
109 typedef struct _GAllocator GAllocator;
110 typedef struct _GMemChunk  GMemChunk;
111 #define g_mem_chunk_create(type, pre_alloc, alloc_type)	( \
112   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
113 		   sizeof (type), \
114 		   sizeof (type) * (pre_alloc), \
115 		   (alloc_type)) \
116 )
117 #define g_chunk_new(type, chunk)	( \
118   (type *) g_mem_chunk_alloc (chunk) \
119 )
120 #define g_chunk_new0(type, chunk)	( \
121   (type *) g_mem_chunk_alloc0 (chunk) \
122 )
123 #define g_chunk_free(mem, mem_chunk)	G_STMT_START { \
124   g_mem_chunk_free ((mem_chunk), (mem)); \
125 } G_STMT_END
126 #define G_ALLOC_ONLY	  1
127 #define G_ALLOC_AND_FREE  2
128 GMemChunk* g_mem_chunk_new     (const gchar *name,
129 				gint         atom_size,
130 				gsize        area_size,
131 				gint         type);
132 void       g_mem_chunk_destroy (GMemChunk   *mem_chunk);
133 gpointer   g_mem_chunk_alloc   (GMemChunk   *mem_chunk);
134 gpointer   g_mem_chunk_alloc0  (GMemChunk   *mem_chunk);
135 void       g_mem_chunk_free    (GMemChunk   *mem_chunk,
136 				gpointer     mem);
137 void       g_mem_chunk_clean   (GMemChunk   *mem_chunk);
138 void       g_mem_chunk_reset   (GMemChunk   *mem_chunk);
139 void       g_mem_chunk_print   (GMemChunk   *mem_chunk);
140 void       g_mem_chunk_info    (void);
141 void	   g_blow_chunks       (void);
142 GAllocator*g_allocator_new     (const gchar  *name,
143 				guint         n_preallocs);
144 void       g_allocator_free    (GAllocator   *allocator);
145 #define	G_ALLOCATOR_LIST       (1)
146 #define	G_ALLOCATOR_SLIST      (2)
147 #define	G_ALLOCATOR_NODE       (3)
148 #endif /* G_DISABLE_DEPRECATED */
149 
150 G_END_DECLS
151 
152 #endif /* __G_MEM_H__ */
153