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 #ifdef __cplusplus 11 extern "C" { 12 #endif 13 14 /** 15 * This structure is the header of a cirbuf type. 16 */ 17 struct cirbuf { 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 */ 22 char *buf; 23 }; 24 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") 28 #else 29 #define dprintf(...) (void)0 30 #endif 31 32 33 /** 34 * Init the circular buffer 35 */ 36 int cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen); 37 38 39 /** 40 * Return 1 if the circular buffer is full 41 */ 42 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len) 43 44 /** 45 * Return 1 if the circular buffer is empty 46 */ 47 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0) 48 49 /** 50 * return current size of the circular buffer (number of used elements) 51 */ 52 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len) 53 54 /** 55 * return size of the circular buffer (used + free elements) 56 */ 57 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen) 58 59 /** 60 * return the number of free elts 61 */ 62 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len) 63 64 /** 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 69 */ 70 #define CIRBUF_FOREACH(c, i, e) \ 71 for ( i=0, e=(c)->buf[(c)->start] ; \ 72 i<((c)->len) ; \ 73 i ++, e=(c)->buf[((c)->start+i)%((c)->maxlen)]) 74 75 76 /** 77 * Add a character at head of the circular buffer. Return 0 on success, or 78 * a negative value on error. 79 */ 80 int cirbuf_add_head_safe(struct cirbuf *cbuf, char c); 81 82 /** 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. 85 */ 86 void cirbuf_add_head(struct cirbuf *cbuf, char c); 87 88 /** 89 * Add a character at tail of the circular buffer. Return 0 on success, or 90 * a negative value on error. 91 */ 92 int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c); 93 94 /** 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. 97 */ 98 void cirbuf_add_tail(struct cirbuf *cbuf, char c); 99 100 /** 101 * Remove a char at the head of the circular buffer. Return 0 on 102 * success, or a negative value on error. 103 */ 104 int cirbuf_del_head_safe(struct cirbuf *cbuf); 105 106 /** 107 * Remove a char at the head of the circular buffer. You _must_ check 108 * that buffer is not empty before calling the function. 109 */ 110 void cirbuf_del_head(struct cirbuf *cbuf); 111 112 /** 113 * Remove a char at the tail of the circular buffer. Return 0 on 114 * success, or a negative value on error. 115 */ 116 int cirbuf_del_tail_safe(struct cirbuf *cbuf); 117 118 /** 119 * Remove a char at the tail of the circular buffer. You _must_ check 120 * that buffer is not empty before calling the function. 121 */ 122 void cirbuf_del_tail(struct cirbuf *cbuf); 123 124 /** 125 * Return the head of the circular buffer. You _must_ check that 126 * buffer is not empty before calling the function. 127 */ 128 char cirbuf_get_head(struct cirbuf *cbuf); 129 130 /** 131 * Return the tail of the circular buffer. You _must_ check that 132 * buffer is not empty before calling the function. 133 */ 134 char cirbuf_get_tail(struct cirbuf *cbuf); 135 136 /** 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. 140 */ 141 int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n); 142 143 /** 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. 147 */ 148 int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n); 149 150 /** 151 * Remove chars at the head of the circular buffer. Return 0 on 152 * success, or a negative value on error. 153 */ 154 int cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size); 155 156 /** 157 * Remove chars at the tail of the circular buffer. Return 0 on 158 * success, or a negative value on error. 159 */ 160 int cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size); 161 162 /** 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 165 * chars. 166 */ 167 int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size); 168 169 /** 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 172 * chars. 173 */ 174 int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size); 175 176 177 /** 178 * Set the start of the data to the index 0 of the internal buffer. 179 */ 180 int cirbuf_align_left(struct cirbuf *cbuf); 181 182 /** 183 * Set the end of the data to the last index of the internal buffer. 184 */ 185 int cirbuf_align_right(struct cirbuf *cbuf); 186 187 #ifdef __cplusplus 188 } 189 #endif 190 191 #endif /* _CIRBUF_H_ */ 192