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.
22 ***************************************************************************/
27 #include <curl/curl.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
33 #define _MPRINTF_REPLACE
34 #include <curl/mprintf.h>
45 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
46 #include "curl_memory.h"
50 # define assert(x) do { } while (0)
59 /* I'm hoping this is the thing with the strictest alignment
60 * requirements. That also means we waste some space :-( */
64 * Note that these debug functions are very simple and they are meant to
65 * remain so. For advanced analysis, record a log file and write perl scripts
68 * Don't use these with multithreaded test programs!
71 #define logfile curl_debuglogfile
72 FILE *curl_debuglogfile = NULL;
73 static bool memlimit = FALSE; /* enable memory limit */
74 static long memsize = 0; /* set number of mallocs allowed */
76 /* this sets the log file name */
77 void curl_memdebug(const char *logname)
81 logfile = fopen(logname, "w");
84 #ifdef MEMDEBUG_LOG_SYNC
85 /* Flush the log file after every line so the log isn't lost in a crash */
86 setvbuf(logfile, (char *)NULL, _IOLBF, 0);
91 /* This function sets the number of malloc() calls that should return
93 void curl_memlimit(long limit)
101 /* returns TRUE if this isn't allowed! */
102 static bool countcheck(const char *func, int line, const char *source)
104 /* if source is NULL, then the call is made internally and this check
105 should not be made */
106 if(memlimit && source) {
110 curl_memlog("LIMIT %s:%d %s reached memlimit\n",
112 /* log to stderr also */
113 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
117 return TRUE; /* RETURN ERROR! */
120 memsize--; /* countdown */
122 /* log the countdown */
124 curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
125 source, line, memsize);
129 return FALSE; /* allow this */
132 void *curl_domalloc(size_t wantedsize, int line, const char *source)
134 struct memdebug *mem;
137 assert(wantedsize != 0);
139 if(countcheck("malloc", line, source))
142 /* alloc at least 64 bytes */
143 size = sizeof(struct memdebug)+wantedsize;
145 mem = (Curl_cmalloc)(size);
147 /* fill memory with junk */
148 memset(mem->mem, 0xA5, wantedsize);
149 mem->size = wantedsize;
153 curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
154 source, line, wantedsize, mem ? mem->mem : 0);
155 return (mem ? mem->mem : NULL);
158 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
159 int line, const char *source)
161 struct memdebug *mem;
162 size_t size, user_size;
164 assert(wanted_elements != 0);
165 assert(wanted_size != 0);
167 if(countcheck("calloc", line, source))
170 /* alloc at least 64 bytes */
171 user_size = wanted_size * wanted_elements;
172 size = sizeof(struct memdebug) + user_size;
174 mem = (Curl_cmalloc)(size);
176 /* fill memory with zeroes */
177 memset(mem->mem, 0, user_size);
178 mem->size = user_size;
182 curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
183 source, line, wanted_elements, wanted_size, mem?mem->mem:0);
184 return (mem ? mem->mem : NULL);
187 char *curl_dostrdup(const char *str, int line, const char *source)
194 if(countcheck("strdup", line, source))
199 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
201 memcpy(mem, str, len);
204 curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
205 source, line, str, len, mem);
210 /* We provide a realloc() that accepts a NULL as pointer, which then
211 performs a malloc(). In order to work with ares. */
212 void *curl_dorealloc(void *ptr, size_t wantedsize,
213 int line, const char *source)
215 struct memdebug *mem=NULL;
217 size_t size = sizeof(struct memdebug)+wantedsize;
219 assert(wantedsize != 0);
221 if(countcheck("realloc", line, source))
225 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
227 mem = (Curl_crealloc)(mem, size);
229 curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
230 source, line, ptr, wantedsize, mem?mem->mem:NULL);
233 mem->size = wantedsize;
240 void curl_dofree(void *ptr, int line, const char *source)
242 struct memdebug *mem;
246 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
249 memset(mem->mem, 0x13, mem->size);
255 curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr);
258 int curl_socket(int domain, int type, int protocol, int line,
261 int sockfd=socket(domain, type, protocol);
262 if(source && (sockfd!=-1))
263 curl_memlog("FD %s:%d socket() = %d\n",
264 source, line, sockfd);
268 int curl_accept(int s, void *saddr, void *saddrlen,
269 int line, const char *source)
271 struct sockaddr *addr = (struct sockaddr *)saddr;
272 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
273 int sockfd=accept(s, addr, addrlen);
275 curl_memlog("FD %s:%d accept() = %d\n",
276 source, line, sockfd);
280 /* separate function to allow libcurl to mark a "faked" close */
281 void curl_mark_sclose(int sockfd, int line, const char *source)
284 curl_memlog("FD %s:%d sclose(%d)\n",
285 source, line, sockfd);
288 /* this is our own defined way to close sockets on *ALL* platforms */
289 int curl_sclose(int sockfd, int line, const char *source)
291 int res=sclose(sockfd);
292 curl_mark_sclose(sockfd, line, source);
296 FILE *curl_fopen(const char *file, const char *mode,
297 int line, const char *source)
299 FILE *res=fopen(file, mode);
301 curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
302 source, line, file, mode, res);
307 FILE *curl_fdopen(int filedes, const char *mode,
308 int line, const char *source)
310 FILE *res=fdopen(filedes, mode);
312 curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
313 source, line, filedes, mode, res);
318 int curl_fclose(FILE *file, int line, const char *source)
322 assert(file != NULL);
326 curl_memlog("FILE %s:%d fclose(%p)\n",
331 #define LOGLINE_BUFSIZE 1024
333 /* this does the writting to the memory tracking log file */
334 void curl_memlog(const char *format, ...)
343 buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
347 va_start(ap, format);
348 nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
351 if(nchars > LOGLINE_BUFSIZE - 1)
352 nchars = LOGLINE_BUFSIZE - 1;
355 fwrite(buf, 1, nchars, logfile);
360 #endif /* CURLDEBUG */