1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-23 13:42:27 +01:00

Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston
2025-09-15 22:01:30 -05:00
2 changed files with 29 additions and 47 deletions

View File

@@ -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;
@@ -70,19 +44,6 @@ bool parse_json(const std::string &data, const json_parse_t &f) {
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks) // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
} }
// JsonBuilder implementation
JsonBuilder::JsonBuilder()
: doc_(
#ifdef USE_PSRAM
(allocator_ = std::make_unique<SpiRamAllocator>(), allocator_.get())
#else
nullptr
#endif
) {
}
JsonBuilder::~JsonBuilder() = default;
std::string JsonBuilder::serialize() { std::string JsonBuilder::serialize() {
if (doc_.overflowed()) { if (doc_.overflowed()) {
ESP_LOGE(TAG, "JSON document overflow"); ESP_LOGE(TAG, "JSON document overflow");

View File

@@ -13,6 +13,31 @@
namespace esphome { namespace esphome {
namespace json { namespace 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 allocator_.allocate(size); }
void deallocate(void *ptr) 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(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)>;
@@ -25,15 +50,9 @@ std::string build_json(const json_build_t &f);
/// Parse a JSON string and run the provided json parse function if it's valid. /// Parse a JSON string and run the provided json parse function if it's valid.
bool parse_json(const std::string &data, const json_parse_t &f); bool parse_json(const std::string &data, const json_parse_t &f);
// Forward declaration to avoid exposing implementation details
struct SpiRamAllocator;
/// Builder class for creating JSON documents without lambdas /// Builder class for creating JSON documents without lambdas
class JsonBuilder { class JsonBuilder {
public: public:
JsonBuilder();
~JsonBuilder();
JsonObject root() { JsonObject root() {
if (!root_created_) { if (!root_created_) {
root_ = doc_.to<JsonObject>(); root_ = doc_.to<JsonObject>();
@@ -46,9 +65,11 @@ class JsonBuilder {
private: private:
#ifdef USE_PSRAM #ifdef USE_PSRAM
std::unique_ptr<SpiRamAllocator> allocator_; // One heap allocation, but keeps code clean SpiRamAllocator allocator_;
#endif JsonDocument doc_{&allocator_};
#else
JsonDocument doc_; JsonDocument doc_;
#endif
JsonObject root_; JsonObject root_;
bool root_created_{false}; bool root_created_{false};
}; };