1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
27 #include <curl/curl.h>
29 #define _MPRINTF_REPLACE
30 #include <curl/mprintf.h>
33 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
34 #include "curl_memory.h"
38 # define assert(x) Curl_nop_stmt
42 * Until 2011-08-17 libcurl's Memory Tracking feature also performed
43 * automatic malloc and free filling operations using 0xA5 and 0x13
44 * values. Our own preinitialization of dynamically allocated memory
45 * might be useful when not using third party memory debuggers, but
46 * on the other hand this would fool memory debuggers into thinking
47 * that all dynamically allocated memory is properly initialized.
49 * As a default setting, libcurl's Memory Tracking feature no longer
50 * performs preinitialization of dynamically allocated memory on its
51 * own. If you know what you are doing, and really want to retain old
52 * behavior, you can achieve this compiling with preprocessor symbols
53 * CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate
57 #ifdef CURL_MT_MALLOC_FILL
58 # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff)
59 # error "invalid CURL_MT_MALLOC_FILL or out of range"
63 #ifdef CURL_MT_FREE_FILL
64 # if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff)
65 # error "invalid CURL_MT_FREE_FILL or out of range"
69 #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL)
70 # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL)
71 # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL"
75 #ifdef CURL_MT_MALLOC_FILL
76 # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len))
78 # define mt_malloc_fill(buf,len) Curl_nop_stmt
81 #ifdef CURL_MT_FREE_FILL
82 # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len))
84 # define mt_free_fill(buf,len) Curl_nop_stmt
94 /* I'm hoping this is the thing with the strictest alignment
95 * requirements. That also means we waste some space :-( */
99 * Note that these debug functions are very simple and they are meant to
100 * remain so. For advanced analysis, record a log file and write perl scripts
103 * Don't use these with multithreaded test programs!
106 #define logfile curl_debuglogfile
107 FILE *curl_debuglogfile = NULL;
108 static bool memlimit = FALSE; /* enable memory limit */
109 static long memsize = 0; /* set number of mallocs allowed */
111 /* this sets the log file name */
112 void curl_memdebug(const char *logname)
115 if(logname && *logname)
116 logfile = fopen(logname, "w");
119 #ifdef MEMDEBUG_LOG_SYNC
120 /* Flush the log file after every line so the log isn't lost in a crash */
121 setvbuf(logfile, (char *)NULL, _IOLBF, 0);
126 /* This function sets the number of malloc() calls that should return
128 void curl_memlimit(long limit)
136 /* returns TRUE if this isn't allowed! */
137 static bool countcheck(const char *func, int line, const char *source)
139 /* if source is NULL, then the call is made internally and this check
140 should not be made */
141 if(memlimit && source) {
145 curl_memlog("LIMIT %s:%d %s reached memlimit\n",
147 /* log to stderr also */
148 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
152 return TRUE; /* RETURN ERROR! */
155 memsize--; /* countdown */
157 /* log the countdown */
159 curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
160 source, line, memsize);
164 return FALSE; /* allow this */
167 void *curl_domalloc(size_t wantedsize, int line, const char *source)
169 struct memdebug *mem;
172 assert(wantedsize != 0);
174 if(countcheck("malloc", line, source))
177 /* alloc at least 64 bytes */
178 size = sizeof(struct memdebug)+wantedsize;
180 mem = (Curl_cmalloc)(size);
182 /* fill memory with junk */
183 mt_malloc_fill(mem->mem, wantedsize);
184 mem->size = wantedsize;
188 curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
189 source, line, wantedsize, mem ? mem->mem : 0);
190 return (mem ? mem->mem : NULL);
193 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
194 int line, const char *source)
196 struct memdebug *mem;
197 size_t size, user_size;
199 assert(wanted_elements != 0);
200 assert(wanted_size != 0);
202 if(countcheck("calloc", line, source))
205 /* alloc at least 64 bytes */
206 user_size = wanted_size * wanted_elements;
207 size = sizeof(struct memdebug) + user_size;
209 mem = (Curl_ccalloc)(1, size);
211 mem->size = user_size;
214 curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
215 source, line, wanted_elements, wanted_size, mem?mem->mem:0);
216 return (mem ? mem->mem : NULL);
219 char *curl_dostrdup(const char *str, int line, const char *source)
226 if(countcheck("strdup", line, source))
231 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
233 memcpy(mem, str, len);
236 curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
237 source, line, str, len, mem);
243 wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
250 if(countcheck("wcsdup", line, source))
253 wsiz = wcslen(str) + 1;
254 bsiz = wsiz * sizeof(wchar_t);
256 mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
258 memcpy(mem, str, bsiz);
261 curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
262 source, line, str, bsiz, mem);
268 /* We provide a realloc() that accepts a NULL as pointer, which then
269 performs a malloc(). In order to work with ares. */
270 void *curl_dorealloc(void *ptr, size_t wantedsize,
271 int line, const char *source)
273 struct memdebug *mem=NULL;
275 size_t size = sizeof(struct memdebug)+wantedsize;
277 assert(wantedsize != 0);
279 if(countcheck("realloc", line, source))
282 #ifdef __INTEL_COMPILER
283 # pragma warning(push)
284 # pragma warning(disable:1684)
285 /* 1684: conversion from pointer to same-sized integral type */
289 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
291 #ifdef __INTEL_COMPILER
292 # pragma warning(pop)
295 mem = (Curl_crealloc)(mem, size);
297 curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
298 source, line, ptr, wantedsize, mem?mem->mem:NULL);
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)
317 /* 1684: conversion from pointer to same-sized integral type */
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);
333 curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr);
336 curl_socket_t curl_socket(int domain, int type, int protocol,
337 int line, const char *source)
339 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
340 "FD %s:%d socket() = %d\n" :
341 (sizeof(curl_socket_t) == sizeof(long)) ?
342 "FD %s:%d socket() = %ld\n" :
343 "FD %s:%d socket() = %zd\n" ;
345 curl_socket_t sockfd = socket(domain, type, protocol);
346 if(source && (sockfd != CURL_SOCKET_BAD))
347 curl_memlog(fmt, source, line, sockfd);
351 #ifdef HAVE_SOCKETPAIR
352 int curl_socketpair(int domain, int type, int protocol,
353 curl_socket_t socket_vector[2],
354 int line, const char *source)
356 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
357 "FD %s:%d socketpair() = %d %d\n" :
358 (sizeof(curl_socket_t) == sizeof(long)) ?
359 "FD %s:%d socketpair() = %ld %ld\n" :
360 "FD %s:%d socketpair() = %zd %zd\n" ;
362 int res = socketpair(domain, type, protocol, socket_vector);
363 if(source && (0 == res))
364 curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
369 curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
370 int line, const char *source)
372 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
373 "FD %s:%d accept() = %d\n" :
374 (sizeof(curl_socket_t) == sizeof(long)) ?
375 "FD %s:%d accept() = %ld\n" :
376 "FD %s:%d accept() = %zd\n" ;
378 struct sockaddr *addr = (struct sockaddr *)saddr;
379 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
380 curl_socket_t sockfd = accept(s, addr, addrlen);
381 if(source && (sockfd != CURL_SOCKET_BAD))
382 curl_memlog(fmt, source, line, sockfd);
386 /* separate function to allow libcurl to mark a "faked" close */
387 void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
389 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
390 "FD %s:%d sclose(%d)\n" :
391 (sizeof(curl_socket_t) == sizeof(long)) ?
392 "FD %s:%d sclose(%ld)\n" :
393 "FD %s:%d sclose(%zd)\n" ;
396 curl_memlog(fmt, source, line, sockfd);
399 /* this is our own defined way to close sockets on *ALL* platforms */
400 int curl_sclose(curl_socket_t sockfd, int line, const char *source)
402 int res=sclose(sockfd);
403 curl_mark_sclose(sockfd, line, source);
407 FILE *curl_fopen(const char *file, const char *mode,
408 int line, const char *source)
410 FILE *res=fopen(file, mode);
412 curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
413 source, line, file, mode, res);
418 FILE *curl_fdopen(int filedes, const char *mode,
419 int line, const char *source)
421 FILE *res=fdopen(filedes, mode);
423 curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
424 source, line, filedes, mode, res);
429 int curl_fclose(FILE *file, int line, const char *source)
433 assert(file != NULL);
437 curl_memlog("FILE %s:%d fclose(%p)\n",
442 #define LOGLINE_BUFSIZE 1024
444 /* this does the writting to the memory tracking log file */
445 void curl_memlog(const char *format, ...)
454 buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
458 va_start(ap, format);
459 nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
462 if(nchars > LOGLINE_BUFSIZE - 1)
463 nchars = LOGLINE_BUFSIZE - 1;
466 fwrite(buf, 1, nchars, logfile);
471 #endif /* CURLDEBUG */