1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org> 4 * All rights reserved. 5 */ 6 7 #ifndef _CIRBUF_H_ 8 #define _CIRBUF_H_ 9 10 #include <rte_config.h> 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 16 /** 17 * This structure is the header of a cirbuf type. 18 */ 19 struct cirbuf { 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 */ 24 char *buf; 25 }; 26 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") 30 #else 31 #define dprintf(...) (void)0 32 #endif 33 34 35 /** 36 * Init the circular buffer 37 */ 38 int cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen); 39 40 41 /** 42 * Return 1 if the circular buffer is full 43 */ 44 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len) 45 46 /** 47 * Return 1 if the circular buffer is empty 48 */ 49 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0) 50 51 /** 52 * return current size of the circular buffer (number of used elements) 53 */ 54 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len) 55 56 /** 57 * return size of the circular buffer (used + free elements) 58 */ 59 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen) 60 61 /** 62 * return the number of free elts 63 */ 64 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len) 65 66 /** 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 71 */ 72 #define CIRBUF_FOREACH(c, i, e) \ 73 for ( i=0, e=(c)->buf[(c)->start] ; \ 74 i<((c)->len) ; \ 75 i ++, e=(c)->buf[((c)->start+i)%((c)->maxlen)]) 76 77 78 /** 79 * Add a character at head of the circular buffer. Return 0 on success, or 80 * a negative value on error. 81 */ 82 int cirbuf_add_head_safe(struct cirbuf *cbuf, char c); 83 84 /** 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. 87 */ 88 void cirbuf_add_head(struct cirbuf *cbuf, char c); 89 90 /** 91 * Add a character at tail of the circular buffer. Return 0 on success, or 92 * a negative value on error. 93 */ 94 int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c); 95 96 /** 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. 99 */ 100 void cirbuf_add_tail(struct cirbuf *cbuf, char c); 101 102 /** 103 * Remove a char at the head of the circular buffer. Return 0 on 104 * success, or a negative value on error. 105 */ 106 int cirbuf_del_head_safe(struct cirbuf *cbuf); 107 108 /** 109 * Remove a char at the head of the circular buffer. You _must_ check 110 * that buffer is not empty before calling the function. 111 */ 112 void cirbuf_del_head(struct cirbuf *cbuf); 113 114 /** 115 * Remove a char at the tail of the circular buffer. Return 0 on 116 * success, or a negative value on error. 117 */ 118 int cirbuf_del_tail_safe(struct cirbuf *cbuf); 119 120 /** 121 * Remove a char at the tail of the circular buffer. You _must_ check 122 * that buffer is not empty before calling the function. 123 */ 124 void cirbuf_del_tail(struct cirbuf *cbuf); 125 126 /** 127 * Return the head of the circular buffer. You _must_ check that 128 * buffer is not empty before calling the function. 129 */ 130 char cirbuf_get_head(struct cirbuf *cbuf); 131 132 /** 133 * Return the tail of the circular buffer. You _must_ check that 134 * buffer is not empty before calling the function. 135 */ 136 char cirbuf_get_tail(struct cirbuf *cbuf); 137 138 /** 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. 142 */ 143 int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n); 144 145 /** 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. 149 */ 150 int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n); 151 152 /** 153 * Remove chars at the head of the circular buffer. Return 0 on 154 * success, or a negative value on error. 155 */ 156 int cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size); 157 158 /** 159 * Remove chars at the tail of the circular buffer. Return 0 on 160 * success, or a negative value on error. 161 */ 162 int cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size); 163 164 /** 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 167 * chars. 168 */ 169 int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size); 170 171 /** 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 174 * chars. 175 */ 176 int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size); 177 178 179 /** 180 * Set the start of the data to the index 0 of the internal buffer. 181 */ 182 int cirbuf_align_left(struct cirbuf *cbuf); 183 184 /** 185 * Set the end of the data to the last index of the internal buffer. 186 */ 187 int cirbuf_align_right(struct cirbuf *cbuf); 188 189 #ifdef __cplusplus 190 } 191 #endif 192 193 #endif /* _CIRBUF_H_ */ 194