ENH: typo
[cmake.git] / Utilities / cmcurl / memdebug.c
blob76f3d34e93a7c6de3c5efda5ee47dc108d37efd4
1 #ifdef CURLDEBUG
2 /***************************************************************************
3 * _ _ ____ _
4 * Project ___| | | | _ \| |
5 * / __| | | | |_) | |
6 * | (__| |_| | _ <| |___
7 * \___|\___/|_| \_\_____|
9 * Copyright (C) 1998 - 2005, 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.
22 * $Id: memdebug.c,v 1.2 2007-03-15 19:22:13 andy Exp $
23 ***************************************************************************/
25 #include "setup.h"
27 #include <curl/curl.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
31 #endif
33 #define _MPRINTF_REPLACE
34 #include <curl/mprintf.h>
35 #include "urldata.h"
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
45 #include "memory.h"
46 #include "memdebug.h"
48 struct memdebug {
49 size_t size;
50 double mem[1];
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
58 * to analyze them!
60 * Don't use these with multithreaded test programs!
63 #define logfile curl_debuglogfile
64 FILE *curl_debuglogfile = NULL;
65 static bool memlimit = FALSE; /* enable memory limit */
66 static long memsize = 0; /* set number of mallocs allowed */
68 /* this sets the log file name */
69 void curl_memdebug(const char *logname)
71 if (!logfile) {
72 if(logname)
73 logfile = fopen(logname, "w");
74 else
75 logfile = stderr;
79 /* This function sets the number of malloc() calls that should return
80 successfully! */
81 void curl_memlimit(long limit)
83 if (!memlimit) {
84 memlimit = TRUE;
85 memsize = limit;
89 /* returns TRUE if this isn't allowed! */
90 static bool countcheck(const char *func, int line, const char *source)
92 /* if source is NULL, then the call is made internally and this check
93 should not be made */
94 if(memlimit && source) {
95 if(!memsize) {
96 if(logfile && source)
97 fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
98 source, line, func);
99 if(source)
100 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
101 source, line, func);
102 errno = ENOMEM;
103 return TRUE; /* RETURN ERROR! */
105 else
106 memsize--; /* countdown */
108 /* log the countdown */
109 if(logfile && source)
110 fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
111 source, line, memsize);
115 return FALSE; /* allow this */
118 void *curl_domalloc(size_t wantedsize, int line, const char *source)
120 struct memdebug *mem;
121 size_t size;
123 if(countcheck("malloc", line, source))
124 return NULL;
126 /* alloc at least 64 bytes */
127 size = sizeof(struct memdebug)+wantedsize;
129 mem=(struct memdebug *)(Curl_cmalloc)(size);
130 if(mem) {
131 /* fill memory with junk */
132 memset(mem->mem, 0xA5, wantedsize);
133 mem->size = wantedsize;
136 if(logfile && source)
137 fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
138 source, line, wantedsize, mem ? mem->mem : 0);
139 return (mem ? mem->mem : NULL);
142 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
143 int line, const char *source)
145 struct memdebug *mem;
146 size_t size, user_size;
148 if(countcheck("calloc", line, source))
149 return NULL;
151 /* alloc at least 64 bytes */
152 user_size = wanted_size * wanted_elements;
153 size = sizeof(struct memdebug) + user_size;
155 mem = (struct memdebug *)(Curl_cmalloc)(size);
156 if(mem) {
157 /* fill memory with zeroes */
158 memset(mem->mem, 0, user_size);
159 mem->size = user_size;
162 if(logfile && source)
163 fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n",
164 source, line, wanted_elements, wanted_size, mem ? mem->mem : 0);
165 return (mem ? mem->mem : NULL);
168 char *curl_dostrdup(const char *str, int line, const char *source)
170 char *mem;
171 size_t len;
173 curlassert(str != NULL);
175 if(countcheck("strdup", line, source))
176 return NULL;
178 len=strlen(str)+1;
180 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
181 if (mem)
182 memcpy(mem, str, len);
184 if(logfile)
185 fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n",
186 source, line, str, len, mem);
188 return mem;
191 /* We provide a realloc() that accepts a NULL as pointer, which then
192 performs a malloc(). In order to work with ares. */
193 void *curl_dorealloc(void *ptr, size_t wantedsize,
194 int line, const char *source)
196 struct memdebug *mem=NULL;
198 size_t size = sizeof(struct memdebug)+wantedsize;
200 if(countcheck("realloc", line, source))
201 return NULL;
203 if(ptr)
204 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
206 mem=(struct memdebug *)(Curl_crealloc)(mem, size);
207 if(logfile)
208 fprintf(logfile, "MEM %s:%d realloc(%p, %zd) = %p\n",
209 source, line, ptr, wantedsize, mem?mem->mem:NULL);
211 if(mem) {
212 mem->size = wantedsize;
213 return mem->mem;
216 return NULL;
219 void curl_dofree(void *ptr, int line, const char *source)
221 struct memdebug *mem;
223 curlassert(ptr != NULL);
225 mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
227 /* destroy */
228 memset(mem->mem, 0x13, mem->size);
230 /* free for real */
231 (Curl_cfree)(mem);
233 if(logfile)
234 fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
237 int curl_socket(int domain, int type, int protocol, int line,
238 const char *source)
240 int sockfd=(socket)(domain, type, protocol);
241 if(logfile && (sockfd!=-1))
242 fprintf(logfile, "FD %s:%d socket() = %d\n",
243 source, line, sockfd);
244 return sockfd;
247 int curl_accept(int s, void *saddr, void *saddrlen,
248 int line, const char *source)
250 struct sockaddr *addr = (struct sockaddr *)saddr;
251 socklen_t *addrlen = (socklen_t *)saddrlen;
252 int sockfd=(accept)(s, addr, addrlen);
253 if(logfile)
254 fprintf(logfile, "FD %s:%d accept() = %d\n",
255 source, line, sockfd);
256 return sockfd;
259 /* this is our own defined way to close sockets on *ALL* platforms */
260 int curl_sclose(int sockfd, int line, const char *source)
262 int res=sclose(sockfd);
263 if(logfile)
264 fprintf(logfile, "FD %s:%d sclose(%d)\n",
265 source, line, sockfd);
266 return res;
269 FILE *curl_fopen(const char *file, const char *mode,
270 int line, const char *source)
272 FILE *res=(fopen)(file, mode);
273 if(logfile)
274 fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
275 source, line, file, mode, res);
276 return res;
279 int curl_fclose(FILE *file, int line, const char *source)
281 int res;
283 curlassert(file != NULL);
285 res=(fclose)(file);
286 if(logfile)
287 fprintf(logfile, "FILE %s:%d fclose(%p)\n",
288 source, line, file);
289 return res;
291 #else
292 #ifdef VMS
293 int VOID_VAR_MEMDEBUG;
294 #else
295 /* we provide a fake do-nothing function here to avoid compiler warnings */
296 void curl_memdebug(void) {}
297 #endif /* VMS */
298 #endif /* CURLDEBUG */