curl_memory.h (4949B)
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 CURLDEBUG 61 62 #undef strdup 63 #undef malloc 64 #undef calloc 65 #undef realloc 66 #undef free 67 #undef send 68 #undef recv 69 70 #ifdef _WIN32 71 #undef _tcsdup 72 #endif 73 74 #undef socket 75 #undef accept 76 #ifdef HAVE_ACCEPT4 77 #undef accept4 78 #endif 79 #ifdef HAVE_SOCKETPAIR 80 #undef socketpair 81 #endif 82 83 /* sclose is probably already defined, redefine it! */ 84 #undef sclose 85 #define sclose(x) CURL_SCLOSE(x) 86 #undef fopen 87 #ifdef CURL_FOPEN 88 #define fopen(fname, mode) CURL_FOPEN(fname, mode) 89 #endif 90 #undef fdopen 91 #undef fclose 92 93 #endif /* CURLDEBUG */ 94 95 #undef HEADER_CURL_MEMDEBUG_H 96 #endif /* HEADER_CURL_MEMDEBUG_H */ 97 98 /* 99 ** Following section applies even when CURLDEBUG is not defined. 100 */ 101 102 #undef fake_sclose 103 104 #ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */ 105 /* 106 * The following memory function replacement typedef's are COPIED from 107 * curl/curl.h and MUST match the originals. We copy them to avoid having to 108 * include curl/curl.h here. We avoid that include since it includes stdio.h 109 * and other headers that may get messed up with defines done here. 110 */ 111 typedef void *(*curl_malloc_callback)(size_t size); 112 typedef void (*curl_free_callback)(void *ptr); 113 typedef void *(*curl_realloc_callback)(void *ptr, size_t size); 114 typedef char *(*curl_strdup_callback)(const char *str); 115 typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size); 116 #define CURL_DID_MEMORY_FUNC_TYPEDEFS 117 #endif 118 119 extern curl_malloc_callback Curl_cmalloc; 120 extern curl_free_callback Curl_cfree; 121 extern curl_realloc_callback Curl_crealloc; 122 extern curl_strdup_callback Curl_cstrdup; 123 extern curl_calloc_callback Curl_ccalloc; 124 125 #ifndef CURLDEBUG 126 127 /* 128 * libcurl's 'memory tracking' system defines strdup, malloc, calloc, 129 * realloc and free, along with others, in memdebug.h in a different 130 * way although still using memory callbacks forward declared above. 131 * When using the 'memory tracking' system (CURLDEBUG defined) we do 132 * not define here the five memory functions given that definitions 133 * from memdebug.h are the ones that shall be used. 134 */ 135 136 #undef strdup 137 #define strdup(ptr) Curl_cstrdup(ptr) 138 #undef malloc 139 #define malloc(size) Curl_cmalloc(size) 140 #undef calloc 141 #define calloc(nbelem,size) Curl_ccalloc(nbelem, size) 142 #undef realloc 143 #define realloc(ptr,size) Curl_crealloc(ptr, size) 144 #undef free 145 #define free(ptr) Curl_cfree(ptr) 146 147 #ifdef _WIN32 148 #undef _tcsdup 149 #ifdef UNICODE 150 #define _tcsdup(ptr) Curl_wcsdup(ptr) 151 #else 152 #define _tcsdup(ptr) Curl_cstrdup(ptr) 153 #endif 154 #endif /* _WIN32 */ 155 156 #endif /* CURLDEBUG */ 157 #endif /* HEADER_CURL_MEMORY_H */