allocator.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <map>
  3. #include <utility>
  4. #include <string>
  5. #include <iostream>
  6. #include "test_config.h"
  7. // Allocator for temporaries
  8. #if defined(POOL_ALLOCATOR)
  9. #define TMP_ALLOC pool_allocator
  10. #else
  11. #define TMP_ALLOC stat_allocator
  12. #endif
  13. #include <dsppp/memory_pool.hpp>
  14. using namespace arm_cmsis_dsp;
  15. constexpr int NBVEC_2 = 2;
  16. constexpr int NBVEC_3 = 3;
  17. constexpr int NBVEC_4 = 4;
  18. constexpr int NBVEC_8 = 8;
  19. constexpr int NBVEC_9 = 9;
  20. constexpr int NBVEC_16 = 16;
  21. constexpr int NBVEC_32 = 32;
  22. constexpr int NBVEC_44 = 44;
  23. constexpr int NBVEC_47 = 47;
  24. constexpr int NBVEC_64 = 64;
  25. constexpr int NBVEC_128 = 128;
  26. constexpr int NBVEC_256 = 256;
  27. constexpr int NBVEC_258 = 258;
  28. constexpr int NBVEC_512 = 512;
  29. constexpr int NBVEC_1024 = 1024;
  30. constexpr int NBVEC_2048 = 2048;
  31. template<int L>
  32. struct pool_allocator;
  33. #define POOL(BYTES) \
  34. constexpr int POOL_BLOCK_##BYTES = BYTES; \
  35. extern MemoryPool<POOL_BLOCK_##BYTES,user_allocator_aligned_malloc> vecPool_##BYTES;\
  36. template<> \
  37. struct pool_allocator<BYTES> { \
  38. static char* allocate () noexcept{ \
  39. return(vecPool_##BYTES.get_new_buffer()); \
  40. } \
  41. \
  42. static void destroy ( char* ptr ) noexcept { \
  43. vecPool_##BYTES.recycle_buffer(ptr); \
  44. } \
  45. \
  46. };
  47. #if defined(POOL_ALLOCATOR)
  48. #include "allocation/all.h"
  49. #endif
  50. template<>
  51. struct pool_allocator<DYNAMIC> {
  52. /* Dynamic size allocations */
  53. static char* allocate ( std::size_t sz) noexcept{
  54. return(reinterpret_cast<char*>(std::malloc(sz)));
  55. }
  56. static void destroy ( char* ptr ) noexcept {
  57. std::free(ptr);
  58. }
  59. };
  60. extern std::map<int, int> current_stats;
  61. extern std::map<int, int> max_stats;
  62. extern std::map<void*, std::size_t> current_dyn_stats;
  63. template<int L>
  64. struct stat_allocator {
  65. /* Dynamic allocations */
  66. static char* allocate ( std::size_t sz) noexcept{
  67. current_stats[-sz]++;
  68. if (current_stats[-sz]>max_stats[-sz])
  69. {
  70. max_stats[-sz] = current_stats[-sz];
  71. }
  72. void *ptr = std::malloc(sz);
  73. current_dyn_stats[ptr]=sz;
  74. return(reinterpret_cast<char*>(ptr));
  75. }
  76. /* Size known at build time */
  77. static char* allocate () noexcept{
  78. current_stats[L]++;
  79. if (current_stats[L]>max_stats[L])
  80. {
  81. max_stats[L] = current_stats[L];
  82. }
  83. return(reinterpret_cast<char*>(std::malloc(L)));
  84. }
  85. static void destroy ( char* ptr ) noexcept {
  86. if (L<0)
  87. {
  88. std::size_t sz = current_dyn_stats[ptr];
  89. current_stats[-sz]--;
  90. }
  91. else
  92. {
  93. current_stats[L]--;
  94. }
  95. std::free(ptr);
  96. }
  97. };
  98. extern void print_map(std::string comment);
  99. extern void check_current_stats();
  100. extern void reset_current_stats();