chunked_ringbuffer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2021 ML!PA Consulting GmbH
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stddef.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
34 #ifndef CONFIG_CHUNK_NUM_MAX
35 #define CONFIG_CHUNK_NUM_MAX (4)
36 #endif
37 
42 typedef struct {
43  uint8_t *buffer;
44  uint8_t *buffer_end;
45  uint8_t *cur;
46  uint8_t *cur_start;
47  uint8_t *protect;
48  uint8_t *chunk_start[CONFIG_CHUNK_NUM_MAX];
49  uint16_t chunk_len[CONFIG_CHUNK_NUM_MAX];
50  uint8_t chunk_cur;
52 
60 typedef void (*crb_foreach_callback_t)(void *ctx, uint8_t *bytes, size_t len);
61 
69 void crb_init(chunk_ringbuf_t *rb, void *buffer, size_t len);
70 
84 unsigned crb_end_chunk(chunk_ringbuf_t *rb, bool valid);
85 
99 static inline bool crb_start_chunk(chunk_ringbuf_t *rb)
100 {
101  /* discard stale chunk */
102  if (rb->cur_start) {
103  crb_end_chunk(rb, false);
104  }
105 
106  /* pointing to the start of the first chunk */
107  if (rb->cur == rb->protect) {
108  return false;
109  }
110 
111  rb->cur_start = rb->cur;
112 
113  if (rb->protect == NULL) {
114  rb->protect = rb->cur_start;
115  }
116 
117  return true;
118 }
119 
134 static inline bool crb_add_byte(chunk_ringbuf_t *rb, uint8_t b)
135 {
136  /* if this is the first chunk, protect will be at start */
137  if (rb->cur == rb->protect &&
138  rb->cur != rb->cur_start) {
139  return false;
140  }
141 
142  *rb->cur = b;
143 
144  /* handle wrap around */
145  if (rb->cur == rb->buffer_end) {
146  rb->cur = rb->buffer;
147  } else {
148  ++rb->cur;
149  }
150 
151  return true;
152 }
153 
169 bool crb_add_bytes(chunk_ringbuf_t *rb, const void *data, size_t len);
170 
187 bool crb_add_chunk(chunk_ringbuf_t *rb, const void *data, size_t len);
188 
198 bool crb_get_chunk_size(chunk_ringbuf_t *rb, size_t *len);
199 
211 bool crb_peek_bytes(chunk_ringbuf_t *rb, void *dst, size_t offset, size_t len);
212 
225 bool crb_consume_chunk(chunk_ringbuf_t *rb, void *dst, size_t len);
226 
241 
242 #ifdef __cplusplus
243 }
244 #endif
245 
bool crb_peek_bytes(chunk_ringbuf_t *rb, void *dst, size_t offset, size_t len)
Get a number of bytes from the first valid chunk without consuming it.
static bool crb_add_byte(chunk_ringbuf_t *rb, uint8_t b)
Insert a byte into the current chunk.
void crb_init(chunk_ringbuf_t *rb, void *buffer, size_t len)
Initialize a Chunked Ringbuffer.
static bool crb_start_chunk(chunk_ringbuf_t *rb)
Start a new chunk on the ringbuffer.
void(* crb_foreach_callback_t)(void *ctx, uint8_t *bytes, size_t len)
Callback function for crb_chunk_foreach.
bool crb_add_bytes(chunk_ringbuf_t *rb, const void *data, size_t len)
Insert a number of bytes into the current chunk.
bool crb_get_chunk_size(chunk_ringbuf_t *rb, size_t *len)
Get the size of the first valid chunk.
bool crb_add_chunk(chunk_ringbuf_t *rb, const void *data, size_t len)
Add a complete chunk to the Ringbuffer.
#define CONFIG_CHUNK_NUM_MAX
The maximum number of chunks that can be stored in a Chunked Ringbuffer.
unsigned crb_end_chunk(chunk_ringbuf_t *rb, bool valid)
Close the current chunk.
bool crb_consume_chunk(chunk_ringbuf_t *rb, void *dst, size_t len)
Remove a chunk from the valid chunk array.
bool crb_chunk_foreach(chunk_ringbuf_t *rb, crb_foreach_callback_t func, void *ctx)
Execute a callback for each byte in the first valid chunk The callback function may be called twice i...
A chunked ringbuffer.
uint8_t chunk_cur
Index of the first valid chunk.
uint8_t * cur_start
start of the currently written chunk
uint8_t * buffer_end
last data element
uint8_t * buffer
pointer to the memory to hold the data
uint8_t * cur
current write pointer
uint8_t * protect
start of the first valid chunk