ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
split_buffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstdlib>
5
6namespace esphome::split_buffer {
15 public:
16 SplitBuffer() = default;
18
19 // Initialize the buffer with the desired total length; no sub-buffer will be larger than `max_buffer_size`
20 bool init(size_t total_length, size_t max_buffer_size = SIZE_MAX);
21
22 // Free all allocated buffers
23 void free();
24
25 // Access operators
26 uint8_t &operator[](size_t index);
27 const uint8_t &operator[](size_t index) const;
28 void fill(uint8_t value) const;
29
30 // Pointer to the byte at `index`; `length` is set to how many bytes are contiguous from there.
31 // Returns nullptr with `length` 0 if `index` is out of range.
32 const uint8_t *get_span(size_t index, size_t &length) const;
33 uint8_t *get_span(size_t index, size_t &length);
34 // Copy `length` bytes from `data` into the buffer starting at `index`; bytes past the end are dropped.
35 void write(size_t index, const uint8_t *data, size_t length);
36
37 // Get the total length
38 size_t size() const { return this->total_length_; }
39
40 // Get buffer information
41 size_t get_buffer_count() const { return this->buffer_count_; }
42
43 // Check if successfully initialized
44 bool is_valid() const { return this->buffers_ != nullptr && this->buffer_count_ > 0; }
45
46 private:
47 uint8_t **buffers_{nullptr};
48 size_t buffer_count_{0};
49 size_t buffer_size_{0};
50 size_t total_length_{0};
51};
52
53} // namespace esphome::split_buffer
A SplitBuffer allocates a large memory buffer potentially as multiple smaller buffers to facilitate a...
bool init(size_t total_length, size_t max_buffer_size=SIZE_MAX)
void fill(uint8_t value) const
Fill the entire buffer with a single byte value.
void write(size_t index, const uint8_t *data, size_t length)
const uint8_t * get_span(size_t index, size_t &length) const
uint8_t & operator[](size_t index)
uint16_t length
Definition tt21100.cpp:0