xref: /curl/lib/curl_memory.h (revision 28d1d71f)
1 #ifndef HEADER_CURL_MEMORY_H
2 #define HEADER_CURL_MEMORY_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 
27 /*
28  * Nasty internal details ahead...
29  *
30  * File curl_memory.h must be included by _all_ *.c source files
31  * that use memory related functions strdup, malloc, calloc, realloc
32  * or free, and given source file is used to build libcurl library.
33  * It should be included immediately before memdebug.h as the last files
34  * included to avoid undesired interaction with other memory function
35  * headers in dependent libraries.
36  *
37  * There is nearly no exception to above rule. All libcurl source
38  * files in 'lib' subdirectory as well as those living deep inside
39  * 'packages' subdirectories and linked together in order to build
40  * libcurl library shall follow it.
41  *
42  * File lib/strdup.c is an exception, given that it provides a strdup
43  * clone implementation while using malloc. Extra care needed inside
44  * this one.
45  *
46  * The need for curl_memory.h inclusion is due to libcurl's feature
47  * of allowing library user to provide memory replacement functions,
48  * memory callbacks, at runtime with curl_global_init_mem()
49  *
50  * Any *.c source file used to build libcurl library that does not
51  * include curl_memory.h and uses any memory function of the five
52  * mentioned above will compile without any indication, but it will
53  * trigger weird memory related issues at runtime.
54  *
55  */
56 
57 #ifdef HEADER_CURL_MEMDEBUG_H
58 /* cleanup after memdebug.h */
59 
60 #ifdef MEMDEBUG_NODEFINES
61 #ifdef CURLDEBUG
62 
63 #undef strdup
64 #undef malloc
65 #undef calloc
66 #undef realloc
67 #undef free
68 #undef send
69 #undef recv
70 
71 #ifdef _WIN32
72 #  ifdef UNICODE
73 #    undef wcsdup
74 #    undef _wcsdup
75 #    undef _tcsdup
76 #  else
77 #    undef _tcsdup
78 #  endif
79 #endif
80 
81 #undef socket
82 #undef accept
83 #ifdef HAVE_SOCKETPAIR
84 #undef socketpair
85 #endif
86 
87 #ifndef CURL_NO_GETADDRINFO_OVERRIDE
88 #ifdef HAVE_GETADDRINFO
89 #if defined(getaddrinfo) && defined(__osf__)
90 #undef ogetaddrinfo
91 #else
92 #undef getaddrinfo
93 #endif
94 #endif /* HAVE_GETADDRINFO */
95 
96 #ifdef HAVE_FREEADDRINFO
97 #undef freeaddrinfo
98 #endif /* HAVE_FREEADDRINFO */
99 #endif /* !CURL_NO_GETADDRINFO_OVERRIDE */
100 
101 /* sclose is probably already defined, redefine it! */
102 #undef sclose
103 #undef fopen
104 #undef fdopen
105 #undef fclose
106 
107 #endif /* MEMDEBUG_NODEFINES */
108 #endif /* CURLDEBUG */
109 
110 #undef HEADER_CURL_MEMDEBUG_H
111 #endif /* HEADER_CURL_MEMDEBUG_H */
112 
113 /*
114 ** Following section applies even when CURLDEBUG is not defined.
115 */
116 
117 #undef fake_sclose
118 
119 #ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */
120 /*
121  * The following memory function replacement typedef's are COPIED from
122  * curl/curl.h and MUST match the originals. We copy them to avoid having to
123  * include curl/curl.h here. We avoid that include since it includes stdio.h
124  * and other headers that may get messed up with defines done here.
125  */
126 typedef void *(*curl_malloc_callback)(size_t size);
127 typedef void (*curl_free_callback)(void *ptr);
128 typedef void *(*curl_realloc_callback)(void *ptr, size_t size);
129 typedef char *(*curl_strdup_callback)(const char *str);
130 typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size);
131 #define CURL_DID_MEMORY_FUNC_TYPEDEFS
132 #endif
133 
134 extern curl_malloc_callback Curl_cmalloc;
135 extern curl_free_callback Curl_cfree;
136 extern curl_realloc_callback Curl_crealloc;
137 extern curl_strdup_callback Curl_cstrdup;
138 extern curl_calloc_callback Curl_ccalloc;
139 #if defined(_WIN32) && defined(UNICODE)
140 extern curl_wcsdup_callback Curl_cwcsdup;
141 #endif
142 
143 #ifndef CURLDEBUG
144 
145 /*
146  * libcurl's 'memory tracking' system defines strdup, malloc, calloc,
147  * realloc and free, along with others, in memdebug.h in a different
148  * way although still using memory callbacks forward declared above.
149  * When using the 'memory tracking' system (CURLDEBUG defined) we do
150  * not define here the five memory functions given that definitions
151  * from memdebug.h are the ones that shall be used.
152  */
153 
154 #undef strdup
155 #define strdup(ptr) Curl_cstrdup(ptr)
156 #undef malloc
157 #define malloc(size) Curl_cmalloc(size)
158 #undef calloc
159 #define calloc(nbelem,size) Curl_ccalloc(nbelem, size)
160 #undef realloc
161 #define realloc(ptr,size) Curl_crealloc(ptr, size)
162 #undef free
163 #define free(ptr) Curl_cfree(ptr)
164 
165 #ifdef _WIN32
166 #  ifdef UNICODE
167 #    undef wcsdup
168 #    define wcsdup(ptr) Curl_cwcsdup(ptr)
169 #    undef _wcsdup
170 #    define _wcsdup(ptr) Curl_cwcsdup(ptr)
171 #    undef _tcsdup
172 #    define _tcsdup(ptr) Curl_cwcsdup(ptr)
173 #  else
174 #    undef _tcsdup
175 #    define _tcsdup(ptr) Curl_cstrdup(ptr)
176 #  endif
177 #endif
178 
179 #endif /* CURLDEBUG */
180 #endif /* HEADER_CURL_MEMORY_H */
181