From 8770453c09a5ded3858ee8aa2c7a11971b2b40fe Mon Sep 17 00:00:00 2001 From: jp9000 Date: Tue, 27 Jun 2017 21:18:01 -0700 Subject: [PATCH] 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 --- libobs/util/circlebuf.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libobs/util/circlebuf.h b/libobs/util/circlebuf.h index 111f17adb..c069d5e5f 100644 --- a/libobs/util/circlebuf.h +++ b/libobs/util/circlebuf.h @@ -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