ESPHome 2026.3.0
Loading...
Searching...
No Matches
esphome::api::APIBuffer Class Reference

Byte buffer that skips zero-initialization on resize(). More...

#include <api_buffer.h>

Public Member Functions

void clear ()
 
void reserve (size_t n) ESPHOME_ALWAYS_INLINE
 
void resize (size_t n) ESPHOME_ALWAYS_INLINE
 
uint8_t * data ()
 
const uint8_t * data () const
 
size_t size () 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).
 

Protected Member Functions

void grow_ (size_t n)
 

Protected Attributes

std::unique_ptr< uint8_t[]> data_
 
size_t size_ {0}
 
size_t capacity_ {0}
 

Detailed Description

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 36 of file api_buffer.h.

Member Function Documentation

◆ clear()

void esphome::api::APIBuffer::clear ( )
inline

Definition at line 38 of file api_buffer.h.

◆ data() [1/2]

uint8_t * esphome::api::APIBuffer::data ( )
inline

Definition at line 47 of file api_buffer.h.

◆ data() [2/2]

const uint8_t * esphome::api::APIBuffer::data ( ) const
inline

Definition at line 48 of file api_buffer.h.

◆ empty()

bool esphome::api::APIBuffer::empty ( ) const
inline

Definition at line 50 of file api_buffer.h.

◆ grow_()

void esphome::api::APIBuffer::grow_ ( size_t n)
protected

Definition at line 5 of file api_buffer.cpp.

◆ operator[]() [1/2]

uint8_t & esphome::api::APIBuffer::operator[] ( size_t i)
inline

Definition at line 51 of file api_buffer.h.

◆ operator[]() [2/2]

const uint8_t & esphome::api::APIBuffer::operator[] ( size_t i) const
inline

Definition at line 52 of file api_buffer.h.

◆ release()

void esphome::api::APIBuffer::release ( )
inline

Release all memory (equivalent to std::vector swap trick).

Definition at line 54 of file api_buffer.h.

◆ reserve()

void esphome::api::APIBuffer::reserve ( size_t n)
inline

Definition at line 39 of file api_buffer.h.

◆ resize()

void esphome::api::APIBuffer::resize ( size_t n)
inline

Definition at line 43 of file api_buffer.h.

◆ size()

size_t esphome::api::APIBuffer::size ( ) const
inline

Definition at line 49 of file api_buffer.h.

Field Documentation

◆ capacity_

size_t esphome::api::APIBuffer::capacity_ {0}
protected

Definition at line 64 of file api_buffer.h.

◆ data_

std::unique_ptr<uint8_t[]> esphome::api::APIBuffer::data_
protected

Definition at line 62 of file api_buffer.h.

◆ size_

size_t esphome::api::APIBuffer::size_ {0}
protected

Definition at line 63 of file api_buffer.h.


The documentation for this class was generated from the following files: