1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-12 22:00:30 +01:00

[core] add reallocation support to RAMAllocator (#8390)

This commit is contained in:
Kevin Ahrendt 2025-03-12 15:04:05 -05:00 committed by GitHub
parent 64d1d93fe0
commit bd853e6883
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -719,6 +719,25 @@ template<class T> class RAMAllocator {
return ptr;
}
T *reallocate(T *p, size_t n) { return this->reallocate(p, n, sizeof(T)); }
T *reallocate(T *p, size_t n, size_t manual_size) {
size_t size = n * sizeof(T);
T *ptr = nullptr;
#ifdef USE_ESP32
if (this->flags_ & Flags::ALLOC_EXTERNAL) {
ptr = static_cast<T *>(heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
}
if (ptr == nullptr && this->flags_ & Flags::ALLOC_INTERNAL) {
ptr = static_cast<T *>(heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
}
#else
// Ignore ALLOC_EXTERNAL/ALLOC_INTERNAL flags if external allocation is not supported
ptr = static_cast<T *>(realloc(p, size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
#endif
return ptr;
}
void deallocate(T *p, size_t n) {
free(p); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
}