const-ified lots of function arguments
[platform/upstream/curl.git] / lib / memdebug.c
1 #ifdef MALLOCDEBUG
2 /*****************************************************************************
3  *                                  _   _ ____  _     
4  *  Project                     ___| | | |  _ \| |    
5  *                             / __| | | | |_) | |    
6  *                            | (__| |_| |  _ <| |___ 
7  *                             \___|\___/|_| \_\_____|
8  *
9  * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
10  *
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.
13  *
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.
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 /*
51  * Note that these debug functions are very simple and they are meant to
52  * remain so. For advanced analysis, record a log file and write perl scripts
53  * to analyze them!
54  *
55  * Don't use these with multithreaded test programs!
56  */
57
58 FILE *logfile;
59
60 /* this sets the log file name */
61 void curl_memdebug(char *logname)
62 {
63   logfile = fopen(logname, "w");
64 }
65
66
67 void *curl_domalloc(size_t size, int line, const char *source)
68 {
69   void *mem=(malloc)(size);
70   fprintf(logfile?logfile:stderr, "MEM %s:%d malloc(%d) = %p\n",
71           source, line, size, mem);
72   return mem;
73 }
74
75 char *curl_dostrdup(const char *str, int line, const char *source)
76 {
77   char *mem;
78   size_t len;
79   
80   if(NULL ==str) {
81     fprintf(stderr, "ILLEGAL strdup() on NULL at %s:%d\n",
82             source, line);
83     exit(2);
84   }
85
86   mem=(strdup)(str);
87   len=strlen(str)+1;
88   fprintf(logfile?logfile:stderr, "MEM %s:%d strdup(%p) (%d) = %p\n",
89           source, line, str, len, mem);
90   return mem;
91 }
92
93 void *curl_dorealloc(void *ptr, size_t size, int line, const char *source)
94 {
95   void *mem=(realloc)(ptr, size);
96   fprintf(logfile?logfile:stderr, "MEM %s:%d realloc(%p, %d) = %p\n",
97           source, line, ptr, size, mem);
98   return mem;
99 }
100
101 void curl_dofree(void *ptr, int line, const char *source)
102 {
103   if(NULL == ptr) {
104     fprintf(stderr, "ILLEGAL free() on NULL at %s:%d\n",
105             source, line);
106     exit(2);
107   }
108
109   (free)(ptr);
110
111   fprintf(logfile?logfile:stderr, "MEM %s:%d free(%p)\n",
112           source, line, ptr);
113 }
114
115 int curl_socket(int domain, int type, int protocol, int line, char *source)
116 {
117   int sockfd=(socket)(domain, type, protocol);
118   fprintf(logfile?logfile:stderr, "FD %s:%d socket() = %d\n",
119           source, line, sockfd);
120   return sockfd;
121 }
122
123 int curl_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
124                 int line, const char *source)
125 {
126   int sockfd=(accept)(s, addr, addrlen);
127   fprintf(logfile?logfile:stderr, "FD %s:%d accept() = %d\n",
128           source, line, sockfd);
129   return sockfd;
130 }
131
132 /* this is our own defined way to close sockets on *ALL* platforms */
133 int curl_sclose(int sockfd, int line, char *source)
134 {
135   int res=sclose(sockfd);
136   fprintf(logfile?logfile:stderr, "FD %s:%d sclose(%d)\n",
137           source, line, sockfd);
138   return res;
139 }
140
141 FILE *curl_fopen(const char *file, const char *mode,
142                  int line, const char *source)
143 {
144   FILE *res=(fopen)(file, mode);
145   fprintf(logfile?logfile:stderr, "FILE %s:%d fopen(\"%s\") = %p\n",
146           source, line, file, res);
147   return res;
148 }
149
150 int curl_fclose(FILE *file, int line, const char *source)
151 {
152   int res=(fclose)(file);
153   fprintf(logfile?logfile:stderr, "FILE %s:%d fclose(%p)\n",
154           source, line, file);
155   return res;
156 }
157
158 #endif /* MALLOCDEBUG */