ESPHome 2026.3.0
Loading...
Searching...
No Matches
download_buffer.cpp
Go to the documentation of this file.
1#include "download_buffer.h"
2#include "esphome/core/log.h"
3#include <cstring>
4
6
7static const char *const TAG = "online_image.download_buffer";
8
10 RAMAllocator<uint8_t> allocator;
11 this->buffer_ = allocator.allocate(size);
12 this->reset();
13 if (!this->buffer_) {
14 ESP_LOGE(TAG, "Initial allocation of download buffer failed!");
15 this->size_ = 0;
16 }
17}
18
19uint8_t *DownloadBuffer::data(size_t offset) {
20 if (offset > this->size_) {
21 ESP_LOGE(TAG, "Tried to access beyond download buffer bounds!!!");
22 return this->buffer_;
23 }
24 return this->buffer_ + offset;
25}
26
27size_t DownloadBuffer::read(size_t len) {
28 if (len >= this->unread_) {
29 this->unread_ = 0;
30 return 0;
31 }
32 this->unread_ -= len;
33 memmove(this->data(), this->data(len), this->unread_);
34 return this->unread_;
35}
36
38 if (this->size_ >= size) {
39 // Avoid useless reallocations; if the buffer is big enough, don't reallocate.
40 return this->size_;
41 }
42 RAMAllocator<uint8_t> allocator;
43 allocator.deallocate(this->buffer_, this->size_);
44 this->buffer_ = allocator.allocate(size);
45 this->reset();
46 if (this->buffer_) {
47 this->size_ = size;
48 return size;
49 } else {
50 ESP_LOGE(TAG, "allocation of %zu bytes failed. Biggest block in heap: %zu Bytes", size,
51 allocator.get_max_free_block_size());
52 this->size_ = 0;
53 return 0;
54 }
55}
56
57} // namespace esphome::online_image
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:1899
void deallocate(T *p, size_t n)
Definition helpers.h:1954
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:1982
T * allocate(size_t n)
Definition helpers.h:1916
size_t unread_
Total number of downloaded bytes not yet read.
std::string size_t len
Definition helpers.h:892
size_t size
Definition helpers.h:929