#pragma once

#include <vector>
#include "esp_heap_caps.h"

// Allocator custom per PSRAM
template <typename T>
struct PSRAMAllocator {
    using value_type = T;

    PSRAMAllocator() noexcept {}

    template <typename U>
    PSRAMAllocator(const PSRAMAllocator<U>&) noexcept {}

    T* allocate(std::size_t n) {
        void* ptr = heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM);
        if (!ptr) {
            throw std::bad_alloc();
        }
        return static_cast<T*>(ptr);
    }

    void deallocate(T* p, std::size_t) noexcept {
        heap_caps_free(p);
    }
};

template <typename T>
using PSRAMVector = std::vector<T, PSRAMAllocator<T>>;
