31 #define MEMDEBUG_NODEFINES 54 #ifdef CURL_MT_MALLOC_FILL 55 # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff) 56 # error "invalid CURL_MT_MALLOC_FILL or out of range" 60 #ifdef CURL_MT_FREE_FILL 61 # if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff) 62 # error "invalid CURL_MT_FREE_FILL or out of range" 66 #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL) 67 # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL) 68 # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL" 72 #ifdef CURL_MT_MALLOC_FILL 73 # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len)) 75 # define mt_malloc_fill(buf,len) Curl_nop_stmt 78 #ifdef CURL_MT_FREE_FILL 79 # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len)) 81 # define mt_free_fill(buf,len) Curl_nop_stmt 103 #define logfile curl_debuglogfile 104 FILE *curl_debuglogfile = NULL;
105 static bool memlimit =
FALSE;
106 static long memsize = 0;
109 void curl_memdebug(
const char *logname)
112 if(logname && *logname)
116 #ifdef MEMDEBUG_LOG_SYNC 119 setbuf(logfile, (
char *)NULL);
126 void curl_memlimit(
long limit)
135 static bool countcheck(
const char *func,
int line,
const char *source)
139 if(memlimit && source) {
143 curl_memlog(
"LIMIT %s:%d %s reached memlimit\n",
146 fprintf(stderr,
"LIMIT %s:%d %s reached memlimit\n",
162 void *curl_domalloc(
size_t wantedsize,
int line,
const char *source)
164 struct memdebug *mem;
169 if(countcheck(
"malloc", line, source))
173 size =
sizeof(
struct memdebug) + wantedsize;
178 mt_malloc_fill(mem->mem, wantedsize);
179 mem->size = wantedsize;
183 curl_memlog(
"MEM %s:%d malloc(%zu) = %p\n",
184 source, line, wantedsize,
185 mem ? (
void *)mem->mem : (
void *)0);
187 return (mem ? mem->mem : NULL);
190 void *curl_docalloc(
size_t wanted_elements,
size_t wanted_size,
191 int line,
const char *source)
193 struct memdebug *mem;
194 size_t size, user_size;
199 if(countcheck(
"calloc", line, source))
203 user_size = wanted_size * wanted_elements;
204 size =
sizeof(
struct memdebug) + user_size;
208 mem->size = user_size;
211 curl_memlog(
"MEM %s:%d calloc(%zu,%zu) = %p\n",
212 source, line, wanted_elements, wanted_size,
213 mem ? (
void *)mem->mem : (
void *)0);
215 return (mem ? mem->mem : NULL);
218 char *curl_dostrdup(
const char *
str,
int line,
const char *source)
225 if(countcheck(
"strdup", line, source))
228 len = strlen(str) + 1;
230 mem = curl_domalloc(len, 0, NULL);
235 curl_memlog(
"MEM %s:%d strdup(%p) (%zu) = %p\n",
236 source, line, (
const void *)str, len, (
const void *)mem);
241 #if defined(WIN32) && defined(UNICODE) 242 wchar_t *curl_dowcsdup(
const wchar_t *str,
int line,
const char *source)
249 if(countcheck(
"wcsdup", line, source))
252 wsiz = wcslen(str) + 1;
253 bsiz = wsiz *
sizeof(wchar_t);
255 mem = curl_domalloc(bsiz, 0, NULL);
260 curl_memlog(
"MEM %s:%d wcsdup(%p) (%zu) = %p\n",
261 source, line, (
void *)str, bsiz, (
void *)mem);
269 void *curl_dorealloc(
void *
ptr,
size_t wantedsize,
270 int line,
const char *source)
272 struct memdebug *mem = NULL;
274 size_t size =
sizeof(
struct memdebug) + wantedsize;
278 if(countcheck(
"realloc", line, source))
281 #ifdef __INTEL_COMPILER 282 # pragma warning(push) 283 # pragma warning(disable:1684) 288 mem = (
void *)((
char *)ptr - offsetof(
struct memdebug, mem));
290 #ifdef __INTEL_COMPILER 291 # pragma warning(pop) 296 curl_memlog(
"MEM %s:%d realloc(%p, %zu) = %p\n",
297 source, line, (
void *)ptr, wantedsize,
298 mem ? (
void *)mem->mem : (
void *)0);
301 mem->size = wantedsize;
308 void curl_dofree(
void *ptr,
int line,
const char *source)
310 struct memdebug *mem;
314 #ifdef __INTEL_COMPILER 315 # pragma warning(push) 316 # pragma warning(disable:1684) 320 mem = (
void *)((
char *)ptr - offsetof(
struct memdebug, mem));
322 #ifdef __INTEL_COMPILER 323 # pragma warning(pop) 327 mt_free_fill(mem->mem, mem->size);
334 curl_memlog(
"MEM %s:%d free(%p)\n", source, line, (
void *)ptr);
337 curl_socket_t curl_socket(
int domain,
int type,
int protocol,
338 int line,
const char *source)
341 "FD %s:%d socket() = %d\n" :
343 "FD %s:%d socket() = %ld\n" :
344 "FD %s:%d socket() = %zd\n";
349 curl_memlog(fmt, source, line, sockfd);
354 #ifdef HAVE_SOCKETPAIR 355 int curl_socketpair(
int domain,
int type,
int protocol,
357 int line,
const char *source)
360 "FD %s:%d socketpair() = %d %d\n" :
362 "FD %s:%d socketpair() = %ld %ld\n" :
363 "FD %s:%d socketpair() = %zd %zd\n";
365 int res = socketpair(domain, type, protocol, socket_vector);
367 if(source && (0 == res))
368 curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
375 int line,
const char *source)
378 "FD %s:%d accept() = %d\n" :
380 "FD %s:%d accept() = %ld\n" :
381 "FD %s:%d accept() = %zd\n";
383 struct sockaddr *addr = (
struct sockaddr *)saddr;
389 curl_memlog(fmt, source, line, sockfd);
395 void curl_mark_sclose(
curl_socket_t sockfd,
int line,
const char *source)
398 "FD %s:%d sclose(%d)\n":
400 "FD %s:%d sclose(%ld)\n":
401 "FD %s:%d sclose(%zd)\n";
404 curl_memlog(fmt, source, line, sockfd);
408 int curl_sclose(
curl_socket_t sockfd,
int line,
const char *source)
411 curl_mark_sclose(sockfd, line, source);
415 FILE *curl_fopen(
const char *file,
const char *mode,
416 int line,
const char *source)
418 FILE *res = fopen(file, mode);
421 curl_memlog(
"FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
422 source, line, file, mode, (
void *)res);
428 FILE *curl_fdopen(
int filedes,
const char *mode,
429 int line,
const char *source)
431 FILE *res = fdopen(filedes, mode);
434 curl_memlog(
"FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
435 source, line, filedes, mode, (
void *)res);
441 int curl_fclose(FILE *file,
int line,
const char *source)
450 curl_memlog(
"FILE %s:%d fclose(%p)\n",
451 source, line, (
void *)file);
456 #define LOGLINE_BUFSIZE 1024 459 void curl_memlog(
const char *format, ...)
472 va_start(ap, format);
473 nchars =
vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
476 if(nchars > LOGLINE_BUFSIZE - 1)
477 nchars = LOGLINE_BUFSIZE - 1;
480 fwrite(buf, 1, (
size_t)nchars, logfile);
curl_free_callback Curl_cfree
UNITTEST_START char * ptr
curl_calloc_callback Curl_ccalloc
curl_realloc_callback Curl_crealloc
memcpy(filename, filename1, strlen(filename1))
CURL_TYPEOF_CURL_SOCKLEN_T curl_socklen_t
curl_malloc_callback Curl_cmalloc
CURL_TYPEOF_CURL_OFF_T curl_off_t