00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include <stdio.h>
00037
00038 #include "memory.h"
00039
00040 #ifndef HAVE_MALLOC
00041 void *rpl_malloc(size_t size)
00042 {
00043 if (size == 0)
00044 ++size;
00045
00046 return malloc(size);
00047 }
00048 #endif
00049
00050 #ifndef HAVE_REALLOC
00051 void *rpl_realloc(void *p, size_t size)
00052 {
00053 if (size == 0)
00054 ++size;
00055
00056 return realloc(p, size);
00057 }
00058 #endif
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 #if defined(TOMBDEBUG) && ! defined (MEMPROF)
00085 #include "logger.h"
00086 #endif
00087
00088 #ifndef HAVE_CALLOC
00089 void *rpl_calloc(size_t n, size_t s)
00090 {
00091 size_t bytes;
00092
00093 if (n == 0 || s == 0)
00094 return calloc (1, 1);
00095
00096
00097
00098 bytes = n * s;
00099 if (bytes / s != n)
00100 return NULL;
00101
00102 return calloc (n, s);
00103 }
00104 #endif
00105
00106
00107 #ifdef MEMPROF
00108
00109
00110
00111 static int mallocCount = 0;
00112 static int freeCount = 0;
00113 static int totalAlloc = 0;
00114 static int curAlloc = 0;
00115 static int maxAlloc = 0;
00116
00117 static void print_stats()
00118 {
00119 printf("MAL:%d FRE:%d CUR:%d TOT:%d MAX:%d\n",
00120 mallocCount, freeCount, curAlloc, totalAlloc, maxAlloc);
00121 }
00122
00123 void *MALLOC(size_t size)
00124 {
00125 mallocCount++;
00126 totalAlloc += size;
00127 curAlloc += size;
00128 if (curAlloc > maxAlloc)
00129 maxAlloc = curAlloc;
00130 void *ptr = malloc(size + sizeof(int));
00131 *((int *)ptr) = size;
00132 print_stats();
00133 return ( (void *)((int *)ptr + 1) );
00134 }
00135 void *CALLOC(size_t nmemb, size_t size)
00136 {
00137 mallocCount++;
00138 totalAlloc += size * nmemb;
00139 curAlloc += size * nmemb;
00140 if (curAlloc > maxAlloc)
00141 maxAlloc = curAlloc;
00142 void *ptr = calloc(nmemb, size + sizeof(int));
00143 *((int *)ptr) = size;
00144 print_stats();
00145 return ( (void *)((int *)ptr + 1) );
00146 }
00147 void *REALLOC(void *ptr, size_t size)
00148 {
00149 freeCount++;
00150 mallocCount++;
00151 int previous = *((int *)ptr - 1);
00152 totalAlloc += size - previous;
00153 curAlloc += size - previous;
00154 if (curAlloc > maxAlloc)
00155 maxAlloc = curAlloc;
00156 ptr = realloc((void *)((int *)ptr - 1), size + sizeof(int));
00157 *((int *)ptr) = size;
00158 print_stats();
00159 return ( (void *)((int *)ptr + 1) );
00160 }
00161 void FREE(void *ptr)
00162 {
00163 freeCount++;
00164 int size = *((int *)ptr - 1);
00165 curAlloc -= size;
00166 print_stats();
00167 return free( (void *)((int *)ptr - 1) );
00168 }
00169
00170 #else
00171
00172 #ifdef TOMBDEBUG
00173 void *MALLOC(size_t size)
00174 {
00175 #ifdef DEBUG_MALLOC_0
00176 if (size <= 0)
00177 {
00178 printf("malloc called with 0! aborting...\n");
00179 _print_backtrace(stderr);
00180 abort();
00181 }
00182 #endif
00183 return malloc(size);
00184 }
00185 void *REALLOC(void *ptr, size_t size)
00186 {
00187 #ifdef DEBUG_MALLOC_0
00188 if (size <= 0)
00189 {
00190 printf("realloc called with 0! aborting...\n");
00191 _print_backtrace(stderr);
00192 abort();
00193 }
00194 #endif
00195 return realloc(ptr, size);
00196 }
00197
00198 #endif // TOMBDEBUG
00199
00200 #endif // MEMPROF