curl_memory.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef HEADER_CURL_MEMORY_H
  2. #define HEADER_CURL_MEMORY_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  24. /*
  25. * Nasty internal details ahead...
  26. *
  27. * File curl_memory.h must be included by _all_ *.c source files
  28. * that use memory related functions strdup, malloc, calloc, realloc
  29. * or free, and given source file is used to build libcurl library.
  30. * It should be included immediately before memdebug.h as the last files
  31. * included to avoid undesired interaction with other memory function
  32. * headers in dependent libraries.
  33. *
  34. * There is nearly no exception to above rule. All libcurl source
  35. * files in 'lib' subdirectory as well as those living deep inside
  36. * 'packages' subdirectories and linked together in order to build
  37. * libcurl library shall follow it.
  38. *
  39. * File lib/strdup.c is an exception, given that it provides a strdup
  40. * clone implementation while using malloc. Extra care needed inside
  41. * this one.
  42. *
  43. * The need for curl_memory.h inclusion is due to libcurl's feature
  44. * of allowing library user to provide memory replacement functions,
  45. * memory callbacks, at runtime with curl_global_init_mem()
  46. *
  47. * Any *.c source file used to build libcurl library that does not
  48. * include curl_memory.h and uses any memory function of the five
  49. * mentioned above will compile without any indication, but it will
  50. * trigger weird memory related issues at runtime.
  51. *
  52. * OTOH some source files from 'lib' subdirectory may additionally be
  53. * used directly as source code when using some curlx_ functions by
  54. * third party programs that don't even use libcurl at all. When using
  55. * these source files in this way it is necessary these are compiled
  56. * with CURLX_NO_MEMORY_CALLBACKS defined, in order to ensure that no
  57. * attempt of calling libcurl's memory callbacks is done from code
  58. * which can not use this machinery.
  59. *
  60. * Notice that libcurl's 'memory tracking' system works chaining into
  61. * the memory callback machinery. This implies that when compiling
  62. * 'lib' source files with CURLX_NO_MEMORY_CALLBACKS defined this file
  63. * disengages usage of libcurl's 'memory tracking' system, defining
  64. * MEMDEBUG_NODEFINES and overriding CURLDEBUG purpose.
  65. *
  66. * CURLX_NO_MEMORY_CALLBACKS takes precedence over CURLDEBUG. This is
  67. * done in order to allow building a 'memory tracking' enabled libcurl
  68. * and at the same time allow building programs which do not use it.
  69. *
  70. * Programs and libraries in 'tests' subdirectories have specific
  71. * purposes and needs, and as such each one will use whatever fits
  72. * best, depending additionally whether it links with libcurl or not.
  73. *
  74. * Caveat emptor. Proper curlx_* separation is a work in progress
  75. * the same as CURLX_NO_MEMORY_CALLBACKS usage, some adjustments may
  76. * still be required. IOW don't use them yet, there are sharp edges.
  77. */
  78. #ifdef HEADER_CURL_MEMDEBUG_H
  79. #error "Header memdebug.h shall not be included before curl_memory.h"
  80. #endif
  81. #ifndef CURLX_NO_MEMORY_CALLBACKS
  82. #ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */
  83. /*
  84. * The following memory function replacement typedef's are COPIED from
  85. * curl/curl.h and MUST match the originals. We copy them to avoid having to
  86. * include curl/curl.h here. We avoid that include since it includes stdio.h
  87. * and other headers that may get messed up with defines done here.
  88. */
  89. typedef void *(*curl_malloc_callback)(size_t size);
  90. typedef void (*curl_free_callback)(void *ptr);
  91. typedef void *(*curl_realloc_callback)(void *ptr, size_t size);
  92. typedef char *(*curl_strdup_callback)(const char *str);
  93. typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size);
  94. #define CURL_DID_MEMORY_FUNC_TYPEDEFS
  95. #endif
  96. extern curl_malloc_callback Curl_cmalloc;
  97. extern curl_free_callback Curl_cfree;
  98. extern curl_realloc_callback Curl_crealloc;
  99. extern curl_strdup_callback Curl_cstrdup;
  100. extern curl_calloc_callback Curl_ccalloc;
  101. #if defined(WIN32) && defined(UNICODE)
  102. extern curl_wcsdup_callback Curl_cwcsdup;
  103. #endif
  104. #ifndef CURLDEBUG
  105. /*
  106. * libcurl's 'memory tracking' system defines strdup, malloc, calloc,
  107. * realloc and free, along with others, in memdebug.h in a different
  108. * way although still using memory callbacks forward declared above.
  109. * When using the 'memory tracking' system (CURLDEBUG defined) we do
  110. * not define here the five memory functions given that definitions
  111. * from memdebug.h are the ones that shall be used.
  112. */
  113. #undef strdup
  114. #define strdup(ptr) Curl_cstrdup(ptr)
  115. #undef malloc
  116. #define malloc(size) Curl_cmalloc(size)
  117. #undef calloc
  118. #define calloc(nbelem,size) Curl_ccalloc(nbelem, size)
  119. #undef realloc
  120. #define realloc(ptr,size) Curl_crealloc(ptr, size)
  121. #undef free
  122. #define free(ptr) Curl_cfree(ptr)
  123. #ifdef WIN32
  124. # ifdef UNICODE
  125. # undef wcsdup
  126. # define wcsdup(ptr) Curl_cwcsdup(ptr)
  127. # undef _wcsdup
  128. # define _wcsdup(ptr) Curl_cwcsdup(ptr)
  129. # undef _tcsdup
  130. # define _tcsdup(ptr) Curl_cwcsdup(ptr)
  131. # else
  132. # undef _tcsdup
  133. # define _tcsdup(ptr) Curl_cstrdup(ptr)
  134. # endif
  135. #endif
  136. #endif /* CURLDEBUG */
  137. #else /* CURLX_NO_MEMORY_CALLBACKS */
  138. #ifndef MEMDEBUG_NODEFINES
  139. #define MEMDEBUG_NODEFINES
  140. #endif
  141. #endif /* CURLX_NO_MEMORY_CALLBACKS */
  142. #endif /* HEADER_CURL_MEMORY_H */