blob: 0ba8a98229c4d0027b406ca36445938b3d0829da [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001#ifndef __AUD_RINGBUF_H__
2#define __AUD_RINGBUF_H__
3
4#include "kal_public_api.h"
5
6/*
7============================================================================================================
8------------------------------------------------------------------------------------------------------------
9|| Ring buffer handling
10------------------------------------------------------------------------------------------------------------
11============================================================================================================
12*/
13
14
15#ifndef RINGBUFFER_T
16#define RINGBUFFER_T(elemtype,size) \
17 struct \
18 { \
19 volatile kal_uint32 write; /* must be unsigned */ \
20 volatile kal_uint32 read; /* must be unsigned */ \
21 elemtype volatile queue[size]; \
22 }
23
24#define RB_SIZE( rb ) \
25 ( sizeof( rb.queue ) / sizeof( *rb.queue ) )
26
27#define RB_MASK( rb ) \
28 ( RB_SIZE( rb ) - 1 )
29
30#define RB_INIT( rb ) \
31 ( rb.read = rb.write = 0 )
32
33#define RB_COUNT( rb ) \
34 ( rb.write - rb.read )
35
36#define RB_FULL( rb ) \
37 ( RB_COUNT( rb ) >= RB_SIZE( rb ) )
38
39#define RB_EMPTY( rb ) \
40 ( rb.write == rb.read )
41
42#define RB_PUT( rb, value ) \
43{ \
44 ASSERT( !RB_FULL( rb ) ); \
45 rb.queue[ rb.write & RB_MASK( rb ) ] = value; \
46 ++rb.write; \
47}
48
49#define RB_GET( rb, value ) \
50{ \
51 ASSERT( !RB_EMPTY( rb ) ); \
52 value = rb.queue[ rb.read & RB_MASK( rb ) ]; \
53 ++rb.read; \
54}
55
56#define RB_PEEK( rb, value ) \
57{ \
58 ASSERT( !RB_EMPTY( rb ) ); \
59 value = rb.queue[ rb.read & RB_MASK( rb ) ]; \
60}
61
62#define RB_PEEK_THROUGH( rb, idx, value ) \
63{ \
64 ASSERT( (rb.read+idx) < rb.write ); \
65 value = rb.queue[ (rb.read+idx) & RB_MASK( rb ) ]; \
66}
67
68#define RB_CONSUME( rb ) \
69{ \
70 ASSERT( !RB_EMPTY( rb ) ); \
71 ++rb.read; \
72}
73
74#define RB_UNPUT( rb, value ) \
75{ \
76 ASSERT( !RB_EMPTY( rb ) ); \
77 --rb.write; \
78 value = rb.queue[ rb.write & RB_MASK( rb ) ]; \
79}
80
81#define RB_UNGET( rb, value ) \
82{ \
83 ASSERT( !RB_FULL( rb ) ); \
84 --rb.read; \
85 rb.queue[ rb.read & RB_MASK( rb ) ] = value; \
86}
87#endif /* RINGBUFFER_T */
88
89
90
91#endif //__AUD_RINGBUF_H__
92