34 lines
641 B
C++
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
|