memtrace.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef _TOOLKIT_MEMORY_TRACE_H__
  2. #define _TOOLKIT_MEMORY_TRACE_H__
  3. #include "config.h"
  4. #pragma once
  5. #ifdef malloc
  6. #undef malloc
  7. #endif
  8. #ifdef calloc
  9. #undef calloc
  10. #endif
  11. #ifdef realloc
  12. #undef realloc
  13. #endif
  14. #ifdef free
  15. #undef free
  16. #endif
  17. #ifdef strdup
  18. #undef strdup
  19. #endif
  20. #ifdef _strdup
  21. #undef _strdup
  22. #endif
  23. #ifndef __GNUC__
  24. #ifndef __attribute__
  25. #define __attribute__(x) /* nothing */
  26. #endif
  27. #endif
  28. #define MEMTRACE_LOCATION __FILE__, __FUNCTION__, __LINE__
  29. /** Override malloc with toolkit_malloc. */
  30. #define zalloc(x) toolkit_memtrace_zalloc(x, MEMTRACE_LOCATION)
  31. /** Override malloc with toolkit_malloc. */
  32. #define malloc(x) toolkit_memtrace_malloc(x, MEMTRACE_LOCATION)
  33. /** Override calloc with toolkit_calloc. */
  34. #define calloc(x,y) toolkit_memtrace_calloc(x, y, MEMTRACE_LOCATION)
  35. /** Override calloc with toolkit_czalloc. */
  36. #define czalloc(x,y) toolkit_memtrace_czalloc(x, y, MEMTRACE_LOCATION)
  37. /** Override realloc with toolkit_realloc. */
  38. #define realloc(x,y) toolkit_memtrace_realloc(x, y, MEMTRACE_LOCATION)
  39. /** Override free with toolkit_free. */
  40. #define free(x) toolkit_memtrace_free(x, MEMTRACE_LOCATION)
  41. /** Override strdup() with toolkit_strdup() */
  42. #define strdup(x) toolkit_memtrace_strdup(x, MEMTRACE_LOCATION)
  43. //#define _strdup(x) toolkit_memtrace_strdup(x, MEMTRACE_LOCATION)
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. TOOLKIT_API const char* tk_get_program_name();
  48. TOOLKIT_API void tk_set_program_name(const char* name);
  49. TOOLKIT_API void* toolkit_memtrace_zalloc(size_t bytes, const char* file, const char* function, int line);
  50. TOOLKIT_API void* toolkit_memtrace_malloc(size_t bytes, const char* file, const char* function, int line)
  51. __attribute__((no_instrument_function))
  52. __attribute__((malloc));
  53. TOOLKIT_API void* toolkit_memtrace_calloc(size_t elem, size_t ele_size, const char* file, const char* function, int line)
  54. __attribute__((no_instrument_function))
  55. __attribute__((malloc));
  56. TOOLKIT_API void* toolkit_memtrace_czalloc(size_t elem, size_t ele_size, const char* file, const char* function, int line);
  57. TOOLKIT_API void* toolkit_memtrace_realloc(void* space, size_t bytes, const char* file, const char* function, int line)
  58. __attribute__((no_instrument_function))
  59. __attribute__((warn_unused_result))
  60. __attribute__((malloc));
  61. TOOLKIT_API void toolkit_memtrace_free(void* pvSpace, const char* file, const char* function, int line)
  62. __attribute__((no_instrument_function));
  63. TOOLKIT_API char* toolkit_memtrace_strdup(const char* string, const char* file, const char* function, int line)
  64. __attribute__((nonnull));
  65. #ifdef __cplusplus
  66. }//extern "C" {
  67. #endif
  68. #endif /*_TOOLKIT_MEMORY_TRACE_H__*/