Ring buffer: Fix various insert/emplace cases

This commit is contained in:
Jonathan G Rennison
2023-08-19 11:41:29 +01:00
parent 6b8994c947
commit 3f2b06fcbe

View File

@@ -528,12 +528,11 @@ private:
/* front */ /* front */
this->count += num; this->count += num;
this->head -= num; this->head -= num;
return 0; return this->head;
} else if (pos == this->head + this->count) { } else if (pos == this->head + this->count) {
/* back */ /* back */
uint32 ret = this->count;
this->count += num; this->count += num;
return ret; return pos;
} else { } else {
/* middle, move data */ /* middle, move data */
if (pos - this->head < (this->count / 2)) { if (pos - this->head < (this->count / 2)) {
@@ -557,18 +556,18 @@ private:
return insert_start; return insert_start;
} else { } else {
/* closer to the end, shuffle those forwards */ /* closer to the end, shuffle those forwards */
const uint32 after_insert = pos + num; const uint32 last_inserted = pos + num - 1;
const uint32 last = this->head + this->count - 1; const uint32 last = this->head + this->count - 1;
const uint32 new_last = last + num; const uint32 new_last = last + num;
for (uint32 idx = new_last; idx != last; idx--) { for (uint32 idx = new_last; idx != last; idx--) {
/* Move construct to move forwards into uninitialised region */ /* Move construct to move forwards into uninitialised region */
new (this->raw_ptr_at_pos(idx)) T(std::move(*(this->ptr_at_pos(idx - num)))); new (this->raw_ptr_at_pos(idx)) T(std::move(*(this->ptr_at_pos(idx - num))));
} }
for (uint32 idx = last; idx != after_insert; idx--) { for (uint32 idx = last; idx != last_inserted; idx--) {
/* Move assign to move backwards in initialised region */ /* Move assign to move forwards in initialised region */
*this->ptr_at_pos(idx) = std::move(*this->ptr_at_pos(idx - num)); *this->ptr_at_pos(idx) = std::move(*this->ptr_at_pos(idx - num));
} }
for (uint32 idx = after_insert; idx != pos; idx--) { for (uint32 idx = last_inserted; idx != pos; idx--) {
/* Destruct to leave space for inserts */ /* Destruct to leave space for inserts */
this->ptr_at_pos(idx)->~T(); this->ptr_at_pos(idx)->~T();
} }