1 | #ifndef HEADER_CURL_MEMORY_H |
2 | #define |
3 | /*************************************************************************** |
4 | * _ _ ____ _ |
5 | * Project ___| | | | _ \| | |
6 | * / __| | | | |_) | | |
7 | * | (__| |_| | _ <| |___ |
8 | * \___|\___/|_| \_\_____| |
9 | * |
10 | * Copyright (C) 1998 - 2019, 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.haxx.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 | ***************************************************************************/ |
24 | |
25 | /* |
26 | * Nasty internal details ahead... |
27 | * |
28 | * File curl_memory.h must be included by _all_ *.c source files |
29 | * that use memory related functions strdup, malloc, calloc, realloc |
30 | * or free, and given source file is used to build libcurl library. |
31 | * It should be included immediately before memdebug.h as the last files |
32 | * included to avoid undesired interaction with other memory function |
33 | * headers in dependent libraries. |
34 | * |
35 | * There is nearly no exception to above rule. All libcurl source |
36 | * files in 'lib' subdirectory as well as those living deep inside |
37 | * 'packages' subdirectories and linked together in order to build |
38 | * libcurl library shall follow it. |
39 | * |
40 | * File lib/strdup.c is an exception, given that it provides a strdup |
41 | * clone implementation while using malloc. Extra care needed inside |
42 | * this one. |
43 | * |
44 | * The need for curl_memory.h inclusion is due to libcurl's feature |
45 | * of allowing library user to provide memory replacement functions, |
46 | * memory callbacks, at runtime with curl_global_init_mem() |
47 | * |
48 | * Any *.c source file used to build libcurl library that does not |
49 | * include curl_memory.h and uses any memory function of the five |
50 | * mentioned above will compile without any indication, but it will |
51 | * trigger weird memory related issues at runtime. |
52 | * |
53 | * OTOH some source files from 'lib' subdirectory may additionally be |
54 | * used directly as source code when using some curlx_ functions by |
55 | * third party programs that don't even use libcurl at all. When using |
56 | * these source files in this way it is necessary these are compiled |
57 | * with CURLX_NO_MEMORY_CALLBACKS defined, in order to ensure that no |
58 | * attempt of calling libcurl's memory callbacks is done from code |
59 | * which can not use this machinery. |
60 | * |
61 | * Notice that libcurl's 'memory tracking' system works chaining into |
62 | * the memory callback machinery. This implies that when compiling |
63 | * 'lib' source files with CURLX_NO_MEMORY_CALLBACKS defined this file |
64 | * disengages usage of libcurl's 'memory tracking' system, defining |
65 | * MEMDEBUG_NODEFINES and overriding CURLDEBUG purpose. |
66 | * |
67 | * CURLX_NO_MEMORY_CALLBACKS takes precedence over CURLDEBUG. This is |
68 | * done in order to allow building a 'memory tracking' enabled libcurl |
69 | * and at the same time allow building programs which do not use it. |
70 | * |
71 | * Programs and libraries in 'tests' subdirectories have specific |
72 | * purposes and needs, and as such each one will use whatever fits |
73 | * best, depending additionally whether it links with libcurl or not. |
74 | * |
75 | * Caveat emptor. Proper curlx_* separation is a work in progress |
76 | * the same as CURLX_NO_MEMORY_CALLBACKS usage, some adjustments may |
77 | * still be required. IOW don't use them yet, there are sharp edges. |
78 | */ |
79 | |
80 | #ifdef HEADER_CURL_MEMDEBUG_H |
81 | #error "Header memdebug.h shall not be included before curl_memory.h" |
82 | #endif |
83 | |
84 | #ifndef CURLX_NO_MEMORY_CALLBACKS |
85 | |
86 | #ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */ |
87 | /* |
88 | * The following memory function replacement typedef's are COPIED from |
89 | * curl/curl.h and MUST match the originals. We copy them to avoid having to |
90 | * include curl/curl.h here. We avoid that include since it includes stdio.h |
91 | * and other headers that may get messed up with defines done here. |
92 | */ |
93 | typedef void *(*curl_malloc_callback)(size_t size); |
94 | typedef void (*curl_free_callback)(void *ptr); |
95 | typedef void *(*curl_realloc_callback)(void *ptr, size_t size); |
96 | typedef char *(*curl_strdup_callback)(const char *str); |
97 | typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size); |
98 | #define CURL_DID_MEMORY_FUNC_TYPEDEFS |
99 | #endif |
100 | |
101 | extern curl_malloc_callback Curl_cmalloc; |
102 | extern curl_free_callback Curl_cfree; |
103 | extern curl_realloc_callback Curl_crealloc; |
104 | extern curl_strdup_callback Curl_cstrdup; |
105 | extern curl_calloc_callback Curl_ccalloc; |
106 | #if defined(WIN32) && defined(UNICODE) |
107 | extern curl_wcsdup_callback Curl_cwcsdup; |
108 | #endif |
109 | |
110 | #ifndef CURLDEBUG |
111 | |
112 | /* |
113 | * libcurl's 'memory tracking' system defines strdup, malloc, calloc, |
114 | * realloc and free, along with others, in memdebug.h in a different |
115 | * way although still using memory callbacks forward declared above. |
116 | * When using the 'memory tracking' system (CURLDEBUG defined) we do |
117 | * not define here the five memory functions given that definitions |
118 | * from memdebug.h are the ones that shall be used. |
119 | */ |
120 | |
121 | #undef strdup |
122 | #define strdup(ptr) Curl_cstrdup(ptr) |
123 | #undef malloc |
124 | #define malloc(size) Curl_cmalloc(size) |
125 | #undef calloc |
126 | #define calloc(nbelem,size) Curl_ccalloc(nbelem, size) |
127 | #undef realloc |
128 | #define realloc(ptr,size) Curl_crealloc(ptr, size) |
129 | #undef free |
130 | #define free(ptr) Curl_cfree(ptr) |
131 | |
132 | #ifdef WIN32 |
133 | # ifdef UNICODE |
134 | # undef wcsdup |
135 | # define wcsdup(ptr) Curl_cwcsdup(ptr) |
136 | # undef _wcsdup |
137 | # define _wcsdup(ptr) Curl_cwcsdup(ptr) |
138 | # undef _tcsdup |
139 | # define _tcsdup(ptr) Curl_cwcsdup(ptr) |
140 | # else |
141 | # undef _tcsdup |
142 | # define _tcsdup(ptr) Curl_cstrdup(ptr) |
143 | # endif |
144 | #endif |
145 | |
146 | #endif /* CURLDEBUG */ |
147 | |
148 | #else /* CURLX_NO_MEMORY_CALLBACKS */ |
149 | |
150 | #ifndef MEMDEBUG_NODEFINES |
151 | #define MEMDEBUG_NODEFINES |
152 | #endif |
153 | |
154 | #endif /* CURLX_NO_MEMORY_CALLBACKS */ |
155 | |
156 | #endif /* HEADER_CURL_MEMORY_H */ |
157 | |