Files
bolar_2/main/core/psram_object.h
2026-11-27 16:38:11 +08:00

34 lines
641 B
C++

#pragma once
#include <memory>
#include <new>
#include <esp_heap_caps.h>
namespace borla {
template <typename T>
struct PsramObjectDeleter {
void operator()(T* object) const
{
if (object != nullptr) {
object->~T();
heap_caps_free(object);
}
}
};
template <typename T>
std::unique_ptr<T, PsramObjectDeleter<T>> MakePsramObject()
{
void* memory = heap_caps_malloc(
sizeof(T), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (memory == nullptr) {
return {};
}
return std::unique_ptr<T, PsramObjectDeleter<T>>(
new (memory) T());
}
} // namespace borla