Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify circular buffer threadsafe #38

Merged
merged 2 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/c/circular_buffer/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ typedef struct circular_buf_t circular_buf_t;
typedef circular_buf_t* cbuf_handle_t;

/// Pass in a storage buffer and size, returns a circular buffer handle
/// Requires: buffer is not NULL, size > 0
/// Requires: buffer is not NULL, size > 0 (size > 1 for the threadsafe
// version, because it holds size - 1 elements)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic catch!

/// Ensures: cbuf has been created and is returned in an empty state
cbuf_handle_t circular_buf_init(uint8_t* buffer, size_t size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void retreat_pointer(cbuf_handle_t cbuf)

cbuf_handle_t circular_buf_init(uint8_t* buffer, size_t size)
{
assert(buffer && size);
assert(buffer && size > 1);

cbuf_handle_t cbuf = malloc(sizeof(circular_buf_t));
assert(cbuf);
Expand Down Expand Up @@ -150,7 +150,7 @@ bool circular_buf_empty(cbuf_handle_t cbuf)
{
assert(cbuf);

return (!circular_buf_full(cbuf) && (cbuf->head == cbuf->tail));
return cbuf->head == cbuf->tail;
}

bool circular_buf_full(circular_buf_t* cbuf)
Expand Down