00001 #ifndef HEADER_CURL_MEMORY_H 00002 #define HEADER_CURL_MEMORY_H 00003 /*************************************************************************** 00004 * _ _ ____ _ 00005 * Project ___| | | | _ \| | 00006 * / __| | | | |_) | | 00007 * | (__| |_| | _ <| |___ 00008 * \___|\___/|_| \_\_____| 00009 * 00010 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. 00011 * 00012 * This software is licensed as described in the file COPYING, which 00013 * you should have received as part of this distribution. The terms 00014 * are also available at https://curl.haxx.se/docs/copyright.html. 00015 * 00016 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 00017 * copies of the Software, and permit persons to whom the Software is 00018 * furnished to do so, under the terms of the COPYING file. 00019 * 00020 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 00021 * KIND, either express or implied. 00022 * 00023 ***************************************************************************/ 00024 00025 /* 00026 * Nasty internal details ahead... 00027 * 00028 * File curl_memory.h must be included by _all_ *.c source files 00029 * that use memory related functions strdup, malloc, calloc, realloc 00030 * or free, and given source file is used to build libcurl library. 00031 * It should be included immediately before memdebug.h as the last files 00032 * included to avoid undesired interaction with other memory function 00033 * headers in dependent libraries. 00034 * 00035 * There is nearly no exception to above rule. All libcurl source 00036 * files in 'lib' subdirectory as well as those living deep inside 00037 * 'packages' subdirectories and linked together in order to build 00038 * libcurl library shall follow it. 00039 * 00040 * File lib/strdup.c is an exception, given that it provides a strdup 00041 * clone implementation while using malloc. Extra care needed inside 00042 * this one. TODO: revisit this paragraph and related code. 00043 * 00044 * The need for curl_memory.h inclusion is due to libcurl's feature 00045 * of allowing library user to provide memory replacement functions, 00046 * memory callbacks, at runtime with curl_global_init_mem() 00047 * 00048 * Any *.c source file used to build libcurl library that does not 00049 * include curl_memory.h and uses any memory function of the five 00050 * mentioned above will compile without any indication, but it will 00051 * trigger weird memory related issues at runtime. 00052 * 00053 * OTOH some source files from 'lib' subdirectory may additionally be 00054 * used directly as source code when using some curlx_ functions by 00055 * third party programs that don't even use libcurl at all. When using 00056 * these source files in this way it is necessary these are compiled 00057 * with CURLX_NO_MEMORY_CALLBACKS defined, in order to ensure that no 00058 * attempt of calling libcurl's memory callbacks is done from code 00059 * which can not use this machinery. 00060 * 00061 * Notice that libcurl's 'memory tracking' system works chaining into 00062 * the memory callback machinery. This implies that when compiling 00063 * 'lib' source files with CURLX_NO_MEMORY_CALLBACKS defined this file 00064 * disengages usage of libcurl's 'memory tracking' system, defining 00065 * MEMDEBUG_NODEFINES and overriding CURLDEBUG purpose. 00066 * 00067 * CURLX_NO_MEMORY_CALLBACKS takes precedence over CURLDEBUG. This is 00068 * done in order to allow building a 'memory tracking' enabled libcurl 00069 * and at the same time allow building programs which do not use it. 00070 * 00071 * Programs and libraries in 'tests' subdirectories have specific 00072 * purposes and needs, and as such each one will use whatever fits 00073 * best, depending additionally wether it links with libcurl or not. 00074 * 00075 * Caveat emptor. Proper curlx_* separation is a work in progress 00076 * the same as CURLX_NO_MEMORY_CALLBACKS usage, some adjustments may 00077 * still be required. IOW don't use them yet, there are sharp edges. 00078 */ 00079 00080 #ifdef HEADER_CURL_MEMDEBUG_H 00081 #error "Header memdebug.h shall not be included before curl_memory.h" 00082 #endif 00083 00084 #ifndef CURLX_NO_MEMORY_CALLBACKS 00085 00086 #ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */ 00087 /* 00088 * The following memory function replacement typedef's are COPIED from 00089 * curl/curl.h and MUST match the originals. We copy them to avoid having to 00090 * include curl/curl.h here. We avoid that include since it includes stdio.h 00091 * and other headers that may get messed up with defines done here. 00092 */ 00093 typedef void *(*curl_malloc_callback)(size_t size); 00094 typedef void (*curl_free_callback)(void *ptr); 00095 typedef void *(*curl_realloc_callback)(void *ptr, size_t size); 00096 typedef char *(*curl_strdup_callback)(const char *str); 00097 typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size); 00098 #define CURL_DID_MEMORY_FUNC_TYPEDEFS 00099 #endif 00100 00101 extern curl_malloc_callback Curl_cmalloc; 00102 extern curl_free_callback Curl_cfree; 00103 extern curl_realloc_callback Curl_crealloc; 00104 extern curl_strdup_callback Curl_cstrdup; 00105 extern curl_calloc_callback Curl_ccalloc; 00106 #if defined(WIN32) && defined(UNICODE) 00107 extern curl_wcsdup_callback Curl_cwcsdup; 00108 #endif 00109 00110 #ifndef CURLDEBUG 00111 00112 /* 00113 * libcurl's 'memory tracking' system defines strdup, malloc, calloc, 00114 * realloc and free, along with others, in memdebug.h in a different 00115 * way although still using memory callbacks forward declared above. 00116 * When using the 'memory tracking' system (CURLDEBUG defined) we do 00117 * not define here the five memory functions given that definitions 00118 * from memdebug.h are the ones that shall be used. 00119 */ 00120 00121 #undef strdup 00122 #define strdup(ptr) Curl_cstrdup(ptr) 00123 #undef malloc 00124 #define malloc(size) Curl_cmalloc(size) 00125 #undef calloc 00126 #define calloc(nbelem,size) Curl_ccalloc(nbelem, size) 00127 #undef realloc 00128 #define realloc(ptr,size) Curl_crealloc(ptr, size) 00129 #undef free 00130 #define free(ptr) Curl_cfree(ptr) 00131 00132 #ifdef WIN32 00133 # ifdef UNICODE 00134 # undef wcsdup 00135 # define wcsdup(ptr) Curl_cwcsdup(ptr) 00136 # undef _wcsdup 00137 # define _wcsdup(ptr) Curl_cwcsdup(ptr) 00138 # undef _tcsdup 00139 # define _tcsdup(ptr) Curl_cwcsdup(ptr) 00140 # else 00141 # undef _tcsdup 00142 # define _tcsdup(ptr) Curl_cstrdup(ptr) 00143 # endif 00144 #endif 00145 00146 #endif /* CURLDEBUG */ 00147 00148 #else /* CURLX_NO_MEMORY_CALLBACKS */ 00149 00150 #ifndef MEMDEBUG_NODEFINES 00151 #define MEMDEBUG_NODEFINES 00152 #endif 00153 00154 #endif /* CURLX_NO_MEMORY_CALLBACKS */ 00155 00156 #endif /* HEADER_CURL_MEMORY_H */