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  *
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 #ifndef __G_MEM_H__
00028 #define __G_MEM_H__
00029 
00030 #include <g_types.h>
00031 
00032 /* optionally feature DMALLOC memory allocation debugger
00033  */
00034 #ifdef USE_DMALLOC
00035 #include "dmalloc.h"
00036 #endif
00037 
00038 G_BEGIN_DECLS
00039 
00040 typedef struct _GAllocator      GAllocator;
00041 typedef struct _GMemChunk       GMemChunk;
00042 
00043 /* Provide macros for easily allocating memory. The macros
00044  *  will cast the allocated memory to the specified type
00045  *  in order to avoid compiler warnings. (Makes the code neater).
00046  */
00047 
00048 #ifdef __DMALLOC_H__
00049 #  define g_new(type, count)            (ALLOC (type, count))
00050 #  define g_new0(type, count)           (CALLOC (type, count))
00051 #  define g_renew(type, mem, count)     (REALLOC (mem, type, count))
00052 #else /* __DMALLOC_H__ */
00053 #  define g_new(type, count)      \
00054       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
00055 #  define g_new0(type, count)     \
00056       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
00057 #  define g_renew(type, mem, count)       \
00058       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
00059 #endif /* __DMALLOC_H__ */
00060 
00061 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
00062   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
00063                    sizeof (type), \
00064                    sizeof (type) * (pre_alloc), \
00065                    (alloc_type)) \
00066 )
00067 #define g_chunk_new(type, chunk)        ( \
00068   (type *) g_mem_chunk_alloc (chunk) \
00069 )
00070 #define g_chunk_new0(type, chunk)       ( \
00071   (type *) g_mem_chunk_alloc0 (chunk) \
00072 )
00073 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
00074   g_mem_chunk_free ((mem_chunk), (mem)); \
00075 } G_STMT_END
00076 
00077 /* Memory allocation and debugging
00078  */
00079 #ifdef USE_DMALLOC
00080 
00081 #define g_malloc(size)       ((gpointer) MALLOC (size))
00082 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
00083 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
00084 #define g_free(mem)          FREE (mem)
00085 
00086 #else /* !USE_DMALLOC */
00087 
00088 gpointer g_malloc      (gulong    size);
00089 gpointer g_malloc0     (gulong    size);
00090 gpointer g_realloc     (gpointer  mem,
00091                         gulong    size);
00092 void     g_free        (gpointer  mem);
00093 
00094 #endif /* !USE_DMALLOC */
00095 
00096 void     g_mem_profile (void);
00097 void     g_mem_check   (gpointer  mem);
00098 
00099 /* Generic allocators
00100  */
00101 GAllocator* g_allocator_new   (const gchar  *name,
00102                                guint         n_preallocs);
00103 void        g_allocator_free  (GAllocator   *allocato);
00104 
00105 #define G_ALLOCATOR_LIST        (1)
00106 #define G_ALLOCATOR_SLIST       (2)
00107 #define G_ALLOCATOR_NODE        (3)
00108 
00109 /* "g_mem_chunk_new" creates a new memory chunk.
00110  * Memory chunks are used to allocate pieces of memory which are
00111  *  always the same size. Lists are a good example of such a data type.
00112  * The memory chunk allocates and frees blocks of memory as needed.
00113  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
00114  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
00115  *  fault...somewhere).
00116  *
00117  * Oh yeah, GMemChunk is an opaque data type. (You don't really
00118  *  want to know what's going on inside do you?)
00119  */
00120 
00121 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
00122  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
00123  *  atom. (They are also useful for lists which use MemChunk to allocate
00124  *  memory but are also part of the MemChunk implementation).
00125  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
00126  */
00127 
00128 #define G_ALLOC_ONLY      1
00129 #define G_ALLOC_AND_FREE  2
00130 
00131 GMemChunk* g_mem_chunk_new     (gchar     *name,
00132                                 gint       atom_size,
00133                                 gulong     area_size,
00134                                 gint       type);
00135 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
00136 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
00137 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
00138 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
00139                                 gpointer   mem);
00140 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
00141 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
00142 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
00143 void       g_mem_chunk_info    (void);
00144 
00145 /* Ah yes...we have a "g_blow_chunks" function.
00146  * "g_blow_chunks" simply compresses all the chunks. This operation
00147  *  consists of freeing every memory area that should be freed (but
00148  *  which we haven't gotten around to doing yet). And, no,
00149  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
00150  *  much better name than "g_mem_chunk_clean_all" or something
00151  *  similar.
00152  */
00153 void g_blow_chunks (void);
00154 
00155 G_END_DECLS
00156 
00157 #endif /* __G_MEM_H__ */
00158 

Generated on Tue Jul 5 14:14:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1