curl_memory.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, <[email protected]>, 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. #ifndef CURLDEBUG
  56. /*
  57. * libcurl's 'memory tracking' system defines strdup, malloc, calloc,
  58. * realloc and free, along with others, in memdebug.h in a different
  59. * way although still using memory callbacks forward declared above.
  60. * When using the 'memory tracking' system (CURLDEBUG defined) we do
  61. * not define here the five memory functions given that definitions
  62. * from memdebug.h are the ones that shall be used.
  63. */
  64. #undef strdup
  65. #define strdup(ptr) Curl_cstrdup(ptr)
  66. #undef malloc
  67. #define malloc(size) Curl_cmalloc(size)
  68. #undef calloc
  69. #define calloc(nbelem,size) Curl_ccalloc(nbelem, size)
  70. #undef realloc
  71. #define realloc(ptr,size) Curl_crealloc(ptr, size)
  72. #undef free
  73. #define free(ptr) Curl_cfree(ptr)
  74. #ifdef _WIN32
  75. #undef Curl_tcsdup
  76. #ifdef UNICODE
  77. #define Curl_tcsdup(ptr) Curl_wcsdup(ptr)
  78. #else
  79. #define Curl_tcsdup(ptr) Curl_cstrdup(ptr)
  80. #endif
  81. #endif /* _WIN32 */
  82. #endif /* CURLDEBUG */
  83. #endif /* HEADER_CURL_MEMORY_H */