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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #include "wsint.h"
00072
00073 #if !WS_MEM_DEBUG
00074
00075
00076
00077 void *ws_malloc(size_t size)
00078 {
00079 return malloc(size);
00080 }
00081
00082
00083 void *ws_calloc(size_t num, size_t size)
00084 {
00085 return calloc(num, size);
00086 }
00087
00088
00089 void *ws_realloc(void *ptr, size_t size)
00090 {
00091 if (size == 0) {
00092 if (ptr)
00093 free(ptr);
00094
00095 return NULL;
00096 }
00097
00098 if (ptr == NULL)
00099 return malloc(size);
00100
00101 return realloc(ptr, size);
00102 }
00103
00104
00105 void *ws_memdup(const void *ptr, size_t size)
00106 {
00107 unsigned char *data = ws_malloc(size + 1);
00108
00109 if (data == NULL)
00110 return NULL;
00111
00112 memcpy(data, ptr, size);
00113 data[size] = '\0';
00114
00115 return data;
00116 }
00117
00118
00119 void *ws_strdup(const char *str)
00120 {
00121 size_t len;
00122 void *s;
00123
00124 if (str == NULL)
00125 return NULL;
00126
00127 len = strlen(str);
00128 s = ws_malloc(len + 1);
00129
00130 if (s == NULL)
00131 return NULL;
00132
00133 memcpy(s, str, len + 1);
00134
00135 return s;
00136 }
00137
00138
00139 void ws_free(void *ptr)
00140 {
00141 if (ptr)
00142 free(ptr);
00143 }
00144
00145 #else
00146
00147
00148
00149 #define SIZE(_size) (sizeof(WsMemBlockHdr) + (_size))
00150
00151 #define MAGIC 0xfe01fa77
00152
00153 struct WsMemBlockHdrRec
00154 {
00155 unsigned long magic;
00156 struct WsMemBlockHdrRec *next;
00157 struct WsMemBlockHdrRec *prev;
00158 size_t size;
00159 const char *file;
00160 int line;
00161 };
00162
00163 typedef struct WsMemBlockHdrRec WsMemBlockHdr;
00164
00165
00166 WsMemBlockHdr *blocks = NULL;
00167
00168
00169 unsigned int num_blocks = 0;
00170
00171
00172 unsigned int max_num_blocks = 0;
00173
00174
00175
00176 size_t balance = 0;
00177
00178
00179 size_t max_balance = 0;
00180
00181
00182 unsigned int alloc_number = 0;
00183
00184
00185 unsigned int num_successful_allocs = -1;
00186
00187
00188 static void add_block(WsMemBlockHdr *b, size_t size, const char *file, int line)
00189 {
00190 b->magic = MAGIC;
00191
00192 b->next = blocks;
00193 b->prev = NULL;
00194
00195 if (blocks)
00196 blocks->prev = b;
00197
00198 blocks = b;
00199
00200 b->size = size;
00201 b->file = file;
00202 b->line = line;
00203
00204 num_blocks++;
00205 balance += size;
00206
00207 if (balance > max_balance)
00208 max_balance = balance;
00209
00210 if (num_blocks > max_num_blocks)
00211 max_num_blocks = num_blocks;
00212 }
00213
00214
00215 static void remove_block(WsMemBlockHdr *b)
00216 {
00217 if (b->magic != MAGIC)
00218 ws_fatal("remove_block(): invalid magic\n");
00219
00220 if (b->next)
00221 b->next->prev = b->prev;
00222 if (b->prev)
00223 b->prev->next = b->next;
00224 else
00225 blocks = b->next;
00226
00227 balance -= b->size;
00228 num_blocks--;
00229
00230 memset(b, 0xfe, SIZE(b->size));
00231 }
00232
00233
00234 void *ws_malloc_i(size_t size, const char *file, int line)
00235 {
00236 WsMemBlockHdr *b;
00237
00238 if (alloc_number++ >= num_successful_allocs)
00239 return NULL;
00240
00241 b = malloc(SIZE(size));
00242
00243 if (b == NULL)
00244 return NULL;
00245
00246 add_block(b, size, file, line);
00247
00248 return b + 1;
00249 }
00250
00251
00252 void *ws_calloc_i(size_t num, size_t size, const char *file, int line)
00253 {
00254 void *p = ws_malloc_i(num * size, file, line);
00255
00256 if (p)
00257 memset(p, 0, num * size);
00258
00259 return p;
00260 }
00261
00262
00263 void *ws_realloc_i(void *ptr, size_t size, const char *file, int line)
00264 {
00265 WsMemBlockHdr *b = ((WsMemBlockHdr *) ptr) - 1;
00266 void *n;
00267
00268 if (ptr == NULL)
00269 return ws_malloc_i(size, file, line);
00270
00271 if (b->size >= size)
00272
00273 return ptr;
00274
00275
00276 n = ws_malloc_i(size, file, line);
00277 if (n == NULL)
00278 return NULL;
00279
00280 memcpy(n, ptr, b->size);
00281
00282
00283 remove_block(b);
00284 free(b);
00285
00286 return n;
00287 }
00288
00289
00290 void *ws_memdup_i(const void *ptr, size_t size, const char *file, int line)
00291 {
00292 void *p = ws_malloc_i(size + 1, file, line);
00293
00294 if (p) {
00295 unsigned char *cp = (unsigned char *) p;
00296
00297 memcpy(p, ptr, size);
00298 cp[size] = '\0';
00299 }
00300
00301 return p;
00302 }
00303
00304
00305 void *ws_strdup_i(const char *str, const char *file, int line)
00306 {
00307 return ws_memdup_i(str, strlen(str), file, line);
00308 }
00309
00310
00311 void ws_free_i(void *ptr)
00312 {
00313 WsMemBlockHdr *b = ((WsMemBlockHdr *) ptr) - 1;
00314
00315 if (ptr == NULL)
00316 return;
00317
00318 remove_block(b);
00319 free(b);
00320 }
00321
00322
00323 int ws_has_leaks(void)
00324 {
00325 return num_blocks || balance;
00326 }
00327
00328
00329 void ws_dump_blocks(void)
00330 {
00331 WsMemBlockHdr *b;
00332
00333 fprintf(stderr, "ws: maximum memory usage: %u blocks, %ld bytes\n",
00334 max_num_blocks, (long) max_balance);
00335 fprintf(stderr, "ws: number of allocs: %u\n", alloc_number);
00336
00337 if (num_blocks || balance) {
00338 fprintf(stderr, "ws: memory leaks: %u blocks, %ld bytes:\n",
00339 num_blocks, (long) balance);
00340
00341 for (b = blocks; b; b = b->next)
00342 fprintf(stderr, "%s:%d: %ld\n", b->file, b->line, (long) b->size);
00343 }
00344 }
00345
00346
00347 void ws_clear_leaks(unsigned int num_successful_allocs_)
00348 {
00349 alloc_number = 0;
00350 num_successful_allocs = num_successful_allocs_;
00351 blocks = NULL;
00352 }
00353
00354 #endif
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.