17 if (total_length == 0 || max_buffer_size == 0) {
21 size_t current_buffer_size = std::min(total_length, max_buffer_size);
27 while (current_buffer_size > 0) {
29 size_t needed_buffers = (total_length + current_buffer_size - 1) / current_buffer_size;
32 uint8_t **temp_buffers = ptr_allocator.
allocate(needed_buffers);
33 if (temp_buffers ==
nullptr) {
35 ESP_LOGE(TAG,
"Failed to allocate pointers");
40 for (
size_t i = 0; i < needed_buffers; i++) {
41 temp_buffers[i] =
nullptr;
45 bool allocation_success =
true;
46 for (
size_t i = 0; i < needed_buffers; i++) {
47 size_t this_buffer_size = current_buffer_size;
49 if (i == needed_buffers - 1 && total_length % current_buffer_size != 0) {
50 this_buffer_size = total_length % current_buffer_size;
53 temp_buffers[i] = allocator.
allocate(this_buffer_size);
54 if (temp_buffers[i] ==
nullptr) {
55 allocation_success =
false;
60 memset(temp_buffers[i], 0, this_buffer_size);
63 if (allocation_success) {
65 this->buffers_ = temp_buffers;
66 this->buffer_count_ = needed_buffers;
67 this->buffer_size_ = current_buffer_size;
68 this->total_length_ = total_length;
69 ESP_LOGD(TAG,
"Allocated %zu * %zu bytes - %zu bytes", this->buffer_count_, this->buffer_size_,
75 for (
size_t i = 0; i < needed_buffers; i++) {
76 if (temp_buffers[i] !=
nullptr) {
83 current_buffer_size = current_buffer_size / 2;
86 ESP_LOGE(TAG,
"Failed to allocate %zu bytes", total_length);
108 if (index >= this->total_length_) {
109 ESP_LOGE(TAG,
"Out of bounds - %zu >= %zu", index, this->total_length_);
112 static uint8_t dummy = 0;
116 const auto buffer_index = index / this->buffer_size_;
117 const auto offset_in_buffer = index % this->buffer_size_;
119 return this->buffers_[buffer_index][offset_in_buffer];
161 if (this->buffer_count_ == 0)
165 for (; i != this->buffer_count_ - 1; i++) {
166 memset(this->buffers_[i], value, this->buffer_size_);
171 auto size_last = ((this->total_length_ - 1) % this->buffer_size_) + 1;
172 memset(this->buffers_[i], value, size_last);