14 if (total_length == 0) {
18 this->total_length_ = total_length;
19 size_t current_buffer_size = total_length;
25 while (current_buffer_size > 0) {
27 size_t needed_buffers = (total_length + current_buffer_size - 1) / current_buffer_size;
30 uint8_t **temp_buffers = ptr_allocator.
allocate(needed_buffers);
31 if (temp_buffers ==
nullptr) {
33 ESP_LOGE(TAG,
"Failed to allocate pointers");
38 for (
size_t i = 0; i < needed_buffers; i++) {
39 temp_buffers[i] =
nullptr;
43 bool allocation_success =
true;
44 for (
size_t i = 0; i < needed_buffers; i++) {
45 size_t this_buffer_size = current_buffer_size;
47 if (i == needed_buffers - 1 && total_length % current_buffer_size != 0) {
48 this_buffer_size = total_length % current_buffer_size;
51 temp_buffers[i] = allocator.
allocate(this_buffer_size);
52 if (temp_buffers[i] ==
nullptr) {
53 allocation_success =
false;
58 memset(temp_buffers[i], 0, this_buffer_size);
61 if (allocation_success) {
63 this->buffers_ = temp_buffers;
64 this->buffer_count_ = needed_buffers;
65 this->buffer_size_ = current_buffer_size;
66 ESP_LOGD(TAG,
"Allocated %zu * %zu bytes - %zu bytes", this->buffer_count_, this->buffer_size_,
72 for (
size_t i = 0; i < needed_buffers; i++) {
73 if (temp_buffers[i] !=
nullptr) {
80 current_buffer_size = current_buffer_size / 2;
83 ESP_LOGE(TAG,
"Failed to allocate %zu bytes", total_length);
105 if (index >= this->total_length_) {
106 ESP_LOGE(TAG,
"Out of bounds - %zu >= %zu", index, this->total_length_);
109 static uint8_t dummy = 0;
113 const auto buffer_index = index / this->buffer_size_;
114 const auto offset_in_buffer = index % this->buffer_size_;
116 return this->buffers_[buffer_index][offset_in_buffer];
130 if (this->buffer_count_ == 0)
134 for (; i != this->buffer_count_ - 1; i++) {
135 memset(this->buffers_[i], value, this->buffer_size_);
140 auto size_last = ((this->total_length_ - 1) % this->buffer_size_) + 1;
141 memset(this->buffers_[i], value, size_last);