|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if __has_include(<ESPBufferManager.h>) |
| 4 | +#include <ESPBufferManager.h> |
| 5 | +#define ESP_CPU_MONITOR_HAS_BUFFER_MANAGER 1 |
| 6 | +#elif __has_include(<esp_buffer_manager/buffer_manager.h>) |
| 7 | +#include <esp_buffer_manager/buffer_manager.h> |
| 8 | +#define ESP_CPU_MONITOR_HAS_BUFFER_MANAGER 1 |
| 9 | +#else |
| 10 | +#define ESP_CPU_MONITOR_HAS_BUFFER_MANAGER 0 |
| 11 | +#endif |
| 12 | + |
| 13 | +#include <cstddef> |
| 14 | +#include <cstdlib> |
| 15 | +#include <deque> |
| 16 | +#include <limits> |
| 17 | +#include <new> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +namespace cpu_monitor_allocator_detail { |
| 21 | +inline void *allocate(std::size_t bytes, bool usePSRAMBuffers) noexcept { |
| 22 | +#if ESP_CPU_MONITOR_HAS_BUFFER_MANAGER |
| 23 | + return ESPBufferManager::allocate(bytes, usePSRAMBuffers); |
| 24 | +#else |
| 25 | + (void)usePSRAMBuffers; |
| 26 | + return std::malloc(bytes); |
| 27 | +#endif |
| 28 | +} |
| 29 | + |
| 30 | +inline void deallocate(void *ptr) noexcept { |
| 31 | +#if ESP_CPU_MONITOR_HAS_BUFFER_MANAGER |
| 32 | + ESPBufferManager::deallocate(ptr); |
| 33 | +#else |
| 34 | + std::free(ptr); |
| 35 | +#endif |
| 36 | +} |
| 37 | +} // namespace cpu_monitor_allocator_detail |
| 38 | + |
| 39 | +template <typename T> |
| 40 | +class CpuMonitorAllocator { |
| 41 | + public: |
| 42 | + using value_type = T; |
| 43 | + using propagate_on_container_copy_assignment = std::true_type; |
| 44 | + using propagate_on_container_move_assignment = std::true_type; |
| 45 | + using propagate_on_container_swap = std::true_type; |
| 46 | + |
| 47 | + CpuMonitorAllocator() noexcept = default; |
| 48 | + explicit CpuMonitorAllocator(bool usePSRAMBuffers) noexcept : usePSRAMBuffers_(usePSRAMBuffers) {} |
| 49 | + |
| 50 | + template <typename U> |
| 51 | + CpuMonitorAllocator(const CpuMonitorAllocator<U> &other) noexcept : usePSRAMBuffers_(other.usePSRAMBuffers()) {} |
| 52 | + |
| 53 | + T *allocate(std::size_t n) { |
| 54 | + if (n == 0) { |
| 55 | + return nullptr; |
| 56 | + } |
| 57 | + if (n > (std::numeric_limits<std::size_t>::max() / sizeof(T))) { |
| 58 | +#if defined(__cpp_exceptions) |
| 59 | + throw std::bad_alloc(); |
| 60 | +#else |
| 61 | + std::abort(); |
| 62 | +#endif |
| 63 | + } |
| 64 | + |
| 65 | + void *memory = cpu_monitor_allocator_detail::allocate(n * sizeof(T), usePSRAMBuffers_); |
| 66 | + if (memory == nullptr) { |
| 67 | +#if defined(__cpp_exceptions) |
| 68 | + throw std::bad_alloc(); |
| 69 | +#else |
| 70 | + std::abort(); |
| 71 | +#endif |
| 72 | + } |
| 73 | + return static_cast<T *>(memory); |
| 74 | + } |
| 75 | + |
| 76 | + void deallocate(T *ptr, std::size_t) noexcept { |
| 77 | + cpu_monitor_allocator_detail::deallocate(ptr); |
| 78 | + } |
| 79 | + |
| 80 | + bool usePSRAMBuffers() const noexcept { |
| 81 | + return usePSRAMBuffers_; |
| 82 | + } |
| 83 | + |
| 84 | + template <typename U> |
| 85 | + bool operator==(const CpuMonitorAllocator<U> &other) const noexcept { |
| 86 | + return usePSRAMBuffers_ == other.usePSRAMBuffers(); |
| 87 | + } |
| 88 | + |
| 89 | + template <typename U> |
| 90 | + bool operator!=(const CpuMonitorAllocator<U> &other) const noexcept { |
| 91 | + return !(*this == other); |
| 92 | + } |
| 93 | + |
| 94 | + private: |
| 95 | + template <typename> |
| 96 | + friend class CpuMonitorAllocator; |
| 97 | + |
| 98 | + bool usePSRAMBuffers_ = false; |
| 99 | +}; |
| 100 | + |
| 101 | +template <typename T> |
| 102 | +using CpuMonitorDeque = std::deque<T, CpuMonitorAllocator<T>>; |
| 103 | + |
| 104 | +template <typename T> |
| 105 | +using CpuMonitorVector = std::vector<T, CpuMonitorAllocator<T>>; |
0 commit comments