1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #21073 from benpicco/sys/chunked_ringbuffer-start_discard

sys/chunked_ringbuffer: discard stale chunk when starting a new one
This commit is contained in:
Marian Buschsieweke 2024-12-14 09:28:14 +00:00 committed by GitHub
commit 4d44abcc0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 15 deletions

View File

@ -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.
*
@ -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;
@ -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
*

View File

@ -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));
TEST_ASSERT(crb_add_chunk(&cb, "two", 4));
TEST_ASSERT(crb_add_chunk(&cb, "three", 6));