1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
10 #include <rte_config.h>
17 * This structure is the header of a cirbuf type.
20 unsigned int maxlen; /**< total len of the fifo (number of elements) */
21 unsigned int start; /**< indice of the first elt */
22 unsigned int end; /**< indice of the last elt */
23 unsigned int len; /**< current len of fifo */
27 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
28 #define dprintf_(fmt, ...) printf("line %3.3d - " fmt "%.0s", __LINE__, __VA_ARGS__)
29 #define dprintf(...) dprintf_(__VA_ARGS__, "dummy")
31 #define dprintf(...) (void)0
36 * Init the circular buffer
38 int cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen);
42 * Return 1 if the circular buffer is full
44 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len)
47 * Return 1 if the circular buffer is empty
49 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0)
52 * return current size of the circular buffer (number of used elements)
54 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len)
57 * return size of the circular buffer (used + free elements)
59 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
62 * return the number of free elts
64 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
67 * Iterator for a circular buffer
68 * c: struct cirbuf pointer
69 * i: an integer type internally used in the macro
70 * e: char that takes the value for each iteration
72 #define CIRBUF_FOREACH(c, i, e) \
73 for ( i=0, e=(c)->buf[(c)->start] ; \
75 i ++, e=(c)->buf[((c)->start+i)%((c)->maxlen)])
79 * Add a character at head of the circular buffer. Return 0 on success, or
80 * a negative value on error.
82 int cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
85 * Add a character at head of the circular buffer. You _must_ check that you
86 * have enough free space in the buffer before calling this func.
88 void cirbuf_add_head(struct cirbuf *cbuf, char c);
91 * Add a character at tail of the circular buffer. Return 0 on success, or
92 * a negative value on error.
94 int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
97 * Add a character at tail of the circular buffer. You _must_ check that you
98 * have enough free space in the buffer before calling this func.
100 void cirbuf_add_tail(struct cirbuf *cbuf, char c);
103 * Remove a char at the head of the circular buffer. Return 0 on
104 * success, or a negative value on error.
106 int cirbuf_del_head_safe(struct cirbuf *cbuf);
109 * Remove a char at the head of the circular buffer. You _must_ check
110 * that buffer is not empty before calling the function.
112 void cirbuf_del_head(struct cirbuf *cbuf);
115 * Remove a char at the tail of the circular buffer. Return 0 on
116 * success, or a negative value on error.
118 int cirbuf_del_tail_safe(struct cirbuf *cbuf);
121 * Remove a char at the tail of the circular buffer. You _must_ check
122 * that buffer is not empty before calling the function.
124 void cirbuf_del_tail(struct cirbuf *cbuf);
127 * Return the head of the circular buffer. You _must_ check that
128 * buffer is not empty before calling the function.
130 char cirbuf_get_head(struct cirbuf *cbuf);
133 * Return the tail of the circular buffer. You _must_ check that
134 * buffer is not empty before calling the function.
136 char cirbuf_get_tail(struct cirbuf *cbuf);
139 * Add a buffer at head of the circular buffer. 'c' is a pointer to a
140 * buffer, and n is the number of char to add. Return the number of
141 * copied bytes on success, or a negative value on error.
143 int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n);
146 * Add a buffer at tail of the circular buffer. 'c' is a pointer to a
147 * buffer, and n is the number of char to add. Return the number of
148 * copied bytes on success, or a negative value on error.
150 int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n);
153 * Remove chars at the head of the circular buffer. Return 0 on
154 * success, or a negative value on error.
156 int cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size);
159 * Remove chars at the tail of the circular buffer. Return 0 on
160 * success, or a negative value on error.
162 int cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size);
165 * Copy a maximum of 'size' characters from the head of the circular
166 * buffer to a flat one pointed by 'c'. Return the number of copied
169 int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size);
172 * Copy a maximum of 'size' characters from the tail of the circular
173 * buffer to a flat one pointed by 'c'. Return the number of copied
176 int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size);
180 * Set the start of the data to the index 0 of the internal buffer.
182 int cirbuf_align_left(struct cirbuf *cbuf);
185 * Set the end of the data to the last index of the internal buffer.
187 int cirbuf_align_right(struct cirbuf *cbuf);
193 #endif /* _CIRBUF_H_ */