Skip to content

Commit

Permalink
libobs/util: Add funcs to push zeroed data to circlebufs
Browse files Browse the repository at this point in the history
Adds circlebuf_push_front_zero and circlebuf_push_back_zero to
conveniently push zeroed data to the front/back of the buffer without
having to create an intermediary buffer to accomplish the same thing.
  • Loading branch information
jp9000 committed Dec 2, 2017
1 parent 895814a commit 5d9854e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/sphinx/reference-libobs-util-circlebuf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ Circular Buffer Inline Functions

---------------------

.. function:: void circlebuf_push_back_zero(struct circlebuf *cb, size_t size)

Pushes zeroed data to the end of the circular buffer.

:param cb: The circular buffer
:param size: Size

---------------------

.. function:: void circlebuf_push_front_zero(struct circlebuf *cb, size_t size)

Pushes zeroed data to the front of the circular buffer.

:param cb: The circular buffer
:param size: Size

---------------------

.. function:: void circlebuf_peek_front(struct circlebuf *cb, void *data, size_t size)

Peeks data at the front of the circular buffer.
Expand Down
42 changes: 42 additions & 0 deletions libobs/util/circlebuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,48 @@ static inline void circlebuf_push_front(struct circlebuf *cb, const void *data,
}
}

static inline void circlebuf_push_back_zero(struct circlebuf *cb, size_t size)
{
size_t new_end_pos = cb->end_pos + size;

cb->size += size;
circlebuf_ensure_capacity(cb);

if (new_end_pos > cb->capacity) {
size_t back_size = cb->capacity - cb->end_pos;
size_t loop_size = size - back_size;

if (back_size)
memset((uint8_t*)cb->data + cb->end_pos, 0, back_size);
memset(cb->data, 0, loop_size);

new_end_pos -= cb->capacity;
} else {
memset((uint8_t*)cb->data + cb->end_pos, 0, size);
}

cb->end_pos = new_end_pos;
}

static inline void circlebuf_push_front_zero(struct circlebuf *cb, size_t size)
{
cb->size += size;
circlebuf_ensure_capacity(cb);

if (cb->start_pos < size) {
size_t back_size = size - cb->start_pos;

if (cb->start_pos)
memset(cb->data, 0, cb->start_pos);

cb->start_pos = cb->capacity - back_size;
memset((uint8_t*)cb->data + cb->start_pos, 0, back_size);
} else {
cb->start_pos -= size;
memset((uint8_t*)cb->data + cb->start_pos, 0, size);
}
}

static inline void circlebuf_peek_front(struct circlebuf *cb, void *data,
size_t size)
{
Expand Down

0 comments on commit 5d9854e

Please sign in to comment.