2 /***************************************************************************
4 * Project ___| | | | _ \| |
6 * | (__| |_| | _ <| |___
7 * \___|\___/|_| \_\_____|
9 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at http://curl.haxx.se/docs/copyright.html.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
23 ***************************************************************************/
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>
44 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
51 /* I'm hoping this is the thing with the strictest alignment
52 * requirements. That also means we waste some space :-( */
56 * Note that these debug functions are very simple and they are meant to
57 * remain so. For advanced analysis, record a log file and write perl scripts
60 * Don't use these with multithreaded test programs!
63 #define logfile curl_debuglogfile
64 FILE *curl_debuglogfile;
65 static bool memlimit; /* enable memory limit */
66 static long memsize; /* set number of mallocs allowed */
68 /* this sets the log file name */
69 void curl_memdebug(const char *logname)
72 logfile = fopen(logname, "w");
77 /* This function sets the number of malloc() calls that should return
79 void curl_memlimit(long limit)
85 /* returns TRUE if this isn't allowed! */
86 static bool countcheck(const char *func, int line, const char *source)
88 /* if source is NULL, then the call is made internally and this check
90 if(memlimit && source) {
93 fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
96 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
99 return TRUE; /* RETURN ERROR! */
102 memsize--; /* countdown */
104 /* log the countdown */
105 if(logfile && source)
106 fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
107 source, line, memsize);
111 return FALSE; /* allow this */
114 void *curl_domalloc(size_t wantedsize, int line, const char *source)
116 struct memdebug *mem;
119 if(countcheck("malloc", line, source))
122 /* alloc at least 64 bytes */
123 size = sizeof(struct memdebug)+wantedsize;
125 mem=(struct memdebug *)(Curl_cmalloc)(size);
127 /* fill memory with junk */
128 memset(mem->mem, 0xA5, wantedsize);
129 mem->size = wantedsize;
132 if(logfile && source)
133 fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
134 source, line, wantedsize, mem ? mem->mem : 0);
135 return (mem ? mem->mem : NULL);
138 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
139 int line, const char *source)
141 struct memdebug *mem;
142 size_t size, user_size;
144 if(countcheck("calloc", line, source))
147 /* alloc at least 64 bytes */
148 user_size = wanted_size * wanted_elements;
149 size = sizeof(struct memdebug) + user_size;
151 mem = (struct memdebug *)(Curl_cmalloc)(size);
153 /* fill memory with zeroes */
154 memset(mem->mem, 0, user_size);
155 mem->size = user_size;
158 if(logfile && source)
159 fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n",
160 source, line, wanted_elements, wanted_size, mem ? mem->mem : 0);
161 return (mem ? mem->mem : NULL);
164 char *curl_dostrdup(const char *str, int line, const char *source)
169 curlassert(str != NULL);
171 if(countcheck("strdup", line, source))
176 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
178 memcpy(mem, str, len);
181 fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n",
182 source, line, str, len, mem);
187 /* We provide a realloc() that accepts a NULL as pointer, which then
188 performs a malloc(). In order to work with ares. */
189 void *curl_dorealloc(void *ptr, size_t wantedsize,
190 int line, const char *source)
192 struct memdebug *mem=NULL;
194 size_t size = sizeof(struct memdebug)+wantedsize;
196 if(countcheck("realloc", line, source))
200 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
202 mem=(struct memdebug *)(Curl_crealloc)(mem, size);
204 fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n",
205 source, line, ptr, wantedsize, mem?mem->mem:NULL);
208 mem->size = wantedsize;
215 void curl_dofree(void *ptr, int line, const char *source)
217 struct memdebug *mem;
219 curlassert(ptr != NULL);
221 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
224 memset(mem->mem, 0x13, mem->size);
230 fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
233 int curl_socket(int domain, int type, int protocol, int line,
236 int sockfd=(socket)(domain, type, protocol);
237 if(logfile && (sockfd!=-1))
238 fprintf(logfile, "FD %s:%d socket() = %d\n",
239 source, line, sockfd);
243 int curl_accept(int s, void *saddr, void *saddrlen,
244 int line, const char *source)
246 struct sockaddr *addr = (struct sockaddr *)saddr;
247 socklen_t *addrlen = (socklen_t *)saddrlen;
248 int sockfd=(accept)(s, addr, addrlen);
250 fprintf(logfile, "FD %s:%d accept() = %d\n",
251 source, line, sockfd);
255 /* this is our own defined way to close sockets on *ALL* platforms */
256 int curl_sclose(int sockfd, int line, const char *source)
258 int res=sclose(sockfd);
260 fprintf(logfile, "FD %s:%d sclose(%d)\n",
261 source, line, sockfd);
265 FILE *curl_fopen(const char *file, const char *mode,
266 int line, const char *source)
268 FILE *res=(fopen)(file, mode);
270 fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
271 source, line, file, mode, res);
275 int curl_fclose(FILE *file, int line, const char *source)
279 curlassert(file != NULL);
283 fprintf(logfile, "FILE %s:%d fclose(%p)\n",
289 int VOID_VAR_MEMDEBUG;
291 /* we provide a fake do-nothing function here to avoid compiler warnings */
292 void curl_memdebug(void) {}
294 #endif /* CURLDEBUG */