updated source code boilerplate/header
[platform/upstream/curl.git] / lib / memdebug.c
1 #ifdef MALLOCDEBUG
2 /***************************************************************************
3  *                                  _   _ ____  _     
4  *  Project                     ___| | | |  _ \| |    
5  *                             / __| | | | |_) | |    
6  *                            | (__| |_| |  _ <| |___ 
7  *                             \___|\___/|_| \_\_____|
8  *
9  * Copyright (C) 1998 - 2002, Daniel Stenberg, <daniel@haxx.se>, et al.
10  *
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.
14  * 
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.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  * $Id$
23  ***************************************************************************/
24
25 #include "setup.h"
26
27 #include <curl/curl.h>
28
29 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
30 #include <winsock.h>
31 #else /* some kind of unix */
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #endif
36
37 #define _MPRINTF_REPLACE
38 #include <curl/mprintf.h>
39 #include "urldata.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 /* DONT include memdebug.h here! */
49
50 struct memdebug {
51   int size;
52   double mem[1];
53   /* I'm hoping this is the thing with the strictest alignment
54    * requirements.  That also means we waste some space :-( */
55 };
56
57 /*
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
60  * to analyze them!
61  *
62  * Don't use these with multithreaded test programs!
63  */
64
65 FILE *logfile;
66
67 /* this sets the log file name */
68 void curl_memdebug(const char *logname)
69 {
70   if(logname)
71     logfile = fopen(logname, "w");
72   else
73     logfile = stderr;
74 }
75
76
77 void *curl_domalloc(size_t wantedsize, int line, const char *source)
78 {
79   struct memdebug *mem;
80   size_t size;
81
82   /* alloc at least 64 bytes */
83   size = sizeof(struct memdebug)+wantedsize;
84
85   mem=(struct memdebug *)(malloc)(size);
86   if(mem) {
87     /* fill memory with junk */
88     memset(mem->mem, 0xA5, wantedsize);
89     mem->size = wantedsize;
90   }
91
92   if(logfile && source)
93     fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
94             source, line, wantedsize, mem->mem);
95   return mem->mem;
96 }
97
98 char *curl_dostrdup(const char *str, int line, const char *source)
99 {
100   char *mem;
101   size_t len;
102   
103   if(NULL ==str) {
104     fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
105             source, line);
106     exit(2);
107   }
108
109   len=strlen(str)+1;
110
111   mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
112   memcpy(mem, str, len);
113
114   if(logfile)
115     fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
116             source, line, str, len, mem);
117
118   return mem;
119 }
120
121 void *curl_dorealloc(void *ptr, size_t wantedsize,
122                      int line, const char *source)
123 {
124   struct memdebug *mem;
125
126   size_t size = sizeof(struct memdebug)+wantedsize;
127
128   mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
129
130   mem=(struct memdebug *)(realloc)(mem, size);
131   if(logfile)
132     fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
133             source, line, ptr, wantedsize, mem?mem->mem:NULL);
134
135   if(mem) {
136     mem->size = wantedsize;
137     return mem->mem;
138   }
139
140   return NULL;
141 }
142
143 void curl_dofree(void *ptr, int line, const char *source)
144 {
145   struct memdebug *mem;
146
147   if(NULL == ptr) {
148     fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
149             source, line);
150     exit(2);
151   }
152   mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
153
154   /* destroy  */
155   memset(mem->mem, 0x13, mem->size);
156   
157   /* free for real */
158   (free)(mem);
159
160   if(logfile)
161     fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
162 }
163
164 int curl_socket(int domain, int type, int protocol, int line, char *source)
165 {
166   int sockfd=(socket)(domain, type, protocol);
167   if(logfile)
168     fprintf(logfile, "FD %s:%d socket() = %d\n",
169             source, line, sockfd);
170   return sockfd;
171 }
172
173 int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
174                 int line, const char *source)
175 {
176   int sockfd=(accept)(s, addr, addrlen);
177   if(logfile)
178     fprintf(logfile, "FD %s:%d accept() = %d\n",
179             source, line, sockfd);
180   return sockfd;
181 }
182
183 /* this is our own defined way to close sockets on *ALL* platforms */
184 int curl_sclose(int sockfd, int line, char *source)
185 {
186   int res=sclose(sockfd);
187   if(logfile)
188     fprintf(logfile, "FD %s:%d sclose(%d)\n",
189             source, line, sockfd);
190   return res;
191 }
192
193 FILE *curl_fopen(const char *file, const char *mode,
194                  int line, const char *source)
195 {
196   FILE *res=(fopen)(file, mode);
197   if(logfile)
198     fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
199             source, line, file, res);
200   return res;
201 }
202
203 int curl_fclose(FILE *file, int line, const char *source)
204 {
205   int res;
206
207   if(NULL == file) {
208     fprintf(stderr, "ILLEGAL flose() on NULL at %s:%d\n",
209             source, line);
210     exit(2);
211   }
212
213   res=(fclose)(file);
214   if(logfile)
215     fprintf(logfile, "FILE %s:%d fclose(%p)\n",
216             source, line, file);
217   return res;
218 }
219 #else
220 #ifdef VMS
221 int VOID_VAR_MEMDEBUG;  
222 #endif
223 #endif /* MALLOCDEBUG */
224
225 /*
226  * local variables:
227  * eval: (load-file "../curl-mode.el")
228  * end:
229  * vim600: fdm=marker
230  * vim: et sw=2 ts=2 sts=2 tw=78
231  */