libobs/util: Make minor optimization to circlebuf pops

If size is 0 after popping data from the front or back, set the
start/end points to 0 as well to ensure that any subsequent buffer
pushes start from the beginning of the buffer rather than the middle of
the buffer.  Reduces potential unnecessary operations in that case.

Additionally, this fixes a bug with circulebuf_pop_back where if start
position was 0, and all the data was popped off the buffer (equal to the
capacity), the end position would be equal to the original size.  As an
example to replicate the bug, push 5, pop 5, then push 10.  The
start/end points will be invalid.

Closes jp9000/obs-studio#954
master
jp9000 2017-06-27 21:18:01 -07:00
parent 27bb2f836d
commit 8770453c09
1 changed files with 10 additions and 0 deletions

View File

@ -237,6 +237,11 @@ static inline void circlebuf_pop_front(struct circlebuf *cb, void *data,
circlebuf_peek_front(cb, data, size);
cb->size -= size;
if (!cb->size) {
cb->start_pos = cb->end_pos = 0;
return;
}
cb->start_pos += size;
if (cb->start_pos >= cb->capacity)
cb->start_pos -= cb->capacity;
@ -248,6 +253,11 @@ static inline void circlebuf_pop_back(struct circlebuf *cb, void *data,
circlebuf_peek_front(cb, data, size);
cb->size -= size;
if (!cb->size) {
cb->start_pos = cb->end_pos = 0;
return;
}
if (cb->end_pos <= size)
cb->end_pos = cb->capacity - (size - cb->end_pos);
else