memdebug.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef HEADER_CURL_MEMDEBUG_H
  2. #define HEADER_CURL_MEMDEBUG_H
  3. #ifdef CURLDEBUG
  4. /***************************************************************************
  5. * _ _ ____ _
  6. * Project ___| | | | _ \| |
  7. * / __| | | | |_) | |
  8. * | (__| |_| | _ <| |___
  9. * \___|\___/|_| \_\_____|
  10. *
  11. * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
  12. *
  13. * This software is licensed as described in the file COPYING, which
  14. * you should have received as part of this distribution. The terms
  15. * are also available at https://curl.haxx.se/docs/copyright.html.
  16. *
  17. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  18. * copies of the Software, and permit persons to whom the Software is
  19. * furnished to do so, under the terms of the COPYING file.
  20. *
  21. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  22. * KIND, either express or implied.
  23. *
  24. ***************************************************************************/
  25. /*
  26. * CAUTION: this header is designed to work when included by the app-side
  27. * as well as the library. Do not mix with library internals!
  28. */
  29. #define CURL_MT_LOGFNAME_BUFSIZE 512
  30. #define logfile curl_debuglogfile
  31. extern FILE *logfile;
  32. /* memory functions */
  33. CURL_EXTERN void *curl_domalloc(size_t size, int line, const char *source);
  34. CURL_EXTERN void *curl_docalloc(size_t elements, size_t size, int line,
  35. const char *source);
  36. CURL_EXTERN void *curl_dorealloc(void *ptr, size_t size, int line,
  37. const char *source);
  38. CURL_EXTERN void curl_dofree(void *ptr, int line, const char *source);
  39. CURL_EXTERN char *curl_dostrdup(const char *str, int line, const char *source);
  40. #if defined(WIN32) && defined(UNICODE)
  41. CURL_EXTERN wchar_t *curl_dowcsdup(const wchar_t *str, int line,
  42. const char *source);
  43. #endif
  44. CURL_EXTERN void curl_memdebug(const char *logname);
  45. CURL_EXTERN void curl_memlimit(long limit);
  46. CURL_EXTERN void curl_memlog(const char *format, ...);
  47. /* file descriptor manipulators */
  48. CURL_EXTERN curl_socket_t curl_socket(int domain, int type, int protocol,
  49. int line, const char *source);
  50. CURL_EXTERN void curl_mark_sclose(curl_socket_t sockfd,
  51. int line, const char *source);
  52. CURL_EXTERN int curl_sclose(curl_socket_t sockfd,
  53. int line, const char *source);
  54. CURL_EXTERN curl_socket_t curl_accept(curl_socket_t s, void *a, void *alen,
  55. int line, const char *source);
  56. #ifdef HAVE_SOCKETPAIR
  57. CURL_EXTERN int curl_socketpair(int domain, int type, int protocol,
  58. curl_socket_t socket_vector[2],
  59. int line, const char *source);
  60. #endif
  61. /* send/receive sockets */
  62. CURL_EXTERN SEND_TYPE_RETV curl_dosend(SEND_TYPE_ARG1 sockfd,
  63. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  64. SEND_TYPE_ARG3 len,
  65. SEND_TYPE_ARG4 flags, int line,
  66. const char *source);
  67. CURL_EXTERN RECV_TYPE_RETV curl_dorecv(RECV_TYPE_ARG1 sockfd,
  68. RECV_TYPE_ARG2 buf, RECV_TYPE_ARG3 len,
  69. RECV_TYPE_ARG4 flags, int line,
  70. const char *source);
  71. /* FILE functions */
  72. CURL_EXTERN FILE *curl_fopen(const char *file, const char *mode, int line,
  73. const char *source);
  74. #ifdef HAVE_FDOPEN
  75. CURL_EXTERN FILE *curl_fdopen(int filedes, const char *mode, int line,
  76. const char *source);
  77. #endif
  78. CURL_EXTERN int curl_fclose(FILE *file, int line, const char *source);
  79. #ifndef MEMDEBUG_NODEFINES
  80. /* Set this symbol on the command-line, recompile all lib-sources */
  81. #undef strdup
  82. #define strdup(ptr) curl_dostrdup(ptr, __LINE__, __FILE__)
  83. #define malloc(size) curl_domalloc(size, __LINE__, __FILE__)
  84. #define calloc(nbelem,size) curl_docalloc(nbelem, size, __LINE__, __FILE__)
  85. #define realloc(ptr,size) curl_dorealloc(ptr, size, __LINE__, __FILE__)
  86. #define free(ptr) curl_dofree(ptr, __LINE__, __FILE__)
  87. #define send(a,b,c,d) curl_dosend(a,b,c,d, __LINE__, __FILE__)
  88. #define recv(a,b,c,d) curl_dorecv(a,b,c,d, __LINE__, __FILE__)
  89. #ifdef WIN32
  90. # ifdef UNICODE
  91. # undef wcsdup
  92. # define wcsdup(ptr) curl_dowcsdup(ptr, __LINE__, __FILE__)
  93. # undef _wcsdup
  94. # define _wcsdup(ptr) curl_dowcsdup(ptr, __LINE__, __FILE__)
  95. # undef _tcsdup
  96. # define _tcsdup(ptr) curl_dowcsdup(ptr, __LINE__, __FILE__)
  97. # else
  98. # undef _tcsdup
  99. # define _tcsdup(ptr) curl_dostrdup(ptr, __LINE__, __FILE__)
  100. # endif
  101. #endif
  102. #undef socket
  103. #define socket(domain,type,protocol)\
  104. curl_socket(domain, type, protocol, __LINE__, __FILE__)
  105. #undef accept /* for those with accept as a macro */
  106. #define accept(sock,addr,len)\
  107. curl_accept(sock, addr, len, __LINE__, __FILE__)
  108. #ifdef HAVE_SOCKETPAIR
  109. #define socketpair(domain,type,protocol,socket_vector)\
  110. curl_socketpair(domain, type, protocol, socket_vector, __LINE__, __FILE__)
  111. #endif
  112. #ifdef HAVE_GETADDRINFO
  113. #if defined(getaddrinfo) && defined(__osf__)
  114. /* OSF/1 and Tru64 have getaddrinfo as a define already, so we cannot define
  115. our macro as for other platforms. Instead, we redefine the new name they
  116. define getaddrinfo to become! */
  117. #define ogetaddrinfo(host,serv,hint,res) \
  118. curl_dogetaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
  119. #else
  120. #undef getaddrinfo
  121. #define getaddrinfo(host,serv,hint,res) \
  122. curl_dogetaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
  123. #endif
  124. #endif /* HAVE_GETADDRINFO */
  125. #ifdef HAVE_FREEADDRINFO
  126. #undef freeaddrinfo
  127. #define freeaddrinfo(data) \
  128. curl_dofreeaddrinfo(data, __LINE__, __FILE__)
  129. #endif /* HAVE_FREEADDRINFO */
  130. /* sclose is probably already defined, redefine it! */
  131. #undef sclose
  132. #define sclose(sockfd) curl_sclose(sockfd,__LINE__,__FILE__)
  133. #define fake_sclose(sockfd) curl_mark_sclose(sockfd,__LINE__,__FILE__)
  134. #undef fopen
  135. #define fopen(file,mode) curl_fopen(file,mode,__LINE__,__FILE__)
  136. #undef fdopen
  137. #define fdopen(file,mode) curl_fdopen(file,mode,__LINE__,__FILE__)
  138. #define fclose(file) curl_fclose(file,__LINE__,__FILE__)
  139. #endif /* MEMDEBUG_NODEFINES */
  140. #endif /* CURLDEBUG */
  141. /*
  142. ** Following section applies even when CURLDEBUG is not defined.
  143. */
  144. #ifndef fake_sclose
  145. #define fake_sclose(x) Curl_nop_stmt
  146. #endif
  147. /*
  148. * Curl_safefree defined as a macro to allow MemoryTracking feature
  149. * to log free() calls at same location where Curl_safefree is used.
  150. * This macro also assigns NULL to given pointer when free'd.
  151. */
  152. #define Curl_safefree(ptr) \
  153. do { free((ptr)); (ptr) = NULL;} WHILE_FALSE
  154. #endif /* HEADER_CURL_MEMDEBUG_H */