14
14
extern "C" {
15
15
#endif
16
16
17
- /** @cond INTERNAL_HIDDEN */
18
- /* The limit is used by algorithm for distinguishing between empty and full
19
- * state.
20
- */
21
- #define RING_BUFFER_MAX_SIZE 0x80000000U
22
-
23
- #define RING_BUFFER_SIZE_ASSERT_MSG \
24
- "Size too big"
25
- /** @endcond */
26
-
27
17
/**
28
18
* @file
29
19
* @defgroup ring_buffer_apis Ring Buffer APIs
@@ -34,38 +24,57 @@ extern "C" {
34
24
* @{
35
25
*/
36
26
27
+ /** @cond INTERNAL_HIDDEN */
28
+
29
+ /* The limit is used by algorithm for distinguishing between empty and full
30
+ * state.
31
+ */
32
+ #define RING_BUFFER_MAX_SIZE 0x80000000U
33
+ #define RING_BUFFER_SIZE_ASSERT_MSG \
34
+ "Size too big"
35
+
36
+ struct ring_buf_index { int32_t head , tail , base ; };
37
+
38
+ /** @endcond */
39
+
37
40
/**
38
41
* @brief A structure to represent a ring buffer
39
42
*/
40
43
struct ring_buf {
41
44
/** @cond INTERNAL_HIDDEN */
42
45
uint8_t * buffer ;
43
- int32_t put_head ;
44
- int32_t put_tail ;
45
- int32_t put_base ;
46
- int32_t get_head ;
47
- int32_t get_tail ;
48
- int32_t get_base ;
46
+ struct ring_buf_index put ;
47
+ struct ring_buf_index get ;
49
48
uint32_t size ;
50
49
/** @endcond */
51
50
};
52
51
52
+ /** @cond INTERNAL_HIDDEN */
53
+
54
+ uint32_t ring_buf_area_claim (struct ring_buf * buf , struct ring_buf_index * ring ,
55
+ uint8_t * * data , uint32_t size );
56
+ int ring_buf_area_finish (struct ring_buf * buf , struct ring_buf_index * ring ,
57
+ uint32_t size );
58
+
53
59
/**
54
60
* @brief Function to force ring_buf internal states to given value
55
61
*
56
62
* Any value other than 0 makes sense only in validation testing context.
57
63
*/
58
64
static inline void ring_buf_internal_reset (struct ring_buf * buf , int32_t value )
59
65
{
60
- buf -> put_head = buf -> put_tail = buf -> put_base = value ;
61
- buf -> get_head = buf -> get_tail = buf -> get_base = value ;
66
+ buf -> put . head = buf -> put . tail = buf -> put . base = value ;
67
+ buf -> get . head = buf -> get . tail = buf -> get . base = value ;
62
68
}
63
69
70
+ /** @endcond */
71
+
64
72
#define RING_BUF_INIT (buf , size8 ) \
65
73
{ \
66
74
.buffer = buf, \
67
75
.size = size8, \
68
76
}
77
+
69
78
/**
70
79
* @brief Define and initialize a ring buffer for byte data.
71
80
*
@@ -196,7 +205,7 @@ static inline void ring_buf_item_init(struct ring_buf *buf,
196
205
*/
197
206
static inline bool ring_buf_is_empty (struct ring_buf * buf )
198
207
{
199
- return buf -> get_head == buf -> put_tail ;
208
+ return buf -> get . head == buf -> put . tail ;
200
209
}
201
210
202
211
/**
@@ -218,7 +227,7 @@ static inline void ring_buf_reset(struct ring_buf *buf)
218
227
*/
219
228
static inline uint32_t ring_buf_space_get (struct ring_buf * buf )
220
229
{
221
- return buf -> size - (buf -> put_head - buf -> get_tail );
230
+ return buf -> size - (buf -> put . head - buf -> get . tail );
222
231
}
223
232
224
233
/**
@@ -254,7 +263,7 @@ static inline uint32_t ring_buf_capacity_get(struct ring_buf *buf)
254
263
*/
255
264
static inline uint32_t ring_buf_size_get (struct ring_buf * buf )
256
265
{
257
- return buf -> put_tail - buf -> get_head ;
266
+ return buf -> put . tail - buf -> get . head ;
258
267
}
259
268
260
269
/**
@@ -281,9 +290,13 @@ static inline uint32_t ring_buf_size_get(struct ring_buf *buf)
281
290
* @return Size of allocated buffer which can be smaller than requested if
282
291
* there is not enough free space or buffer wraps.
283
292
*/
284
- uint32_t ring_buf_put_claim (struct ring_buf * buf ,
285
- uint8_t * * data ,
286
- uint32_t size );
293
+ static inline uint32_t ring_buf_put_claim (struct ring_buf * buf ,
294
+ uint8_t * * data ,
295
+ uint32_t size )
296
+ {
297
+ return ring_buf_area_claim (buf , & buf -> put , data ,
298
+ MIN (size , ring_buf_space_get (buf )));
299
+ }
287
300
288
301
/**
289
302
* @brief Indicate number of bytes written to allocated buffers.
@@ -307,7 +320,10 @@ uint32_t ring_buf_put_claim(struct ring_buf *buf,
307
320
* @retval 0 Successful operation.
308
321
* @retval -EINVAL Provided @a size exceeds free space in the ring buffer.
309
322
*/
310
- int ring_buf_put_finish (struct ring_buf * buf , uint32_t size );
323
+ static inline int ring_buf_put_finish (struct ring_buf * buf , uint32_t size )
324
+ {
325
+ return ring_buf_area_finish (buf , & buf -> put , size );
326
+ }
311
327
312
328
/**
313
329
* @brief Write (copy) data to a ring buffer.
@@ -355,9 +371,13 @@ uint32_t ring_buf_put(struct ring_buf *buf, const uint8_t *data, uint32_t size);
355
371
* @return Number of valid bytes in the provided buffer which can be smaller
356
372
* than requested if there is not enough free space or buffer wraps.
357
373
*/
358
- uint32_t ring_buf_get_claim (struct ring_buf * buf ,
359
- uint8_t * * data ,
360
- uint32_t size );
374
+ static inline uint32_t ring_buf_get_claim (struct ring_buf * buf ,
375
+ uint8_t * * data ,
376
+ uint32_t size )
377
+ {
378
+ return ring_buf_area_claim (buf , & buf -> get , data ,
379
+ MIN (size , ring_buf_size_get (buf )));
380
+ }
361
381
362
382
/**
363
383
* @brief Indicate number of bytes read from claimed buffer.
@@ -381,7 +401,10 @@ uint32_t ring_buf_get_claim(struct ring_buf *buf,
381
401
* @retval 0 Successful operation.
382
402
* @retval -EINVAL Provided @a size exceeds valid bytes in the ring buffer.
383
403
*/
384
- int ring_buf_get_finish (struct ring_buf * buf , uint32_t size );
404
+ static inline int ring_buf_get_finish (struct ring_buf * buf , uint32_t size )
405
+ {
406
+ return ring_buf_area_finish (buf , & buf -> get , size );
407
+ }
385
408
386
409
/**
387
410
* @brief Read data from a ring buffer.
0 commit comments