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

sys/chunked_ringbuffer: discard stale chunk when starting a new one #21073

Merged
merged 2 commits into from
Dec 14, 2024
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
37 changes: 22 additions & 15 deletions sys/include/chunked_ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,26 @@ typedef void (*crb_foreach_callback_t)(void *ctx, uint8_t *bytes, size_t len);
*/
void crb_init(chunk_ringbuf_t *rb, void *buffer, size_t len);

/**
* @brief Close the current chunk
*
* @note This function is expected to be called in ISR context / with
* interrupts disabled.
*
* @param[in] rb The Ringbuffer to work on
* @param[in] valid True if the chunk is valid and should be stored
* False if the current chunk should be discarded
*
* @return true If the chunk could be stored in the valid chunk array
* @return false If there is no more space in the valid chunk array
*/
bool crb_end_chunk(chunk_ringbuf_t *rb, bool valid);

/**
* @brief Start a new chunk on the ringbuffer
*
* If an unfinished chunk already exists, it will be discarded.
*
* @note This function is expected to be called in ISR context / with
* interrupts disabled.
*
Expand All @@ -85,6 +102,11 @@ void crb_init(chunk_ringbuf_t *rb, void *buffer, size_t len);
*/
static inline bool crb_start_chunk(chunk_ringbuf_t *rb)
{
/* discard stale chunk */
if (rb->cur_start) {
crb_end_chunk(rb, false);
}

/* pointing to the start of the first chunk */
if (rb->cur == rb->protect) {
return false;
Expand Down Expand Up @@ -150,21 +172,6 @@ static inline bool crb_add_byte(chunk_ringbuf_t *rb, uint8_t b)
*/
bool crb_add_bytes(chunk_ringbuf_t *rb, const void *data, size_t len);

/**
* @brief Close the current chunk
*
* @note This function is expected to be called in ISR context / with
* interrupts disabled.
*
* @param[in] rb The Ringbuffer to work on
* @param[in] valid True if the chunk is valid and should be stored
* False if the current chunk should be discarded
*
* @return true If the chunk could be stored in the valid chunk array
* @return false If there is no more space in the valid chunk array
*/
bool crb_end_chunk(chunk_ringbuf_t *rb, bool valid);

/**
* @brief Add a complete chunk to the Ringbuffer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ static void test_crb_add_and_consume(void)

crb_init(&cb, buffer, sizeof(buffer));

/* add a chunk but don't finish it */
crb_start_chunk(&cb);
crb_add_byte(&cb, 1);
crb_add_byte(&cb, 2);
crb_add_byte(&cb, 3);

/* unfinished chunk should be silently discarded */
TEST_ASSERT(crb_add_chunk(&cb, "one", 4));
benpicco marked this conversation as resolved.
Show resolved Hide resolved
TEST_ASSERT(crb_add_chunk(&cb, "two", 4));
TEST_ASSERT(crb_add_chunk(&cb, "three", 6));
Expand Down
Loading