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