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#ifdef HAVE_GETADDRINFO
88#if defined(getaddrinfo) && defined(__osf__)
89#undef ogetaddrinfo
90#else
91#undef getaddrinfo
92#endif
93#endif /* HAVE_GETADDRINFO */
94
95#ifdef HAVE_FREEADDRINFO
96#undef freeaddrinfo
97#endif /* HAVE_FREEADDRINFO */
98
99/* sclose is probably already defined, redefine it! */
100#undef sclose
101#undef fopen
102#undef fdopen
103#undef fclose
104
105#endif /* MEMDEBUG_NODEFINES */
106#endif /* CURLDEBUG */
107
108#undef HEADER_CURL_MEMDEBUG_H
109#endif /* HEADER_CURL_MEMDEBUG_H */
110
111/*
112** Following section applies even when CURLDEBUG is not defined.
113*/
114
115#undef fake_sclose
116
117#ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */
118/*
119 * The following memory function replacement typedef's are COPIED from
120 * curl/curl.h and MUST match the originals. We copy them to avoid having to
121 * include curl/curl.h here. We avoid that include since it includes stdio.h
122 * and other headers that may get messed up with defines done here.
123 */
124typedef void *(*curl_malloc_callback)(size_t size);
125typedef void (*curl_free_callback)(void *ptr);
126typedef void *(*curl_realloc_callback)(void *ptr, size_t size);
127typedef char *(*curl_strdup_callback)(const char *str);
128typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size);
129#define CURL_DID_MEMORY_FUNC_TYPEDEFS
130#endif
131
132extern curl_malloc_callback Curl_cmalloc;
133extern curl_free_callback Curl_cfree;
134extern curl_realloc_callback Curl_crealloc;
135extern curl_strdup_callback Curl_cstrdup;
136extern curl_calloc_callback Curl_ccalloc;
137#if defined(WIN32) && defined(UNICODE)
138extern curl_wcsdup_callback Curl_cwcsdup;
139#endif
140
141#ifndef CURLDEBUG
142
143/*
144 * libcurl's 'memory tracking' system defines strdup, malloc, calloc,
145 * realloc and free, along with others, in memdebug.h in a different
146 * way although still using memory callbacks forward declared above.
147 * When using the 'memory tracking' system (CURLDEBUG defined) we do
148 * not define here the five memory functions given that definitions
149 * from memdebug.h are the ones that shall be used.
150 */
151
152#undef strdup
153#define strdup(ptr) Curl_cstrdup(ptr)
154#undef malloc
155#define malloc(size) Curl_cmalloc(size)
156#undef calloc
157#define calloc(nbelem,size) Curl_ccalloc(nbelem, size)
158#undef realloc
159#define realloc(ptr,size) Curl_crealloc(ptr, size)
160#undef free
161#define free(ptr) Curl_cfree(ptr)
162
163#ifdef WIN32
164# ifdef UNICODE
165# undef wcsdup
166# define wcsdup(ptr) Curl_cwcsdup(ptr)
167# undef _wcsdup
168# define _wcsdup(ptr) Curl_cwcsdup(ptr)
169# undef _tcsdup
170# define _tcsdup(ptr) Curl_cwcsdup(ptr)
171# else
172# undef _tcsdup
173# define _tcsdup(ptr) Curl_cstrdup(ptr)
174# endif
175#endif
176
177#endif /* CURLDEBUG */
178#endif /* HEADER_CURL_MEMORY_H */
179