113498266Sopenharmony_ci#ifndef HEADER_CURL_MEMORY_H 213498266Sopenharmony_ci#define HEADER_CURL_MEMORY_H 313498266Sopenharmony_ci/*************************************************************************** 413498266Sopenharmony_ci * _ _ ____ _ 513498266Sopenharmony_ci * Project ___| | | | _ \| | 613498266Sopenharmony_ci * / __| | | | |_) | | 713498266Sopenharmony_ci * | (__| |_| | _ <| |___ 813498266Sopenharmony_ci * \___|\___/|_| \_\_____| 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 1113498266Sopenharmony_ci * 1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1513498266Sopenharmony_ci * 1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1913498266Sopenharmony_ci * 2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 2113498266Sopenharmony_ci * KIND, either express or implied. 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci * SPDX-License-Identifier: curl 2413498266Sopenharmony_ci * 2513498266Sopenharmony_ci ***************************************************************************/ 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci/* 2813498266Sopenharmony_ci * Nasty internal details ahead... 2913498266Sopenharmony_ci * 3013498266Sopenharmony_ci * File curl_memory.h must be included by _all_ *.c source files 3113498266Sopenharmony_ci * that use memory related functions strdup, malloc, calloc, realloc 3213498266Sopenharmony_ci * or free, and given source file is used to build libcurl library. 3313498266Sopenharmony_ci * It should be included immediately before memdebug.h as the last files 3413498266Sopenharmony_ci * included to avoid undesired interaction with other memory function 3513498266Sopenharmony_ci * headers in dependent libraries. 3613498266Sopenharmony_ci * 3713498266Sopenharmony_ci * There is nearly no exception to above rule. All libcurl source 3813498266Sopenharmony_ci * files in 'lib' subdirectory as well as those living deep inside 3913498266Sopenharmony_ci * 'packages' subdirectories and linked together in order to build 4013498266Sopenharmony_ci * libcurl library shall follow it. 4113498266Sopenharmony_ci * 4213498266Sopenharmony_ci * File lib/strdup.c is an exception, given that it provides a strdup 4313498266Sopenharmony_ci * clone implementation while using malloc. Extra care needed inside 4413498266Sopenharmony_ci * this one. 4513498266Sopenharmony_ci * 4613498266Sopenharmony_ci * The need for curl_memory.h inclusion is due to libcurl's feature 4713498266Sopenharmony_ci * of allowing library user to provide memory replacement functions, 4813498266Sopenharmony_ci * memory callbacks, at runtime with curl_global_init_mem() 4913498266Sopenharmony_ci * 5013498266Sopenharmony_ci * Any *.c source file used to build libcurl library that does not 5113498266Sopenharmony_ci * include curl_memory.h and uses any memory function of the five 5213498266Sopenharmony_ci * mentioned above will compile without any indication, but it will 5313498266Sopenharmony_ci * trigger weird memory related issues at runtime. 5413498266Sopenharmony_ci * 5513498266Sopenharmony_ci */ 5613498266Sopenharmony_ci 5713498266Sopenharmony_ci#ifdef HEADER_CURL_MEMDEBUG_H 5813498266Sopenharmony_ci/* cleanup after memdebug.h */ 5913498266Sopenharmony_ci 6013498266Sopenharmony_ci#ifdef MEMDEBUG_NODEFINES 6113498266Sopenharmony_ci#ifdef CURLDEBUG 6213498266Sopenharmony_ci 6313498266Sopenharmony_ci#undef strdup 6413498266Sopenharmony_ci#undef malloc 6513498266Sopenharmony_ci#undef calloc 6613498266Sopenharmony_ci#undef realloc 6713498266Sopenharmony_ci#undef free 6813498266Sopenharmony_ci#undef send 6913498266Sopenharmony_ci#undef recv 7013498266Sopenharmony_ci 7113498266Sopenharmony_ci#ifdef _WIN32 7213498266Sopenharmony_ci# ifdef UNICODE 7313498266Sopenharmony_ci# undef wcsdup 7413498266Sopenharmony_ci# undef _wcsdup 7513498266Sopenharmony_ci# undef _tcsdup 7613498266Sopenharmony_ci# else 7713498266Sopenharmony_ci# undef _tcsdup 7813498266Sopenharmony_ci# endif 7913498266Sopenharmony_ci#endif 8013498266Sopenharmony_ci 8113498266Sopenharmony_ci#undef socket 8213498266Sopenharmony_ci#undef accept 8313498266Sopenharmony_ci#ifdef HAVE_SOCKETPAIR 8413498266Sopenharmony_ci#undef socketpair 8513498266Sopenharmony_ci#endif 8613498266Sopenharmony_ci 8713498266Sopenharmony_ci#ifdef HAVE_GETADDRINFO 8813498266Sopenharmony_ci#if defined(getaddrinfo) && defined(__osf__) 8913498266Sopenharmony_ci#undef ogetaddrinfo 9013498266Sopenharmony_ci#else 9113498266Sopenharmony_ci#undef getaddrinfo 9213498266Sopenharmony_ci#endif 9313498266Sopenharmony_ci#endif /* HAVE_GETADDRINFO */ 9413498266Sopenharmony_ci 9513498266Sopenharmony_ci#ifdef HAVE_FREEADDRINFO 9613498266Sopenharmony_ci#undef freeaddrinfo 9713498266Sopenharmony_ci#endif /* HAVE_FREEADDRINFO */ 9813498266Sopenharmony_ci 9913498266Sopenharmony_ci/* sclose is probably already defined, redefine it! */ 10013498266Sopenharmony_ci#undef sclose 10113498266Sopenharmony_ci#undef fopen 10213498266Sopenharmony_ci#undef fdopen 10313498266Sopenharmony_ci#undef fclose 10413498266Sopenharmony_ci 10513498266Sopenharmony_ci#endif /* MEMDEBUG_NODEFINES */ 10613498266Sopenharmony_ci#endif /* CURLDEBUG */ 10713498266Sopenharmony_ci 10813498266Sopenharmony_ci#undef HEADER_CURL_MEMDEBUG_H 10913498266Sopenharmony_ci#endif /* HEADER_CURL_MEMDEBUG_H */ 11013498266Sopenharmony_ci 11113498266Sopenharmony_ci/* 11213498266Sopenharmony_ci** Following section applies even when CURLDEBUG is not defined. 11313498266Sopenharmony_ci*/ 11413498266Sopenharmony_ci 11513498266Sopenharmony_ci#undef fake_sclose 11613498266Sopenharmony_ci 11713498266Sopenharmony_ci#ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */ 11813498266Sopenharmony_ci/* 11913498266Sopenharmony_ci * The following memory function replacement typedef's are COPIED from 12013498266Sopenharmony_ci * curl/curl.h and MUST match the originals. We copy them to avoid having to 12113498266Sopenharmony_ci * include curl/curl.h here. We avoid that include since it includes stdio.h 12213498266Sopenharmony_ci * and other headers that may get messed up with defines done here. 12313498266Sopenharmony_ci */ 12413498266Sopenharmony_citypedef void *(*curl_malloc_callback)(size_t size); 12513498266Sopenharmony_citypedef void (*curl_free_callback)(void *ptr); 12613498266Sopenharmony_citypedef void *(*curl_realloc_callback)(void *ptr, size_t size); 12713498266Sopenharmony_citypedef char *(*curl_strdup_callback)(const char *str); 12813498266Sopenharmony_citypedef void *(*curl_calloc_callback)(size_t nmemb, size_t size); 12913498266Sopenharmony_ci#define CURL_DID_MEMORY_FUNC_TYPEDEFS 13013498266Sopenharmony_ci#endif 13113498266Sopenharmony_ci 13213498266Sopenharmony_ciextern curl_malloc_callback Curl_cmalloc; 13313498266Sopenharmony_ciextern curl_free_callback Curl_cfree; 13413498266Sopenharmony_ciextern curl_realloc_callback Curl_crealloc; 13513498266Sopenharmony_ciextern curl_strdup_callback Curl_cstrdup; 13613498266Sopenharmony_ciextern curl_calloc_callback Curl_ccalloc; 13713498266Sopenharmony_ci#if defined(_WIN32) && defined(UNICODE) 13813498266Sopenharmony_ciextern curl_wcsdup_callback Curl_cwcsdup; 13913498266Sopenharmony_ci#endif 14013498266Sopenharmony_ci 14113498266Sopenharmony_ci#ifndef CURLDEBUG 14213498266Sopenharmony_ci 14313498266Sopenharmony_ci/* 14413498266Sopenharmony_ci * libcurl's 'memory tracking' system defines strdup, malloc, calloc, 14513498266Sopenharmony_ci * realloc and free, along with others, in memdebug.h in a different 14613498266Sopenharmony_ci * way although still using memory callbacks forward declared above. 14713498266Sopenharmony_ci * When using the 'memory tracking' system (CURLDEBUG defined) we do 14813498266Sopenharmony_ci * not define here the five memory functions given that definitions 14913498266Sopenharmony_ci * from memdebug.h are the ones that shall be used. 15013498266Sopenharmony_ci */ 15113498266Sopenharmony_ci 15213498266Sopenharmony_ci#undef strdup 15313498266Sopenharmony_ci#define strdup(ptr) Curl_cstrdup(ptr) 15413498266Sopenharmony_ci#undef malloc 15513498266Sopenharmony_ci#define malloc(size) Curl_cmalloc(size) 15613498266Sopenharmony_ci#undef calloc 15713498266Sopenharmony_ci#define calloc(nbelem,size) Curl_ccalloc(nbelem, size) 15813498266Sopenharmony_ci#undef realloc 15913498266Sopenharmony_ci#define realloc(ptr,size) Curl_crealloc(ptr, size) 16013498266Sopenharmony_ci#undef free 16113498266Sopenharmony_ci#define free(ptr) Curl_cfree(ptr) 16213498266Sopenharmony_ci 16313498266Sopenharmony_ci#ifdef _WIN32 16413498266Sopenharmony_ci# ifdef UNICODE 16513498266Sopenharmony_ci# undef wcsdup 16613498266Sopenharmony_ci# define wcsdup(ptr) Curl_cwcsdup(ptr) 16713498266Sopenharmony_ci# undef _wcsdup 16813498266Sopenharmony_ci# define _wcsdup(ptr) Curl_cwcsdup(ptr) 16913498266Sopenharmony_ci# undef _tcsdup 17013498266Sopenharmony_ci# define _tcsdup(ptr) Curl_cwcsdup(ptr) 17113498266Sopenharmony_ci# else 17213498266Sopenharmony_ci# undef _tcsdup 17313498266Sopenharmony_ci# define _tcsdup(ptr) Curl_cstrdup(ptr) 17413498266Sopenharmony_ci# endif 17513498266Sopenharmony_ci#endif 17613498266Sopenharmony_ci 17713498266Sopenharmony_ci#endif /* CURLDEBUG */ 17813498266Sopenharmony_ci#endif /* HEADER_CURL_MEMORY_H */ 179