Make realloc() support NULL as pointer. Made to allow us to use these routines
[platform/upstream/curl.git] / lib / memdebug.c
1 #ifdef CURLDEBUG
2 /***************************************************************************
3  *                                  _   _ ____  _     
4  *  Project                     ___| | | |  _ \| |    
5  *                             / __| | | | |_) | |    
6  *                            | (__| |_| |  _ <| |___ 
7  *                             \___|\___/|_| \_\_____|
8  *
9  * Copyright (C) 1998 - 2004, 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 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
31 #endif
32
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>
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
45 #include "memdebug.h"
46
47 struct memdebug {
48   int size;
49   double mem[1];
50   /* I'm hoping this is the thing with the strictest alignment
51    * requirements.  That also means we waste some space :-( */
52 };
53
54 /*
55  * Note that these debug functions are very simple and they are meant to
56  * remain so. For advanced analysis, record a log file and write perl scripts
57  * to analyze them!
58  *
59  * Don't use these with multithreaded test programs!
60  */
61
62 #define logfile curl_debuglogfile
63 FILE *curl_debuglogfile;
64 static bool memlimit; /* enable memory limit */
65 static long memsize;  /* set number of mallocs allowed */
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 /* This function sets the number of malloc() calls that should return
77    successfully! */
78 void curl_memlimit(long limit)
79 {
80   memlimit = TRUE;
81   memsize = limit;
82 }
83
84 /* returns TRUE if this isn't allowed! */
85 static bool countcheck(const char *func, int line, const char *source)
86 {
87   /* if source is NULL, then the call is made internally and this check
88      should not be made */
89   if(memlimit && source) {
90     if(!memsize) {
91       if(logfile && source)
92         fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
93                 source, line, func);
94       return TRUE; /* RETURN ERROR! */
95     }
96     else
97       memsize--; /* countdown */
98     
99     /* log the countdown */
100     if(logfile && source)
101       fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
102               source, line, memsize);
103     
104   }
105
106   return FALSE; /* allow this */
107 }
108
109 void *curl_domalloc(size_t wantedsize, int line, const char *source)
110 {
111   struct memdebug *mem;
112   size_t size;
113
114   if(countcheck("malloc", line, source))
115     return NULL;
116
117   /* alloc at least 64 bytes */
118   size = sizeof(struct memdebug)+wantedsize;
119
120   mem=(struct memdebug *)(malloc)(size);
121   if(mem) {
122     /* fill memory with junk */
123     memset(mem->mem, 0xA5, wantedsize);
124     mem->size = wantedsize;
125   }
126
127   if(logfile && source)
128     fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
129             source, line, wantedsize, mem->mem);
130   return mem->mem;
131 }
132
133 char *curl_dostrdup(const char *str, int line, const char *source)
134 {
135   char *mem;
136   size_t len;
137
138   curlassert(str != NULL);
139   
140   if(countcheck("strdup", line, source))
141     return NULL;
142
143   len=strlen(str)+1;
144
145   mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
146   memcpy(mem, str, len);
147
148   if(logfile)
149     fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
150             source, line, str, len, mem);
151
152   return mem;
153 }
154
155 /* We provide a realloc() that accepts a NULL as pointer, which then
156    performs a malloc(). In order to work with ares. */
157 void *curl_dorealloc(void *ptr, size_t wantedsize,
158                      int line, const char *source)
159 {
160   struct memdebug *mem=NULL;
161
162   size_t size = sizeof(struct memdebug)+wantedsize;
163
164   if(countcheck("realloc", line, source))
165     return NULL;
166
167   if(ptr)
168     mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
169
170   mem=(struct memdebug *)(realloc)(mem, size);
171   if(logfile)
172     fprintf(logfile, "MEM %s:%d realloc(0x%x, %d) = %p\n",
173             source, line, ptr, wantedsize, mem?mem->mem:NULL);
174
175   if(mem) {
176     mem->size = wantedsize;
177     return mem->mem;
178   }
179
180   return NULL;
181 }
182
183 void curl_dofree(void *ptr, int line, const char *source)
184 {
185   struct memdebug *mem;
186
187   curlassert(ptr != NULL);
188
189   mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
190
191   /* destroy  */
192   memset(mem->mem, 0x13, mem->size);
193   
194   /* free for real */
195   (free)(mem);
196
197   if(logfile)
198     fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
199 }
200
201 int curl_socket(int domain, int type, int protocol, int line,
202                 const char *source)
203 {
204   int sockfd=(socket)(domain, type, protocol);
205   if(logfile && (sockfd!=-1))
206     fprintf(logfile, "FD %s:%d socket() = %d\n",
207             source, line, sockfd);
208   return sockfd;
209 }
210
211 int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
212                 int line, const char *source)
213 {
214   int sockfd=(accept)(s, addr, addrlen);
215   if(logfile)
216     fprintf(logfile, "FD %s:%d accept() = %d\n",
217             source, line, sockfd);
218   return sockfd;
219 }
220
221 /* this is our own defined way to close sockets on *ALL* platforms */
222 int curl_sclose(int sockfd, int line, const char *source)
223 {
224   int res=sclose(sockfd);
225   if(logfile)
226     fprintf(logfile, "FD %s:%d sclose(%d)\n",
227             source, line, sockfd);
228   return res;
229 }
230
231 FILE *curl_fopen(const char *file, const char *mode,
232                  int line, const char *source)
233 {
234   FILE *res=(fopen)(file, mode);
235   if(logfile)
236     fprintf(logfile, "FILE %s:%d fopen(\"%s\") = %p\n",
237             source, line, file, res);
238   return res;
239 }
240
241 int curl_fclose(FILE *file, int line, const char *source)
242 {
243   int res;
244
245   curlassert(file != NULL);
246
247   res=(fclose)(file);
248   if(logfile)
249     fprintf(logfile, "FILE %s:%d fclose(%p)\n",
250             source, line, file);
251   return res;
252 }
253 #else
254 #ifdef VMS
255 int VOID_VAR_MEMDEBUG;  
256 #endif
257 #endif /* CURLDEBUG */