25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

69 lines
3.2 KiB

  1. #pragma once
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "types.hpp"
  5. #include "macros.hpp"
  6. #ifdef USE_FTB_MALLOC
  7. namespace Ftb_Malloc_Stats {
  8. u32 malloc_calls = 0;
  9. u32 free_calls = 0;
  10. u32 realloc_calls = 0;
  11. u32 calloc_calls = 0;
  12. u32 alloca_calls = 0;
  13. }
  14. #define ftb_malloc(size) (++Ftb_Malloc_Stats::malloc_calls, malloc(size))
  15. #define ftb_free(ptr) (++Ftb_Malloc_Stats::free_calls, free(ptr))
  16. #define ftb_realloc(ptr, size) (++Ftb_Malloc_Stats::realloc_calls, realloc(ptr, size))
  17. #define ftb_calloc(num, size) (++Ftb_Malloc_Stats::calloc_calls, calloc(num, size))
  18. #define ftb_alloca(size) (++Ftb_Malloc_Stats::alloca_calls, alloca(size))
  19. void print_malloc_stats() {
  20. printf("\n"
  21. "Global Malloc Stats:\n"
  22. "--------------------\n"
  23. " ftb_malloc calls: %u\n"
  24. " ftb_free calls: %u\n"
  25. " ftb_realloc calls: %u\n"
  26. " ftb_calloc calls: %u\n"
  27. " ftb_alloca calls: %u\n" ,
  28. Ftb_Malloc_Stats::malloc_calls,
  29. Ftb_Malloc_Stats::free_calls,
  30. Ftb_Malloc_Stats::realloc_calls,
  31. Ftb_Malloc_Stats::calloc_calls,
  32. Ftb_Malloc_Stats::alloca_calls);
  33. }
  34. #define profile_mallocs \
  35. MPP_DECLARE(0, u32 MPI_LABEL(profile_mallocs, old_malloc_calls) = Ftb_Malloc_Stats::malloc_calls) \
  36. MPP_DECLARE(1, u32 MPI_LABEL(profile_mallocs, old_free_calls) = Ftb_Malloc_Stats::free_calls) \
  37. MPP_DECLARE(2, u32 MPI_LABEL(profile_mallocs, old_realloc_calls) = Ftb_Malloc_Stats::realloc_calls) \
  38. MPP_DECLARE(3, u32 MPI_LABEL(profile_mallocs, old_calloc_calls) = Ftb_Malloc_Stats::calloc_calls) \
  39. MPP_DECLARE(4, u32 MPI_LABEL(profile_mallocs, old_alloca_calls) = Ftb_Malloc_Stats::alloca_calls) \
  40. MPP_AFTER(5, { \
  41. printf("\n" \
  42. "Local Malloc Stats: (%s %s %d)\n" \
  43. "-------------------\n" \
  44. " ftb_malloc calls: %u\n" \
  45. " ftb_free calls: %u\n" \
  46. " ftb_realloc calls: %u\n" \
  47. " ftb_calloc calls: %u\n" \
  48. " ftb_alloca calls: %u\n" , \
  49. __func__, __FILE__, __LINE__, \
  50. Ftb_Malloc_Stats::malloc_calls - MPI_LABEL(profile_mallocs, old_malloc_calls) , \
  51. Ftb_Malloc_Stats::free_calls - MPI_LABEL(profile_mallocs, old_free_calls) , \
  52. Ftb_Malloc_Stats::realloc_calls - MPI_LABEL(profile_mallocs, old_realloc_calls) , \
  53. Ftb_Malloc_Stats::calloc_calls - MPI_LABEL(profile_mallocs, old_calloc_calls) , \
  54. Ftb_Malloc_Stats::alloca_calls - MPI_LABEL(profile_mallocs, old_alloca_calls)); \
  55. })
  56. #else
  57. # define ftb_malloc malloc
  58. # define ftb_realloc realloc
  59. # define ftb_calloc calloc
  60. # define ftb_free free
  61. # define ftb_alloca alloca
  62. #endif