allocator.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "allocator.h"
  2. #define ALLOC_POOL(BYTES,NB) \
  3. MemoryPool<POOL_BLOCK_##BYTES,user_allocator_aligned_malloc> vecPool_##BYTES(NB);
  4. #if defined(POOL_ALLOCATOR)
  5. #include "allocation/all.cpp"
  6. #endif
  7. std::map<int, int> current_stats;
  8. std::map<int, int> max_stats;
  9. std::map<void*, std::size_t> current_dyn_stats;
  10. void print_map(std::string comment)
  11. {
  12. std::cout << comment << "\r\n";
  13. #if !defined(POOL_ALLOCATOR)
  14. std::size_t total_static=0;
  15. std::size_t total_dynamic=0;
  16. for (const auto v : max_stats)
  17. {
  18. // Only count allocations with size known at build time
  19. if (v.first > 0)
  20. {
  21. std::cout << "ALLOC_POOL(" << v.first << "," << v.second << "); \r\n";
  22. total_static += v.first * v.second;
  23. }
  24. }
  25. for (const auto v : max_stats)
  26. {
  27. // Only count allocations with size known at build time
  28. if (v.first > 0)
  29. {
  30. std::cout << "POOL(" << v.first << "); \r\n";
  31. }
  32. }
  33. std::cout << "\r\n";
  34. std::cout << "Total static bytes: " << total_static << std::hex << " (0x" << total_static << ")\r\n";
  35. total_dynamic = 0;
  36. std::cout << "\r\nDynamic allocations\r\n";
  37. for (const auto v : max_stats)
  38. {
  39. // Only count allocations with size known at build time
  40. if (v.first < 0)
  41. {
  42. // Count is meaningless for dynamic allocation
  43. // since we can track the destroy (destroy has no length
  44. // argument contrary to allocate and so can only get
  45. // the length from the static value).
  46. std::cout << std::dec << -v.first << " : " << v.second << "\r\n";
  47. total_dynamic += (-v.first) * v.second;
  48. }
  49. }
  50. std::cout << "Total dynamic bytes: " << total_dynamic << std::hex << " (0x" << total_dynamic << ")\r\n";
  51. std::cout << "Total bytes: " << (total_static+total_dynamic) << std::hex << " (0x" << (total_static+total_dynamic) << ")\r\n";
  52. #endif
  53. }
  54. void reset_current_stats()
  55. {
  56. #if !defined(POOL_ALLOCATOR)
  57. for (auto v : current_stats)
  58. {
  59. v.second = 0;
  60. }
  61. #endif
  62. }
  63. void check_current_stats()
  64. {
  65. #if !defined(POOL_ALLOCATOR)
  66. for (const auto v : current_stats)
  67. {
  68. if (v.second > 0)
  69. {
  70. if (v.first>0)
  71. {
  72. std::cout << "Error memory pool " << v.first << " not empty = " << v.second << "\r\n";
  73. }
  74. else
  75. {
  76. std::cout << "Error dynamic alloc " << -v.first << " not empty = " << v.second << "\r\n";
  77. }
  78. }
  79. }
  80. reset_current_stats();
  81. #endif
  82. }