ESPHome 2026.3.0-dev
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 this->buffer_ = this->allocator_.allocate(size);
11 this->reset();
12 if (!this->buffer_) {
13 ESP_LOGE(TAG, "Initial allocation of download buffer failed!");
14 this->size_ = 0;
15 }
16}
17
18uint8_t *DownloadBuffer::data(size_t offset) {
19 if (offset > this->size_) {
20 ESP_LOGE(TAG, "Tried to access beyond download buffer bounds!!!");
21 return this->buffer_;
22 }
23 return this->buffer_ + offset;
24}
25
26size_t DownloadBuffer::read(size_t len) {
27 if (len >= this->unread_) {
28 this->unread_ = 0;
29 return 0;
30 }
31 this->unread_ -= len;
32 memmove(this->data(), this->data(len), this->unread_);
33 return this->unread_;
34}
35
37 if (this->size_ >= size) {
38 // Avoid useless reallocations; if the buffer is big enough, don't reallocate.
39 return this->size_;
40 }
41 this->allocator_.deallocate(this->buffer_, this->size_);
42 this->buffer_ = this->allocator_.allocate(size);
43 this->reset();
44 if (this->buffer_) {
45 this->size_ = size;
46 return size;
47 } else {
48 ESP_LOGE(TAG, "allocation of %zu bytes failed. Biggest block in heap: %zu Bytes", size,
50 this->size_ = 0;
51 return 0;
52 }
53}
54
55} // namespace esphome::online_image
void deallocate(T *p, size_t n)
Definition helpers.h:1723
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:1751
T * allocate(size_t n)
Definition helpers.h:1685
size_t unread_
Total number of downloaded bytes not yet read.
std::string size_t len
Definition helpers.h:692
size_t size
Definition helpers.h:729