mirror of
https://github.com/esphome/esphome.git
synced 2025-09-23 05:32:22 +01:00
cleanup
This commit is contained in:
@@ -8,32 +8,6 @@ namespace json {
|
|||||||
|
|
||||||
static const char *const TAG = "json";
|
static const char *const TAG = "json";
|
||||||
|
|
||||||
#ifdef USE_PSRAM
|
|
||||||
// Build an allocator for the JSON Library using the RAMAllocator class
|
|
||||||
// This is only compiled when PSRAM is enabled
|
|
||||||
struct SpiRamAllocator : ArduinoJson::Allocator {
|
|
||||||
void *allocate(size_t size) override { return this->allocator_.allocate(size); }
|
|
||||||
|
|
||||||
void deallocate(void *pointer) override {
|
|
||||||
// ArduinoJson's Allocator interface doesn't provide the size parameter in deallocate.
|
|
||||||
// RAMAllocator::deallocate() requires the size, which we don't have access to here.
|
|
||||||
// RAMAllocator::deallocate implementation just calls free() regardless of whether
|
|
||||||
// the memory was allocated with heap_caps_malloc or malloc.
|
|
||||||
// This is safe because ESP-IDF's heap implementation internally tracks the memory region
|
|
||||||
// and routes free() to the appropriate heap.
|
|
||||||
free(pointer); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
|
|
||||||
}
|
|
||||||
|
|
||||||
void *reallocate(void *ptr, size_t new_size) override {
|
|
||||||
return this->allocator_.reallocate(static_cast<uint8_t *>(ptr), new_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
RAMAllocator<uint8_t> allocator_{RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::NONE)};
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
std::string build_json(const json_build_t &f) {
|
std::string build_json(const json_build_t &f) {
|
||||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||||
JsonBuilder builder;
|
JsonBuilder builder;
|
||||||
@@ -71,24 +45,14 @@ bool parse_json(const std::string &data, const json_parse_t &f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JsonBuilder implementation
|
// JsonBuilder implementation
|
||||||
JsonBuilder::JsonBuilder() {
|
JsonBuilder::JsonBuilder()
|
||||||
|
: doc_(
|
||||||
#ifdef USE_PSRAM
|
#ifdef USE_PSRAM
|
||||||
// Verify our storage is large enough (and log the actual size for reference)
|
&allocator_
|
||||||
static_assert(sizeof(SpiRamAllocator) <= sizeof(allocator_storage_), "allocator_storage_ too small");
|
|
||||||
// Note: sizeof(SpiRamAllocator) is typically around 24-32 bytes on ESP32
|
|
||||||
// Use placement new to construct SpiRamAllocator in the pre-allocated storage
|
|
||||||
auto *allocator = new (allocator_storage_) SpiRamAllocator();
|
|
||||||
doc_ = JsonDocument(allocator);
|
|
||||||
#else
|
#else
|
||||||
doc_ = JsonDocument();
|
nullptr
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonBuilder::~JsonBuilder() {
|
|
||||||
#ifdef USE_PSRAM
|
|
||||||
// Explicitly call destructor for placement-new allocated object
|
|
||||||
reinterpret_cast<SpiRamAllocator *>(allocator_storage_)->~SpiRamAllocator();
|
|
||||||
#endif
|
#endif
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string JsonBuilder::serialize() {
|
std::string JsonBuilder::serialize() {
|
||||||
|
@@ -13,6 +13,24 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace json {
|
namespace json {
|
||||||
|
|
||||||
|
#ifdef USE_PSRAM
|
||||||
|
// Allocator for JSON that uses PSRAM on supported devices
|
||||||
|
struct SpiRamAllocator : ArduinoJson::Allocator {
|
||||||
|
void *allocate(size_t size) override { return allocator_.allocate(size); }
|
||||||
|
|
||||||
|
void deallocate(void *ptr) override {
|
||||||
|
free(ptr); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
|
||||||
|
}
|
||||||
|
|
||||||
|
void *reallocate(void *ptr, size_t new_size) override {
|
||||||
|
return allocator_.reallocate(static_cast<uint8_t *>(ptr), new_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
RAMAllocator<uint8_t> allocator_{RAMAllocator<uint8_t>::NONE};
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
/// Callback function typedef for parsing JsonObjects.
|
/// Callback function typedef for parsing JsonObjects.
|
||||||
using json_parse_t = std::function<bool(JsonObject)>;
|
using json_parse_t = std::function<bool(JsonObject)>;
|
||||||
|
|
||||||
@@ -29,7 +47,6 @@ bool parse_json(const std::string &data, const json_parse_t &f);
|
|||||||
class JsonBuilder {
|
class JsonBuilder {
|
||||||
public:
|
public:
|
||||||
JsonBuilder();
|
JsonBuilder();
|
||||||
~JsonBuilder();
|
|
||||||
|
|
||||||
JsonObject root() {
|
JsonObject root() {
|
||||||
if (!root_created_) {
|
if (!root_created_) {
|
||||||
@@ -43,9 +60,7 @@ class JsonBuilder {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
#ifdef USE_PSRAM
|
#ifdef USE_PSRAM
|
||||||
// Storage for SpiRamAllocator - typically around 24-32 bytes on ESP32
|
SpiRamAllocator allocator_; // Just a regular member on the stack!
|
||||||
// Static assert in .cpp file ensures this is large enough
|
|
||||||
std::aligned_storage<32, alignof(void *)>::type allocator_storage_;
|
|
||||||
#endif
|
#endif
|
||||||
JsonDocument doc_;
|
JsonDocument doc_;
|
||||||
JsonObject root_;
|
JsonObject root_;
|
||||||
|
Reference in New Issue
Block a user