1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
15 * This structure is the header of a cirbuf type.
18 unsigned int maxlen; /**< total len of the fifo (number of elements) */
19 unsigned int start; /**< indice of the first elt */
20 unsigned int end; /**< indice of the last elt */
21 unsigned int len; /**< current len of fifo */
25 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
26 #define dprintf_(fmt, ...) printf("line %3.3d - " fmt "%.0s", __LINE__, __VA_ARGS__)
27 #define dprintf(...) dprintf_(__VA_ARGS__, "dummy")
29 #define dprintf(...) (void)0
34 * Init the circular buffer
36 int cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen);
40 * Return 1 if the circular buffer is full
42 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len)
45 * Return 1 if the circular buffer is empty
47 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0)
50 * return current size of the circular buffer (number of used elements)
52 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len)
55 * return size of the circular buffer (used + free elements)
57 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
60 * return the number of free elts
62 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
65 * Iterator for a circular buffer
66 * c: struct cirbuf pointer
67 * i: an integer type internally used in the macro
68 * e: char that takes the value for each iteration
70 #define CIRBUF_FOREACH(c, i, e) \
71 for ( i=0, e=(c)->buf[(c)->start] ; \
73 i ++, e=(c)->buf[((c)->start+i)%((c)->maxlen)])
77 * Add a character at head of the circular buffer. Return 0 on success, or
78 * a negative value on error.
80 int cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
83 * Add a character at head of the circular buffer. You _must_ check that you
84 * have enough free space in the buffer before calling this func.
86 void cirbuf_add_head(struct cirbuf *cbuf, char c);
89 * Add a character at tail of the circular buffer. Return 0 on success, or
90 * a negative value on error.
92 int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
95 * Add a character at tail of the circular buffer. You _must_ check that you
96 * have enough free space in the buffer before calling this func.
98 void cirbuf_add_tail(struct cirbuf *cbuf, char c);
101 * Remove a char at the head of the circular buffer. Return 0 on
102 * success, or a negative value on error.
104 int cirbuf_del_head_safe(struct cirbuf *cbuf);
107 * Remove a char at the head of the circular buffer. You _must_ check
108 * that buffer is not empty before calling the function.
110 void cirbuf_del_head(struct cirbuf *cbuf);
113 * Remove a char at the tail of the circular buffer. Return 0 on
114 * success, or a negative value on error.
116 int cirbuf_del_tail_safe(struct cirbuf *cbuf);
119 * Remove a char at the tail of the circular buffer. You _must_ check
120 * that buffer is not empty before calling the function.
122 void cirbuf_del_tail(struct cirbuf *cbuf);
125 * Return the head of the circular buffer. You _must_ check that
126 * buffer is not empty before calling the function.
128 char cirbuf_get_head(struct cirbuf *cbuf);
131 * Return the tail of the circular buffer. You _must_ check that
132 * buffer is not empty before calling the function.
134 char cirbuf_get_tail(struct cirbuf *cbuf);
137 * Add a buffer at head of the circular buffer. 'c' is a pointer to a
138 * buffer, and n is the number of char to add. Return the number of
139 * copied bytes on success, or a negative value on error.
141 int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n);
144 * Add a buffer at tail of the circular buffer. 'c' is a pointer to a
145 * buffer, and n is the number of char to add. Return the number of
146 * copied bytes on success, or a negative value on error.
148 int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n);
151 * Remove chars at the head of the circular buffer. Return 0 on
152 * success, or a negative value on error.
154 int cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size);
157 * Remove chars at the tail of the circular buffer. Return 0 on
158 * success, or a negative value on error.
160 int cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size);
163 * Copy a maximum of 'size' characters from the head of the circular
164 * buffer to a flat one pointed by 'c'. Return the number of copied
167 int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size);
170 * Copy a maximum of 'size' characters from the tail of the circular
171 * buffer to a flat one pointed by 'c'. Return the number of copied
174 int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size);
178 * Set the start of the data to the index 0 of the internal buffer.
180 int cirbuf_align_left(struct cirbuf *cbuf);
183 * Set the end of the data to the last index of the internal buffer.
185 int cirbuf_align_right(struct cirbuf *cbuf);
191 #endif /* _CIRBUF_H_ */