Navit SVN

/work/compile/navit/src/navit/support/glib/gmem.h

00001 /* GLIB - Library of useful routines for C programming
00002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 /*
00021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00022  * file for a list of people on the GLib Team.  See the ChangeLog
00023  * files for a list of changes.  These files are distributed with
00024  * GLib at ftp://ftp.gtk.org/pub/gtk/.
00025  */
00026 
00027 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
00028 #error "Only <glib.h> can be included directly."
00029 #endif
00030 
00031 #ifndef __G_MEM_H__
00032 #define __G_MEM_H__
00033 
00034 #include <glib/gslice.h>
00035 #include <glib/gtypes.h>
00036 
00037 G_BEGIN_DECLS
00038 
00039 typedef struct _GMemVTable GMemVTable;
00040 
00041 
00042 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
00043 #  define G_MEM_ALIGN   GLIB_SIZEOF_VOID_P
00044 #else   /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
00045 #  define G_MEM_ALIGN   GLIB_SIZEOF_LONG
00046 #endif  /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
00047 
00048 
00049 /* Memory allocation functions
00050  */
00051 gpointer g_malloc         (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
00052 gpointer g_malloc0        (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
00053 gpointer g_realloc        (gpointer      mem,
00054                            gsize         n_bytes) G_GNUC_WARN_UNUSED_RESULT;
00055 void     g_free           (gpointer      mem);
00056 gpointer g_try_malloc     (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
00057 gpointer g_try_malloc0    (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
00058 gpointer g_try_realloc    (gpointer      mem,
00059                            gsize         n_bytes) G_GNUC_WARN_UNUSED_RESULT;
00060 
00061 
00062 /* Convenience memory allocators
00063  */
00064 #define g_new(struct_type, n_structs)           \
00065     ((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
00066 #define g_new0(struct_type, n_structs)          \
00067     ((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
00068 #define g_renew(struct_type, mem, n_structs)    \
00069     ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
00070 
00071 #define g_try_new(struct_type, n_structs)               \
00072     ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
00073 #define g_try_new0(struct_type, n_structs)              \
00074     ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
00075 #define g_try_renew(struct_type, mem, n_structs)        \
00076     ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
00077 
00078 
00079 /* Memory allocation virtualization for debugging purposes
00080  * g_mem_set_vtable() has to be the very first GLib function called
00081  * if being used
00082  */
00083 struct _GMemVTable
00084 {
00085   gpointer (*malloc)      (gsize    n_bytes);
00086   gpointer (*realloc)     (gpointer mem,
00087                            gsize    n_bytes);
00088   void     (*free)        (gpointer mem);
00089   /* optional; set to NULL if not used ! */
00090   gpointer (*calloc)      (gsize    n_blocks,
00091                            gsize    n_block_bytes);
00092   gpointer (*try_malloc)  (gsize    n_bytes);
00093   gpointer (*try_realloc) (gpointer mem,
00094                            gsize    n_bytes);
00095 };
00096 void     g_mem_set_vtable (GMemVTable   *vtable);
00097 gboolean g_mem_is_system_malloc (void);
00098 
00099 GLIB_VAR gboolean g_mem_gc_friendly;
00100 
00101 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
00102  */
00103 GLIB_VAR GMemVTable     *glib_mem_profiler_table;
00104 void    g_mem_profile   (void);
00105 
00106 
00107 /* deprecated memchunks and allocators */
00108 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
00109 typedef struct _GAllocator GAllocator;
00110 typedef struct _GMemChunk  GMemChunk;
00111 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
00112   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
00113                    sizeof (type), \
00114                    sizeof (type) * (pre_alloc), \
00115                    (alloc_type)) \
00116 )
00117 #define g_chunk_new(type, chunk)        ( \
00118   (type *) g_mem_chunk_alloc (chunk) \
00119 )
00120 #define g_chunk_new0(type, chunk)       ( \
00121   (type *) g_mem_chunk_alloc0 (chunk) \
00122 )
00123 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
00124   g_mem_chunk_free ((mem_chunk), (mem)); \
00125 } G_STMT_END
00126 #define G_ALLOC_ONLY      1
00127 #define G_ALLOC_AND_FREE  2
00128 GMemChunk* g_mem_chunk_new     (const gchar *name,
00129                                 gint         atom_size,
00130                                 gsize        area_size,
00131                                 gint         type);
00132 void       g_mem_chunk_destroy (GMemChunk   *mem_chunk);
00133 gpointer   g_mem_chunk_alloc   (GMemChunk   *mem_chunk);
00134 gpointer   g_mem_chunk_alloc0  (GMemChunk   *mem_chunk);
00135 void       g_mem_chunk_free    (GMemChunk   *mem_chunk,
00136                                 gpointer     mem);
00137 void       g_mem_chunk_clean   (GMemChunk   *mem_chunk);
00138 void       g_mem_chunk_reset   (GMemChunk   *mem_chunk);
00139 void       g_mem_chunk_print   (GMemChunk   *mem_chunk);
00140 void       g_mem_chunk_info    (void);
00141 void       g_blow_chunks       (void);
00142 GAllocator*g_allocator_new     (const gchar  *name,
00143                                 guint         n_preallocs);
00144 void       g_allocator_free    (GAllocator   *allocator);
00145 #define G_ALLOCATOR_LIST       (1)
00146 #define G_ALLOCATOR_SLIST      (2)
00147 #define G_ALLOCATOR_NODE       (3)
00148 #endif /* G_DISABLE_DEPRECATED */
00149 
00150 G_END_DECLS
00151 
00152 #endif /* __G_MEM_H__ */