PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/memdebug.h

http://github.com/bagder/curl
C Header | 174 lines | 108 code | 24 blank | 42 comment | 2 complexity | c7a626bef66fbb0654401851f341e9a5 MD5 | raw file
  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 - 2019, 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. extern FILE *curl_dbg_logfile;
  31. /* memory functions */
  32. CURL_EXTERN void *curl_dbg_malloc(size_t size, int line, const char *source);
  33. CURL_EXTERN void *curl_dbg_calloc(size_t elements, size_t size, int line,
  34. const char *source);
  35. CURL_EXTERN void *curl_dbg_realloc(void *ptr, size_t size, int line,
  36. const char *source);
  37. CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source);
  38. CURL_EXTERN char *curl_dbg_strdup(const char *str, int line, const char *src);
  39. #if defined(WIN32) && defined(UNICODE)
  40. CURL_EXTERN wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line,
  41. const char *source);
  42. #endif
  43. CURL_EXTERN void curl_dbg_memdebug(const char *logname);
  44. CURL_EXTERN void curl_dbg_memlimit(long limit);
  45. CURL_EXTERN void curl_dbg_log(const char *format, ...);
  46. /* file descriptor manipulators */
  47. CURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
  48. int line, const char *source);
  49. CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd,
  50. int line, const char *source);
  51. CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd,
  52. int line, const char *source);
  53. CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s, void *a, void *alen,
  54. int line, const char *source);
  55. #ifdef HAVE_SOCKETPAIR
  56. CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol,
  57. curl_socket_t socket_vector[2],
  58. int line, const char *source);
  59. #endif
  60. /* send/receive sockets */
  61. CURL_EXTERN SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
  62. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  63. SEND_TYPE_ARG3 len,
  64. SEND_TYPE_ARG4 flags, int line,
  65. const char *source);
  66. CURL_EXTERN RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd,
  67. RECV_TYPE_ARG2 buf,
  68. RECV_TYPE_ARG3 len,
  69. RECV_TYPE_ARG4 flags, int line,
  70. const char *source);
  71. /* FILE functions */
  72. CURL_EXTERN FILE *curl_dbg_fopen(const char *file, const char *mode, int line,
  73. const char *source);
  74. CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source);
  75. #ifndef MEMDEBUG_NODEFINES
  76. /* Set this symbol on the command-line, recompile all lib-sources */
  77. #undef strdup
  78. #define strdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__)
  79. #define malloc(size) curl_dbg_malloc(size, __LINE__, __FILE__)
  80. #define calloc(nbelem,size) curl_dbg_calloc(nbelem, size, __LINE__, __FILE__)
  81. #define realloc(ptr,size) curl_dbg_realloc(ptr, size, __LINE__, __FILE__)
  82. #define free(ptr) curl_dbg_free(ptr, __LINE__, __FILE__)
  83. #define send(a,b,c,d) curl_dbg_send(a,b,c,d, __LINE__, __FILE__)
  84. #define recv(a,b,c,d) curl_dbg_recv(a,b,c,d, __LINE__, __FILE__)
  85. #ifdef WIN32
  86. # ifdef UNICODE
  87. # undef wcsdup
  88. # define wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
  89. # undef _wcsdup
  90. # define _wcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
  91. # undef _tcsdup
  92. # define _tcsdup(ptr) curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
  93. # else
  94. # undef _tcsdup
  95. # define _tcsdup(ptr) curl_dbg_strdup(ptr, __LINE__, __FILE__)
  96. # endif
  97. #endif
  98. #undef socket
  99. #define socket(domain,type,protocol)\
  100. curl_dbg_socket(domain, type, protocol, __LINE__, __FILE__)
  101. #undef accept /* for those with accept as a macro */
  102. #define accept(sock,addr,len)\
  103. curl_dbg_accept(sock, addr, len, __LINE__, __FILE__)
  104. #ifdef HAVE_SOCKETPAIR
  105. #define socketpair(domain,type,protocol,socket_vector)\
  106. curl_dbg_socketpair(domain, type, protocol, socket_vector, __LINE__, __FILE__)
  107. #endif
  108. #ifdef HAVE_GETADDRINFO
  109. #if defined(getaddrinfo) && defined(__osf__)
  110. /* OSF/1 and Tru64 have getaddrinfo as a define already, so we cannot define
  111. our macro as for other platforms. Instead, we redefine the new name they
  112. define getaddrinfo to become! */
  113. #define ogetaddrinfo(host,serv,hint,res) \
  114. curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
  115. #else
  116. #undef getaddrinfo
  117. #define getaddrinfo(host,serv,hint,res) \
  118. curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
  119. #endif
  120. #endif /* HAVE_GETADDRINFO */
  121. #ifdef HAVE_FREEADDRINFO
  122. #undef freeaddrinfo
  123. #define freeaddrinfo(data) \
  124. curl_dbg_freeaddrinfo(data, __LINE__, __FILE__)
  125. #endif /* HAVE_FREEADDRINFO */
  126. /* sclose is probably already defined, redefine it! */
  127. #undef sclose
  128. #define sclose(sockfd) curl_dbg_sclose(sockfd,__LINE__,__FILE__)
  129. #define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd,__LINE__,__FILE__)
  130. #undef fopen
  131. #define fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__)
  132. #undef fdopen
  133. #define fdopen(file,mode) curl_dbg_fdopen(file,mode,__LINE__,__FILE__)
  134. #define fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__)
  135. #endif /* MEMDEBUG_NODEFINES */
  136. #endif /* CURLDEBUG */
  137. /*
  138. ** Following section applies even when CURLDEBUG is not defined.
  139. */
  140. #ifndef fake_sclose
  141. #define fake_sclose(x) Curl_nop_stmt
  142. #endif
  143. /*
  144. * Curl_safefree defined as a macro to allow MemoryTracking feature
  145. * to log free() calls at same location where Curl_safefree is used.
  146. * This macro also assigns NULL to given pointer when free'd.
  147. */
  148. #define Curl_safefree(ptr) \
  149. do { free((ptr)); (ptr) = NULL;} while(0)
  150. #endif /* HEADER_CURL_MEMDEBUG_H */