Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

walloc.h

Go to the documentation of this file.
00001 /*
00002  * $Id: walloc.h 13881 2007-06-18 23:48:50Z cbiere $
00003  *
00004  * Copyright (c) 2002-2003, Raphael Manfredi
00005  *
00006  *----------------------------------------------------------------------
00007  * This file is part of gtk-gnutella.
00008  *
00009  *  gtk-gnutella is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  gtk-gnutella is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with gtk-gnutella; if not, write to the Free Software
00021  *  Foundation, Inc.:
00022  *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *----------------------------------------------------------------------
00024  */
00025 
00036 #ifndef _walloc_h_
00037 #define _walloc_h_
00038 
00039 #include "common.h"
00040 #include "malloc.h"
00041 
00042 /*
00043  * Under REMAP_ZALLOC control, those routines are remapped to malloc/free.
00044  * Under TRACK_ZALLOC, we keep track of the allocation places.
00045  */
00046 
00047 #if defined(USE_DMALLOC) && !defined(REMAP_ZALLOC)
00048 #define REMAP_ZALLOC
00049 #endif
00050 
00051 #ifdef REMAP_ZALLOC
00052 
00053 #ifdef TRACK_ZALLOC
00054 #error "TRACK_ZALLOC and REMAP_ZALLOC are mutually exclusive"
00055 #endif
00056 
00057 #if 0 && GLIB_CHECK_VERSION(2, 10, 0)
00058 static inline gpointer walloc(size_t size) { return g_slice_alloc(size); }
00059 static inline gpointer walloc0(size_t size) { return g_slice_alloc0(size); }
00060 static inline void wfree(gpointer p, size_t size) { g_slice_free1(size, p); }
00061 
00062 static inline gpointer
00063 wcopy(gconstpointer p, size_t size)
00064 {
00065     gpointer x = g_slice_alloc(size);
00066     memcpy(x, p, size);
00067     return x;
00068 }
00069 
00070 static inline gpointer
00071 wrealloc(gpointer p, size_t old_size, size_t new_size)
00072 {
00073     gpointer x = g_slice_alloc(new_size);
00074     memcpy(x, p, MIN(new_size, old_size));
00075     g_slice_free1(old_size, p);
00076     return x;
00077 }
00078 
00079 #else   /* GLib < 2.10 */
00080 #define walloc(s)           g_malloc(s)
00081 #define walloc0(s)          g_malloc0(s)
00082 
00083 static inline gpointer
00084 wcopy(gconstpointer p, size_t size)
00085 {
00086     return g_memdup(p, size);
00087 }
00088 
00089 static inline void
00090 wfree(gpointer p, size_t size)
00091 {
00092     (void) size;
00093     g_free(p);
00094 }
00095 
00096 static inline gpointer
00097 wrealloc(gpointer p, size_t o, size_t n)
00098 {
00099     (void) o;
00100     return g_realloc(p, n);
00101 }
00102 #endif  /* GLib >= 2.10 */
00103 
00104 #else   /* !REMAP_ZALLOC */
00105 
00106 gpointer walloc(size_t size) WARN_UNUSED_RESULT G_GNUC_MALLOC;
00107 gpointer walloc0(size_t size) WARN_UNUSED_RESULT G_GNUC_MALLOC;
00108 void wfree(gpointer ptr, size_t size);
00109 gpointer wrealloc(gpointer old, size_t old_size, size_t new_size)
00110             WARN_UNUSED_RESULT G_GNUC_MALLOC;
00111 static inline gpointer wcopy(gconstpointer ptr, size_t size)
00112             WARN_UNUSED_RESULT G_GNUC_MALLOC;
00113 
00114 static inline gpointer
00115 wcopy(gconstpointer ptr, size_t size)
00116 {
00117     gpointer cp = walloc(size);
00118     memcpy(cp, ptr, size);
00119     return cp;
00120 }
00121 
00122 #endif  /* REMAP_ZALLOC */
00123 
00124 #ifdef TRACK_ZALLOC
00125 
00126 #define walloc(s)           walloc_track(s, __FILE__, __LINE__)
00127 #define wcopy(p,s)          wcopy_track(p, s, __FILE__, __LINE__)
00128 #define walloc0(s)          walloc0_track(s, __FILE__, __LINE__)
00129 #define wrealloc(p,o,n)     wrealloc_track(p, o, n, __FILE__, __LINE__)
00130 
00131 gpointer walloc_track(size_t size, gchar *file, gint line);
00132 gpointer walloc0_track(size_t size, gchar *file, gint line);
00133 gpointer wcopy_track(gconstpointer, size_t size, gchar *file, gint line);
00134 gpointer wrealloc_track(gpointer old, size_t old_size, size_t new_size,
00135     gchar *file, gint line);
00136 
00137 #endif  /* TRACK_ZALLOC */
00138 
00139 void wdestroy(void);
00140 
00141 #define WFREE_NULL(p,size)  \
00142 G_STMT_START {              \
00143     if (p) {                \
00144         wfree(p,size);      \
00145         p = NULL;           \
00146     }                       \
00147 } G_STMT_END
00148 
00149 #endif /* _walloc_h_ */
00150 /* vi: set ts=4 sw=4 cindent: */

Generated on Sat Jun 30 17:53:25 2007 for gtk-gnutella by  doxygen 1.3.9.1