curl_memory.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Nasty internal details ahead...
  28. *
  29. * File curl_memory.h must be included by _all_ *.c source files
  30. * that use memory related functions strdup, malloc, calloc, realloc
  31. * or free, and given source file is used to build libcurl library.
  32. * It should be included immediately before memdebug.h as the last files
  33. * included to avoid undesired interaction with other memory function
  34. * headers in dependent libraries.
  35. *
  36. * There is nearly no exception to above rule. All libcurl source
  37. * files in 'lib' subdirectory as well as those living deep inside
  38. * 'packages' subdirectories and linked together in order to build
  39. * libcurl library shall follow it.
  40. *
  41. * File lib/strdup.c is an exception, given that it provides a strdup
  42. * clone implementation while using malloc. Extra care needed inside
  43. * this one.
  44. *
  45. * The need for curl_memory.h inclusion is due to libcurl's feature
  46. * of allowing library user to provide memory replacement functions,
  47. * memory callbacks, at runtime with curl_global_init_mem()
  48. *
  49. * Any *.c source file used to build libcurl library that does not
  50. * include curl_memory.h and uses any memory function of the five
  51. * mentioned above will compile without any indication, but it will
  52. * trigger weird memory related issues at runtime.
  53. *
  54. */
  55. #ifdef HEADER_CURL_MEMDEBUG_H
  56. /* cleanup after memdebug.h */
  57. #ifdef MEMDEBUG_NODEFINES
  58. #ifdef CURLDEBUG
  59. #undef strdup
  60. #undef malloc
  61. #undef calloc
  62. #undef realloc
  63. #undef free
  64. #undef send
  65. #undef recv
  66. #ifdef _WIN32
  67. # ifdef UNICODE
  68. # undef wcsdup
  69. # undef _wcsdup
  70. # undef _tcsdup
  71. # else
  72. # undef _tcsdup
  73. # endif
  74. #endif
  75. #undef socket
  76. #undef accept
  77. #ifdef HAVE_SOCKETPAIR
  78. #undef socketpair
  79. #endif
  80. #ifndef CURL_NO_GETADDRINFO_OVERRIDE
  81. #ifdef HAVE_GETADDRINFO
  82. #if defined(getaddrinfo) && defined(__osf__)
  83. #undef ogetaddrinfo
  84. #else
  85. #undef getaddrinfo
  86. #endif
  87. #endif /* HAVE_GETADDRINFO */
  88. #ifdef HAVE_FREEADDRINFO
  89. #undef freeaddrinfo
  90. #endif /* HAVE_FREEADDRINFO */
  91. #endif /* CURL_NO_GETADDRINFO_OVERRIDE */
  92. /* sclose is probably already defined, redefine it! */
  93. #undef sclose
  94. #undef fopen
  95. #undef fdopen
  96. #undef fclose
  97. #endif /* MEMDEBUG_NODEFINES */
  98. #endif /* CURLDEBUG */
  99. #undef HEADER_CURL_MEMDEBUG_H
  100. #endif /* HEADER_CURL_MEMDEBUG_H */
  101. /*
  102. ** Following section applies even when CURLDEBUG is not defined.
  103. */
  104. #undef fake_sclose
  105. #ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */
  106. /*
  107. * The following memory function replacement typedef's are COPIED from
  108. * curl/curl.h and MUST match the originals. We copy them to avoid having to
  109. * include curl/curl.h here. We avoid that include since it includes stdio.h
  110. * and other headers that may get messed up with defines done here.
  111. */
  112. typedef void *(*curl_malloc_callback)(size_t size);
  113. typedef void (*curl_free_callback)(void *ptr);
  114. typedef void *(*curl_realloc_callback)(void *ptr, size_t size);
  115. typedef char *(*curl_strdup_callback)(const char *str);
  116. typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size);
  117. #define CURL_DID_MEMORY_FUNC_TYPEDEFS
  118. #endif
  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. #if defined(_WIN32) && defined(UNICODE)
  125. extern curl_wcsdup_callback Curl_cwcsdup;
  126. #endif
  127. #ifndef CURLDEBUG
  128. /*
  129. * libcurl's 'memory tracking' system defines strdup, malloc, calloc,
  130. * realloc and free, along with others, in memdebug.h in a different
  131. * way although still using memory callbacks forward declared above.
  132. * When using the 'memory tracking' system (CURLDEBUG defined) we do
  133. * not define here the five memory functions given that definitions
  134. * from memdebug.h are the ones that shall be used.
  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. #ifdef _WIN32
  147. # ifdef UNICODE
  148. # undef wcsdup
  149. # define wcsdup(ptr) Curl_cwcsdup(ptr)
  150. # undef _wcsdup
  151. # define _wcsdup(ptr) Curl_cwcsdup(ptr)
  152. # undef _tcsdup
  153. # define _tcsdup(ptr) Curl_cwcsdup(ptr)
  154. # else
  155. # undef _tcsdup
  156. # define _tcsdup(ptr) Curl_cstrdup(ptr)
  157. # endif
  158. #endif
  159. #endif /* CURLDEBUG */
  160. #endif /* HEADER_CURL_MEMORY_H */