From 44f97e2de42afc3afacfff86b38552ff4f267ffd Mon Sep 17 00:00:00 2001 From: Kevin Ahrendt Date: Wed, 21 May 2025 20:05:13 +0000 Subject: [PATCH] move allocator to be a protected variable --- esphome/components/json/json_util.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/esphome/components/json/json_util.cpp b/esphome/components/json/json_util.cpp index c55ee4e401..4181f60f66 100644 --- a/esphome/components/json/json_util.cpp +++ b/esphome/components/json/json_util.cpp @@ -8,23 +8,22 @@ namespace json { static const char *const TAG = "json"; -static auto ALLOCATOR = RAMAllocator( - RAMAllocator::NONE); // Attempt to allocate in PSRAM before falling back into internal - // Build an allocator for the JSON Library using the RAMAllocator class struct SpiRamAllocator : ArduinoJson::Allocator { - void *allocate(size_t size) override { return ALLOCATOR.allocate(size); } + void *allocate(size_t size) override { return this->allocator_.allocate(size); } void deallocate(void *pointer) override { + // RAMAllocator requires passing the size of the allocated space which don't know, so use free directly free(pointer); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc) } void *reallocate(void *ptr, size_t new_size) override { - return ALLOCATOR.reallocate(static_cast(ptr), new_size); + return this->allocator_.reallocate(static_cast(ptr), new_size); } -}; -static auto DOC_ALLOCATOR = SpiRamAllocator(); + protected: + RAMAllocator allocator_{RAMAllocator(RAMAllocator::NONE)}; +}; std::string build_json(const json_build_t &f) { // Here we are allocating up to 5kb of memory, @@ -32,9 +31,9 @@ std::string build_json(const json_build_t &f) { // as we can not have a true dynamic sized document. // The excess memory is freed below with `shrinkToFit()` while (true) { - ESP_LOGV(TAG, "Attempting to allocate %zu bytes for JSON serialization", request_size); - DynamicJsonDocument json_document(request_size); - if (json_document.capacity() == 0) { + auto DOC_ALLOCATOR = SpiRamAllocator(); + JsonDocument json_document(&DOC_ALLOCATOR); + if (json_document.overflowed()) { ESP_LOGE(TAG, "Could not allocate memory for JSON document!"); return "{}"; } @@ -60,8 +59,9 @@ bool parse_json(const std::string &data, const json_parse_t &f) { // as we can not have a true dynamic sized document. // The excess memory is freed below with `shrinkToFit()` while (true) { - DynamicJsonDocument json_document(request_size); - if (json_document.capacity() == 0) { + auto DOC_ALLOCATOR = SpiRamAllocator(); + JsonDocument json_document(&DOC_ALLOCATOR); + if (json_document.overflowed()) { ESP_LOGE(TAG, "Could not allocate memory for JSON document!"); return false; }