2 /*****************************************************************************
4 * Project ___| | | | _ \| |
6 * | (__| |_| | _ <| |___
7 * \___|\___/|_| \_\_____|
9 * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
11 * In order to be useful for every potential user, curl and libcurl are
12 * dual-licensed under the MPL and the MIT/X-derivate licenses.
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 MPL or the MIT/X-derivate
17 * licenses. You may pick one of these licenses.
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! */
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!
65 /* this sets the log file name */
66 void curl_memdebug(const char *logname)
69 logfile = fopen(logname, "w");
75 void *curl_domalloc(size_t wantedsize, int line, const char *source)
80 /* alloc at least 64 bytes */
81 size = sizeof(struct memdebug)+wantedsize;
83 mem=(struct memdebug *)(malloc)(size);
85 /* fill memory with junk */
86 memset(mem->mem, 0xA5, wantedsize);
87 mem->size = wantedsize;
91 fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
92 source, line, wantedsize, mem->mem);
96 char *curl_dostrdup(const char *str, int line, const char *source)
102 fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
109 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
110 memcpy(mem, str, len);
113 fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
114 source, line, str, len, mem);
119 void *curl_dorealloc(void *ptr, size_t wantedsize,
120 int line, const char *source)
122 struct memdebug *mem;
124 size_t size = sizeof(struct memdebug)+wantedsize;
126 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
128 mem=(struct memdebug *)(realloc)(mem, size);
130 fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
131 source, line, ptr, wantedsize, mem?mem->mem:NULL);
134 mem->size = wantedsize;
141 void curl_dofree(void *ptr, int line, const char *source)
143 struct memdebug *mem;
146 fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
150 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
153 memset(mem->mem, 0x13, mem->size);
159 fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
162 int curl_socket(int domain, int type, int protocol, int line, char *source)
164 int sockfd=(socket)(domain, type, protocol);
166 fprintf(logfile, "FD %s:%d socket() = %d\n",
167 source, line, sockfd);
171 int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
172 int line, const char *source)
174 int sockfd=(accept)(s, addr, addrlen);
176 fprintf(logfile, "FD %s:%d accept() = %d\n",
177 source, line, sockfd);
181 /* this is our own defined way to close sockets on *ALL* platforms */
182 int curl_sclose(int sockfd, int line, char *source)
184 int res=sclose(sockfd);
186 fprintf(logfile, "FD %s:%d sclose(%d)\n",
187 source, line, sockfd);
191 FILE *curl_fopen(const char *file, const char *mode,
192 int line, const char *source)
194 FILE *res=(fopen)(file, mode);
196 fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
197 source, line, file, res);
201 int curl_fclose(FILE *file, int line, const char *source)
206 fprintf(stderr, "ILLEGAL flose() on NULL at %s:%d\n",
213 fprintf(logfile, "FILE %s:%d fclose(%p)\n",
219 int VOID_VAR_MEMDEBUG;
221 #endif /* MALLOCDEBUG */
225 * eval: (load-file "../curl-mode.el")
228 * vim: et sw=2 ts=2 sts=2 tw=78