2 /***************************************************************************
4 * Project ___| | | | _ \| |
6 * | (__| |_| | _ <| |___
7 * \___|\___/|_| \_\_____|
9 * Copyright (C) 1998 - 2002, 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 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
31 #else /* some kind of unix */
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
37 #define _MPRINTF_REPLACE
38 #include <curl/mprintf.h>
48 /* DONT include memdebug.h here! */
53 /* I'm hoping this is the thing with the strictest alignment
54 * requirements. That also means we waste some space :-( */
58 * Note that these debug functions are very simple and they are meant to
59 * remain so. For advanced analysis, record a log file and write perl scripts
62 * Don't use these with multithreaded test programs!
67 /* this sets the log file name */
68 void curl_memdebug(const char *logname)
71 logfile = fopen(logname, "w");
77 void *curl_domalloc(size_t wantedsize, int line, const char *source)
82 /* alloc at least 64 bytes */
83 size = sizeof(struct memdebug)+wantedsize;
85 mem=(struct memdebug *)(malloc)(size);
87 /* fill memory with junk */
88 memset(mem->mem, 0xA5, wantedsize);
89 mem->size = wantedsize;
93 fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
94 source, line, wantedsize, mem->mem);
98 char *curl_dostrdup(const char *str, int line, const char *source)
104 fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
111 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
112 memcpy(mem, str, len);
115 fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
116 source, line, str, len, mem);
121 void *curl_dorealloc(void *ptr, size_t wantedsize,
122 int line, const char *source)
124 struct memdebug *mem;
126 size_t size = sizeof(struct memdebug)+wantedsize;
128 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
130 mem=(struct memdebug *)(realloc)(mem, size);
132 fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
133 source, line, ptr, wantedsize, mem?mem->mem:NULL);
136 mem->size = wantedsize;
143 void curl_dofree(void *ptr, int line, const char *source)
145 struct memdebug *mem;
148 fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
152 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
155 memset(mem->mem, 0x13, mem->size);
161 fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
164 int curl_socket(int domain, int type, int protocol, int line, char *source)
166 int sockfd=(socket)(domain, type, protocol);
168 fprintf(logfile, "FD %s:%d socket() = %d\n",
169 source, line, sockfd);
173 int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
174 int line, const char *source)
176 int sockfd=(accept)(s, addr, addrlen);
178 fprintf(logfile, "FD %s:%d accept() = %d\n",
179 source, line, sockfd);
183 /* this is our own defined way to close sockets on *ALL* platforms */
184 int curl_sclose(int sockfd, int line, char *source)
186 int res=sclose(sockfd);
188 fprintf(logfile, "FD %s:%d sclose(%d)\n",
189 source, line, sockfd);
193 FILE *curl_fopen(const char *file, const char *mode,
194 int line, const char *source)
196 FILE *res=(fopen)(file, mode);
198 fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
199 source, line, file, res);
203 int curl_fclose(FILE *file, int line, const char *source)
208 fprintf(stderr, "ILLEGAL flose() on NULL at %s:%d\n",
215 fprintf(logfile, "FILE %s:%d fclose(%p)\n",
221 int VOID_VAR_MEMDEBUG;
223 #endif /* MALLOCDEBUG */
227 * eval: (load-file "../curl-mode.el")
230 * vim: et sw=2 ts=2 sts=2 tw=78