ESPHome 2026.9.0
Loading...
Searching...
No Matches
api_buffer.cpp
Go to the documentation of this file.
1#include "api_buffer.h"
2#ifdef ESPHOME_DEBUG_API
3#include "esphome/core/log.h"
4#endif
5
6namespace esphome::api {
7
8#ifdef ESPHOME_DEBUG_API
9void APIBuffer::debug_check_drop_(size_t drop) const {
10 if (drop > this->size_) {
11 ESP_LOGE("api.buffer", "drop_front: drop=%zu size=%u", drop, this->size_);
12 abort();
13 }
14}
15#endif
16
17bool APIBuffer::grow_(size_t n) {
18 if (n > MAX_SIZE)
19 return false;
20 // realloc extends in place when it can, avoiding the copy
21 uint8_t *grown = RAMAllocator<uint8_t>().reallocate(this->data_.get(), n);
22 if (grown == nullptr)
23 return false;
24 (void) this->data_.release(); // realloc already freed or reused the old block
25 this->data_.reset(grown);
26 this->capacity_ = n;
27 return true;
28}
29
30uint8_t *APIBuffer::append(size_t n) {
31 const size_t old_size = this->size_;
32 if (!this->resize(old_size + n))
33 return nullptr;
34 return this->data_.get() + old_size;
35}
36
37} // namespace esphome::api
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2142
T * reallocate(T *p, size_t n)
Definition helpers.h:2184
uint8_t * append(size_t n)
Grow by n bytes; returns the new bytes, or nullptr on allocation failure.
RAMUniquePtr< uint8_t[]> data_
Definition api_buffer.h:74
bool resize(size_t n) ESPHOME_ALWAYS_INLINE
Returns false if allocation fails; the buffer is left unchanged. No zero-fill.
Definition api_buffer.h:33
void debug_check_drop_(size_t drop) const
Definition api_buffer.cpp:9
static constexpr size_t MAX_SIZE
Definition api_buffer.h:28