1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 ***************************************************************************/
26 #include <curl/curl.h>
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
32 #define _MPRINTF_REPLACE
33 #include <curl/mprintf.h>
44 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
45 #include "curl_memory.h"
49 # define assert(x) do { } while (0)
58 /* I'm hoping this is the thing with the strictest alignment
59 * requirements. That also means we waste some space :-( */
63 * Note that these debug functions are very simple and they are meant to
64 * remain so. For advanced analysis, record a log file and write perl scripts
67 * Don't use these with multithreaded test programs!
70 #define logfile curl_debuglogfile
71 FILE *curl_debuglogfile = NULL;
72 static bool memlimit = FALSE; /* enable memory limit */
73 static long memsize = 0; /* set number of mallocs allowed */
75 /* this sets the log file name */
76 void curl_memdebug(const char *logname)
80 logfile = fopen(logname, "w");
83 #ifdef MEMDEBUG_LOG_SYNC
84 /* Flush the log file after every line so the log isn't lost in a crash */
85 setvbuf(logfile, (char *)NULL, _IOLBF, 0);
90 /* This function sets the number of malloc() calls that should return
92 void curl_memlimit(long limit)
100 /* returns TRUE if this isn't allowed! */
101 static bool countcheck(const char *func, int line, const char *source)
103 /* if source is NULL, then the call is made internally and this check
104 should not be made */
105 if(memlimit && source) {
109 curl_memlog("LIMIT %s:%d %s reached memlimit\n",
111 /* log to stderr also */
112 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
116 return TRUE; /* RETURN ERROR! */
119 memsize--; /* countdown */
121 /* log the countdown */
123 curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
124 source, line, memsize);
128 return FALSE; /* allow this */
131 void *curl_domalloc(size_t wantedsize, int line, const char *source)
133 struct memdebug *mem;
136 assert(wantedsize != 0);
138 if(countcheck("malloc", line, source))
141 /* alloc at least 64 bytes */
142 size = sizeof(struct memdebug)+wantedsize;
144 mem = (Curl_cmalloc)(size);
146 /* fill memory with junk */
147 memset(mem->mem, 0xA5, wantedsize);
148 mem->size = wantedsize;
152 curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
153 source, line, wantedsize, mem ? mem->mem : 0);
154 return (mem ? mem->mem : NULL);
157 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
158 int line, const char *source)
160 struct memdebug *mem;
161 size_t size, user_size;
163 assert(wanted_elements != 0);
164 assert(wanted_size != 0);
166 if(countcheck("calloc", line, source))
169 /* alloc at least 64 bytes */
170 user_size = wanted_size * wanted_elements;
171 size = sizeof(struct memdebug) + user_size;
173 mem = (Curl_cmalloc)(size);
175 /* fill memory with zeroes */
176 memset(mem->mem, 0, user_size);
177 mem->size = user_size;
181 curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
182 source, line, wanted_elements, wanted_size, mem?mem->mem:0);
183 return (mem ? mem->mem : NULL);
186 char *curl_dostrdup(const char *str, int line, const char *source)
193 if(countcheck("strdup", line, source))
198 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
200 memcpy(mem, str, len);
203 curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
204 source, line, str, len, mem);
209 /* We provide a realloc() that accepts a NULL as pointer, which then
210 performs a malloc(). In order to work with ares. */
211 void *curl_dorealloc(void *ptr, size_t wantedsize,
212 int line, const char *source)
214 struct memdebug *mem=NULL;
216 size_t size = sizeof(struct memdebug)+wantedsize;
218 assert(wantedsize != 0);
220 if(countcheck("realloc", line, source))
223 #ifdef __INTEL_COMPILER
224 # pragma warning(push)
225 # pragma warning(disable:1684)
226 /* 1684: conversion from pointer to same-sized integral type */
230 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
232 #ifdef __INTEL_COMPILER
233 # pragma warning(pop)
236 mem = (Curl_crealloc)(mem, size);
238 curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
239 source, line, ptr, wantedsize, mem?mem->mem:NULL);
242 mem->size = wantedsize;
249 void curl_dofree(void *ptr, int line, const char *source)
251 struct memdebug *mem;
255 #ifdef __INTEL_COMPILER
256 # pragma warning(push)
257 # pragma warning(disable:1684)
258 /* 1684: conversion from pointer to same-sized integral type */
261 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
263 #ifdef __INTEL_COMPILER
264 # pragma warning(pop)
268 memset(mem->mem, 0x13, mem->size);
274 curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr);
277 curl_socket_t curl_socket(int domain, int type, int protocol,
278 int line, const char *source)
280 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
281 "FD %s:%d socket() = %d\n" :
282 (sizeof(curl_socket_t) == sizeof(long)) ?
283 "FD %s:%d socket() = %ld\n" :
284 "FD %s:%d socket() = %zd\n" ;
286 curl_socket_t sockfd = socket(domain, type, protocol);
287 if(source && (sockfd != CURL_SOCKET_BAD))
288 curl_memlog(fmt, source, line, sockfd);
292 curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
293 int line, const char *source)
295 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
296 "FD %s:%d accept() = %d\n" :
297 (sizeof(curl_socket_t) == sizeof(long)) ?
298 "FD %s:%d accept() = %ld\n" :
299 "FD %s:%d accept() = %zd\n" ;
301 struct sockaddr *addr = (struct sockaddr *)saddr;
302 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
303 curl_socket_t sockfd = accept(s, addr, addrlen);
304 if(source && (sockfd != CURL_SOCKET_BAD))
305 curl_memlog(fmt, source, line, sockfd);
309 /* separate function to allow libcurl to mark a "faked" close */
310 void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
312 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
313 "FD %s:%d sclose(%d)\n" :
314 (sizeof(curl_socket_t) == sizeof(long)) ?
315 "FD %s:%d sclose(%ld)\n" :
316 "FD %s:%d sclose(%zd)\n" ;
319 curl_memlog(fmt, source, line, sockfd);
322 /* this is our own defined way to close sockets on *ALL* platforms */
323 int curl_sclose(curl_socket_t sockfd, int line, const char *source)
325 int res=sclose(sockfd);
326 curl_mark_sclose(sockfd, line, source);
330 FILE *curl_fopen(const char *file, const char *mode,
331 int line, const char *source)
333 FILE *res=fopen(file, mode);
335 curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
336 source, line, file, mode, res);
341 FILE *curl_fdopen(int filedes, const char *mode,
342 int line, const char *source)
344 FILE *res=fdopen(filedes, mode);
346 curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
347 source, line, filedes, mode, res);
352 int curl_fclose(FILE *file, int line, const char *source)
356 assert(file != NULL);
360 curl_memlog("FILE %s:%d fclose(%p)\n",
365 #define LOGLINE_BUFSIZE 1024
367 /* this does the writting to the memory tracking log file */
368 void curl_memlog(const char *format, ...)
377 buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
381 va_start(ap, format);
382 nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
385 if(nchars > LOGLINE_BUFSIZE - 1)
386 nchars = LOGLINE_BUFSIZE - 1;
389 fwrite(buf, 1, nchars, logfile);
394 #endif /* CURLDEBUG */