Skip to content

Commit

Permalink
sys/chunked_ringbuffer: discard stale chunk when starting a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Dec 12, 2024
1 parent e481058 commit 982a2c1
Showing 1 changed file with 22 additions and 15 deletions.
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

0 comments on commit 982a2c1

Please sign in to comment.