ESPHome 2026.9.0
Loading...
Searching...
No Matches
api_buffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5#include <memory>
6
9
10namespace esphome::api {
11
26class APIBuffer {
27 public:
28 static constexpr size_t MAX_SIZE = UINT16_MAX; // API frames carry 16 bit lengths
29 void clear() { this->size_ = 0; }
31 [[nodiscard]] inline bool reserve(size_t n) ESPHOME_ALWAYS_INLINE { return n <= this->capacity_ || this->grow_(n); }
33 [[nodiscard]] inline bool resize(size_t n) ESPHOME_ALWAYS_INLINE { return this->reserve_and_resize(n, n); }
37 [[nodiscard]] inline bool reserve_and_resize(size_t reserve_size, size_t new_size) ESPHOME_ALWAYS_INLINE {
38 if (!this->reserve(std::max(reserve_size, new_size)))
39 return false;
40 this->size_ = static_cast<uint16_t>(new_size);
41 return true;
42 }
44 [[nodiscard]] uint8_t *append(size_t n);
46 void drop_front(size_t drop) {
47#ifdef ESPHOME_DEBUG_API
48 this->debug_check_drop_(drop);
49#endif
50 this->size_ -= drop;
51 std::memmove(this->data_.get(), this->data_.get() + drop, this->size_);
52 }
53 uint8_t *data() { return this->data_.get(); }
54 const uint8_t *data() const { return this->data_.get(); }
55 size_t size() const { return this->size_; }
56 size_t capacity() const { return this->capacity_; }
57 bool empty() const { return this->size_ == 0; }
58 uint8_t &operator[](size_t i) { return this->data_[i]; }
59 const uint8_t &operator[](size_t i) const { return this->data_[i]; }
61 void release() {
62 this->data_.reset();
63 this->size_ = 0;
64 this->capacity_ = 0;
65 }
66
67 protected:
68 bool grow_(size_t n);
69#ifdef ESPHOME_DEBUG_API
70 void debug_check_drop_(size_t drop) const;
71#endif
72 // RAMAllocator: PSRAM when available, and it reports failure where
73 // new (std::nothrow) still aborts on ESP-IDF without exceptions
75 uint16_t size_{0};
76 uint16_t capacity_{0};
77};
78
79} // namespace esphome::api
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:26
void drop_front(size_t drop)
Drop the first drop bytes, sliding the rest down. Precondition: drop <= size().
Definition api_buffer.h:46
const uint8_t * data() const
Definition api_buffer.h:54
void release()
Release all memory (equivalent to std::vector swap trick).
Definition api_buffer.h:61
const uint8_t & operator[](size_t i) const
Definition api_buffer.h:59
uint8_t * append(size_t n)
Grow by n bytes; returns the new bytes, or nullptr on allocation failure.
size_t size() const
Definition api_buffer.h:55
RAMUniquePtr< uint8_t[]> data_
Definition api_buffer.h:74
uint8_t & operator[](size_t i)
Definition api_buffer.h:58
size_t capacity() const
Definition api_buffer.h:56
bool reserve(size_t n) ESPHOME_ALWAYS_INLINE
Returns false if allocation fails; the buffer is left unchanged.
Definition api_buffer.h:31
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
bool reserve_and_resize(size_t reserve_size, size_t new_size) ESPHOME_ALWAYS_INLINE
Reserve capacity for max(reserve_size, new_size) bytes, then set size to new_size.
Definition api_buffer.h:37
void debug_check_drop_(size_t drop) const
Definition api_buffer.cpp:9
static constexpr size_t MAX_SIZE
Definition api_buffer.h:28
std::unique_ptr< T, RAMDeleter< T > > RAMUniquePtr
unique_ptr over RAMAllocator storage
Definition helpers.h:2131