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