8e88cea580360ba3bae616b8f90348745f748c92
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / memdebug.h
1 #ifndef HEADER_CURL_MEMDEBUG_H
2 #define HEADER_CURL_MEMDEBUG_H
3 #ifdef CURLDEBUG
4 /***************************************************************************
5  *                                  _   _ ____  _
6  *  Project                     ___| | | |  _ \| |
7  *                             / __| | | | |_) | |
8  *                            | (__| |_| |  _ <| |___
9  *                             \___|\___/|_| \_\_____|
10  *
11  * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
12  *
13  * This software is licensed as described in the file COPYING, which
14  * you should have received as part of this distribution. The terms
15  * are also available at https://curl.se/docs/copyright.html.
16  *
17  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
18  * copies of the Software, and permit persons to whom the Software is
19  * furnished to do so, under the terms of the COPYING file.
20  *
21  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22  * KIND, either express or implied.
23  *
24  ***************************************************************************/
25
26 /*
27  * CAUTION: this header is designed to work when included by the app-side
28  * as well as the library. Do not mix with library internals!
29  */
30
31 #define CURL_MT_LOGFNAME_BUFSIZE 512
32
33 extern FILE *curl_dbg_logfile;
34
35 /* memory functions */
36 CURL_EXTERN void *curl_dbg_malloc(size_t size, int line, const char *source);
37 CURL_EXTERN void *curl_dbg_calloc(size_t elements, size_t size, int line,
38                                   const char *source);
39 CURL_EXTERN void *curl_dbg_realloc(void *ptr, size_t size, int line,
40                                    const char *source);
41 CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source);
42 CURL_EXTERN char *curl_dbg_strdup(const char *str, int line, const char *src);
43 #if defined(WIN32) && defined(UNICODE)
44 CURL_EXTERN wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line,
45                                      const char *source);
46 #endif
47
48 CURL_EXTERN void curl_dbg_memdebug(const char *logname);
49 CURL_EXTERN void curl_dbg_memlimit(long limit);
50 CURL_EXTERN void curl_dbg_log(const char *format, ...);
51
52 /* file descriptor manipulators */
53 CURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
54                                           int line, const char *source);
55 CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd,
56                                       int line, const char *source);
57 CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd,
58                                 int line, const char *source);
59 CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s, void *a, void *alen,
60                                           int line, const char *source);
61 #ifdef HAVE_SOCKETPAIR
62 CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol,
63                                     curl_socket_t socket_vector[2],
64                                     int line, const char *source);
65 #endif
66
67 /* send/receive sockets */
68 CURL_EXTERN SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
69                                          SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
70                                          SEND_TYPE_ARG3 len,
71                                          SEND_TYPE_ARG4 flags, int line,
72                                          const char *source);
73 CURL_EXTERN RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd,
74                                          RECV_TYPE_ARG2 buf,
75                                          RECV_TYPE_ARG3 len,
76                                          RECV_TYPE_ARG4 flags, int line,
77                                          const char *source);
78
79 /* FILE functions */
80 CURL_EXTERN FILE *curl_dbg_fopen(const char *file, const char *mode, int line,
81                                  const char *source);
82 CURL_EXTERN FILE *curl_dbg_fdopen(int filedes, const char *mode,
83                                   int line, const char *source);
84
85 CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source);
86
87 #ifndef MEMDEBUG_NODEFINES
88
89 /* Set this symbol on the command-line, recompile all lib-sources */
90 #undef strdup
91 #define strdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__)
92 #define malloc(size) curl_dbg_malloc(size, __LINE__, __FILE__)
93 #define calloc(nbelem,size) curl_dbg_calloc(nbelem, size, __LINE__, __FILE__)
94 #define realloc(ptr,size) curl_dbg_realloc(ptr, size, __LINE__, __FILE__)
95 #define free(ptr) curl_dbg_free(ptr, __LINE__, __FILE__)
96 #define send(a,b,c,d) curl_dbg_send(a,b,c,d, __LINE__, __FILE__)
97 #define recv(a,b,c,d) curl_dbg_recv(a,b,c,d, __LINE__, __FILE__)
98
99 #ifdef WIN32
100 #  ifdef UNICODE
101 #    undef wcsdup
102 #    define wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
103 #    undef _wcsdup
104 #    define _wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
105 #    undef _tcsdup
106 #    define _tcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
107 #  else
108 #    undef _tcsdup
109 #    define _tcsdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__)
110 #  endif
111 #endif
112
113 #undef socket
114 #define socket(domain,type,protocol)\
115  curl_dbg_socket(domain, type, protocol, __LINE__, __FILE__)
116 #undef accept /* for those with accept as a macro */
117 #define accept(sock,addr,len)\
118  curl_dbg_accept(sock, addr, len, __LINE__, __FILE__)
119 #ifdef HAVE_SOCKETPAIR
120 #define socketpair(domain,type,protocol,socket_vector)\
121  curl_dbg_socketpair(domain, type, protocol, socket_vector, __LINE__, __FILE__)
122 #endif
123
124 #ifdef HAVE_GETADDRINFO
125 #if defined(getaddrinfo) && defined(__osf__)
126 /* OSF/1 and Tru64 have getaddrinfo as a define already, so we cannot define
127    our macro as for other platforms. Instead, we redefine the new name they
128    define getaddrinfo to become! */
129 #define ogetaddrinfo(host,serv,hint,res) \
130   curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
131 #else
132 #undef getaddrinfo
133 #define getaddrinfo(host,serv,hint,res) \
134   curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
135 #endif
136 #endif /* HAVE_GETADDRINFO */
137
138 #ifdef HAVE_FREEADDRINFO
139 #undef freeaddrinfo
140 #define freeaddrinfo(data) \
141   curl_dbg_freeaddrinfo(data, __LINE__, __FILE__)
142 #endif /* HAVE_FREEADDRINFO */
143
144 /* sclose is probably already defined, redefine it! */
145 #undef sclose
146 #define sclose(sockfd) curl_dbg_sclose(sockfd,__LINE__,__FILE__)
147
148 #define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd,__LINE__,__FILE__)
149
150 #undef fopen
151 #define fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__)
152 #undef fdopen
153 #define fdopen(file,mode) curl_dbg_fdopen(file,mode,__LINE__,__FILE__)
154 #define fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__)
155
156 #endif /* MEMDEBUG_NODEFINES */
157
158 #endif /* CURLDEBUG */
159
160 /*
161 ** Following section applies even when CURLDEBUG is not defined.
162 */
163
164 #ifndef fake_sclose
165 #define fake_sclose(x)  Curl_nop_stmt
166 #endif
167
168 /*
169  * Curl_safefree defined as a macro to allow MemoryTracking feature
170  * to log free() calls at same location where Curl_safefree is used.
171  * This macro also assigns NULL to given pointer when free'd.
172  */
173
174 #define Curl_safefree(ptr) \
175   do { free((ptr)); (ptr) = NULL;} while(0)
176
177 #endif /* HEADER_CURL_MEMDEBUG_H */