gmem.h

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

Copyright © Nokia Corporation 2001-2008
Back to top