|
ESPHome 2026.9.0
|
Byte buffer that skips zero-initialization on resize(). More...
#include <api_buffer.h>
Public Member Functions | |
| void | clear () |
| bool | reserve (size_t n) ESPHOME_ALWAYS_INLINE |
| Returns false if allocation fails; the buffer is left unchanged. | |
| bool | resize (size_t n) ESPHOME_ALWAYS_INLINE |
| Returns false if allocation fails; the buffer is left unchanged. No zero-fill. | |
| 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. | |
| uint8_t * | append (size_t n) |
| Grow by n bytes; returns the new bytes, or nullptr on allocation failure. | |
| void | drop_front (size_t drop) |
Drop the first drop bytes, sliding the rest down. Precondition: drop <= size(). | |
| uint8_t * | data () |
| const uint8_t * | data () const |
| size_t | size () const |
| size_t | capacity () const |
| bool | empty () const |
| uint8_t & | operator[] (size_t i) |
| const uint8_t & | operator[] (size_t i) const |
| void | release () |
| Release all memory (equivalent to std::vector swap trick). | |
Static Public Attributes | |
| static constexpr size_t | MAX_SIZE = UINT16_MAX |
Protected Member Functions | |
| bool | grow_ (size_t n) |
| void | debug_check_drop_ (size_t drop) const |
Protected Attributes | |
| RAMUniquePtr< uint8_t[]> | data_ |
| uint16_t | size_ {0} |
| uint16_t | capacity_ {0} |
Byte buffer that skips zero-initialization on resize().
std::vector<uint8_t>::resize() zero-fills new bytes via memset. For the shared protobuf write buffer, every byte is overwritten by the encoder, making the zero-fill pure waste. For the receive buffer, bytes are overwritten by socket reads.
Designed for bulk clear/resize/overwrite patterns. grow_() allocates exactly the requested size (no growth factor) since callers resize to known sizes rather than appending incrementally.
Safe because: callers always write exactly the number of bytes they resize for. In the protobuf write path, debug_check_bounds_ validates writes in debug builds.
Definition at line 26 of file api_buffer.h.
|
nodiscard |
Grow by n bytes; returns the new bytes, or nullptr on allocation failure.
Definition at line 30 of file api_buffer.cpp.
|
inline |
Definition at line 56 of file api_buffer.h.
|
inline |
Definition at line 29 of file api_buffer.h.
|
inline |
Definition at line 53 of file api_buffer.h.
|
inline |
Definition at line 54 of file api_buffer.h.
|
protected |
Definition at line 9 of file api_buffer.cpp.
|
inline |
Drop the first drop bytes, sliding the rest down. Precondition: drop <= size().
Definition at line 46 of file api_buffer.h.
|
inline |
Definition at line 57 of file api_buffer.h.
|
protected |
Definition at line 17 of file api_buffer.cpp.
|
inline |
Definition at line 58 of file api_buffer.h.
|
inline |
Definition at line 59 of file api_buffer.h.
|
inline |
Release all memory (equivalent to std::vector swap trick).
Definition at line 61 of file api_buffer.h.
|
inlinenodiscard |
Returns false if allocation fails; the buffer is left unchanged.
Definition at line 31 of file api_buffer.h.
|
inlinenodiscard |
Reserve capacity for max(reserve_size, new_size) bytes, then set size to new_size.
Single grow_ check regardless of argument order. Returns false if allocation fails; the buffer is left unchanged.
Definition at line 37 of file api_buffer.h.
|
inlinenodiscard |
Returns false if allocation fails; the buffer is left unchanged. No zero-fill.
Definition at line 33 of file api_buffer.h.
|
inline |
Definition at line 55 of file api_buffer.h.
|
protected |
Definition at line 76 of file api_buffer.h.
|
protected |
Definition at line 74 of file api_buffer.h.
|
staticconstexpr |
Definition at line 28 of file api_buffer.h.
|
protected |
Definition at line 75 of file api_buffer.h.