Set errno = ENOMEM on faild countcheck().
[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 "memory.h"
46 #include "memdebug.h"
47
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 :-( */
53 };
54
55 /*
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!
59  *
60  * Don't use these with multithreaded test programs!
61  */
62
63 #define logfile curl_debuglogfile
64 FILE *curl_debuglogfile;
65 static bool memlimit; /* enable memory limit */
66 static long memsize;  /* set number of mallocs allowed */
67
68 /* this sets the log file name */
69 void curl_memdebug(const char *logname)
70 {
71   if(logname)
72     logfile = fopen(logname, "w");
73   else
74     logfile = stderr;
75 }
76
77 /* This function sets the number of malloc() calls that should return
78    successfully! */
79 void curl_memlimit(long limit)
80 {
81   memlimit = TRUE;
82   memsize = limit;
83 }
84
85 /* returns TRUE if this isn't allowed! */
86 static bool countcheck(const char *func, int line, const char *source)
87 {
88   /* if source is NULL, then the call is made internally and this check
89      should not be made */
90   if(memlimit && source) {
91     if(!memsize) {
92       if(logfile && source)
93         fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
94                 source, line, func);
95       if(source)
96         fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
97                 source, line, func);
98       errno = ENOMEM;
99       return TRUE; /* RETURN ERROR! */
100     }
101     else
102       memsize--; /* countdown */
103
104     /* log the countdown */
105     if(logfile && source)
106       fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
107               source, line, memsize);
108
109   }
110
111   return FALSE; /* allow this */
112 }
113
114 void *curl_domalloc(size_t wantedsize, int line, const char *source)
115 {
116   struct memdebug *mem;
117   size_t size;
118
119   if(countcheck("malloc", line, source))
120     return NULL;
121
122   /* alloc at least 64 bytes */
123   size = sizeof(struct memdebug)+wantedsize;
124
125   mem=(struct memdebug *)(Curl_cmalloc)(size);
126   if(mem) {
127     /* fill memory with junk */
128     memset(mem->mem, 0xA5, wantedsize);
129     mem->size = wantedsize;
130   }
131
132   if(logfile && source)
133     fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
134             source, line, wantedsize, mem ? mem->mem : 0);
135   return (mem ? mem->mem : NULL);
136 }
137
138 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
139                     int line, const char *source)
140 {
141   struct memdebug *mem;
142   size_t size, user_size;
143
144   if(countcheck("calloc", line, source))
145     return NULL;
146
147   /* alloc at least 64 bytes */
148   user_size = wanted_size * wanted_elements;
149   size = sizeof(struct memdebug) + user_size;
150
151   mem = (struct memdebug *)(Curl_cmalloc)(size);
152   if(mem) {
153     /* fill memory with zeroes */
154     memset(mem->mem, 0, user_size);
155     mem->size = user_size;
156   }
157
158   if(logfile && source)
159     fprintf(logfile, "MEM %s:%d calloc(%u,%u) = %p\n",
160             source, line, wanted_elements, wanted_size, mem ? mem->mem : 0);
161   return (mem ? mem->mem : NULL);
162 }
163
164 char *curl_dostrdup(const char *str, int line, const char *source)
165 {
166   char *mem;
167   size_t len;
168
169   curlassert(str != NULL);
170
171   if(countcheck("strdup", line, source))
172     return NULL;
173
174   len=strlen(str)+1;
175
176   mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
177   if (mem)
178   memcpy(mem, str, len);
179
180   if(logfile)
181     fprintf(logfile, "MEM %s:%d strdup(%p) (%zd) = %p\n",
182             source, line, str, len, mem);
183
184   return mem;
185 }
186
187 /* We provide a realloc() that accepts a NULL as pointer, which then
188    performs a malloc(). In order to work with ares. */
189 void *curl_dorealloc(void *ptr, size_t wantedsize,
190                      int line, const char *source)
191 {
192   struct memdebug *mem=NULL;
193
194   size_t size = sizeof(struct memdebug)+wantedsize;
195
196   if(countcheck("realloc", line, source))
197     return NULL;
198
199   if(ptr)
200     mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
201
202   mem=(struct memdebug *)(Curl_crealloc)(mem, size);
203   if(logfile)
204     fprintf(logfile, "MEM %s:%d realloc(0x%x, %zd) = %p\n",
205             source, line, ptr, wantedsize, mem?mem->mem:NULL);
206
207   if(mem) {
208     mem->size = wantedsize;
209     return mem->mem;
210   }
211
212   return NULL;
213 }
214
215 void curl_dofree(void *ptr, int line, const char *source)
216 {
217   struct memdebug *mem;
218
219   curlassert(ptr != NULL);
220
221   mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
222
223   /* destroy  */
224   memset(mem->mem, 0x13, mem->size);
225
226   /* free for real */
227   (Curl_cfree)(mem);
228
229   if(logfile)
230     fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
231 }
232
233 int curl_socket(int domain, int type, int protocol, int line,
234                 const char *source)
235 {
236   int sockfd=(socket)(domain, type, protocol);
237   if(logfile && (sockfd!=-1))
238     fprintf(logfile, "FD %s:%d socket() = %d\n",
239             source, line, sockfd);
240   return sockfd;
241 }
242
243 int curl_accept(int s, void *saddr, void *saddrlen,
244                 int line, const char *source)
245 {
246   struct sockaddr *addr = (struct sockaddr *)saddr;
247   socklen_t *addrlen = (socklen_t *)saddrlen;
248   int sockfd=(accept)(s, addr, addrlen);
249   if(logfile)
250     fprintf(logfile, "FD %s:%d accept() = %d\n",
251             source, line, sockfd);
252   return sockfd;
253 }
254
255 /* this is our own defined way to close sockets on *ALL* platforms */
256 int curl_sclose(int sockfd, int line, const char *source)
257 {
258   int res=sclose(sockfd);
259   if(logfile)
260     fprintf(logfile, "FD %s:%d sclose(%d)\n",
261             source, line, sockfd);
262   return res;
263 }
264
265 FILE *curl_fopen(const char *file, const char *mode,
266                  int line, const char *source)
267 {
268   FILE *res=(fopen)(file, mode);
269   if(logfile)
270     fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
271             source, line, file, mode, res);
272   return res;
273 }
274
275 int curl_fclose(FILE *file, int line, const char *source)
276 {
277   int res;
278
279   curlassert(file != NULL);
280
281   res=(fclose)(file);
282   if(logfile)
283     fprintf(logfile, "FILE %s:%d fclose(%p)\n",
284             source, line, file);
285   return res;
286 }
287 #else
288 #ifdef VMS
289 int VOID_VAR_MEMDEBUG;
290 #else
291 /* we provide a fake do-nothing function here to avoid compiler warnings */
292 void curl_memdebug(void) {}
293 #endif /* VMS */
294 #endif /* CURLDEBUG */