15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier/*
355129044dSC.J. Collier * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
365129044dSC.J. Collier * All rights reserved.
375129044dSC.J. Collier * Redistribution and use in source and binary forms, with or without
385129044dSC.J. Collier * modification, are permitted provided that the following conditions are met:
395129044dSC.J. Collier *
405129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
415129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
425129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
435129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in the
445129044dSC.J. Collier *       documentation and/or other materials provided with the distribution.
455129044dSC.J. Collier *     * Neither the name of the University of California, Berkeley nor the
465129044dSC.J. Collier *       names of its contributors may be used to endorse or promote products
475129044dSC.J. Collier *       derived from this software without specific prior written permission.
485129044dSC.J. Collier *
495129044dSC.J. Collier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
505129044dSC.J. Collier * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
515129044dSC.J. Collier * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
525129044dSC.J. Collier * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
535129044dSC.J. Collier * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
545129044dSC.J. Collier * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
555129044dSC.J. Collier * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
565129044dSC.J. Collier * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
575129044dSC.J. Collier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
585129044dSC.J. Collier * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
595129044dSC.J. Collier */
605129044dSC.J. Collier
615129044dSC.J. Collier#ifndef _CIRBUF_H_
625129044dSC.J. Collier#define _CIRBUF_H_
635129044dSC.J. Collier
645129044dSC.J. Collier#ifdef __cplusplus
655129044dSC.J. Collierextern "C" {
665129044dSC.J. Collier#endif
675129044dSC.J. Collier
685129044dSC.J. Collier/**
695129044dSC.J. Collier * This structure is the header of a cirbuf type.
705129044dSC.J. Collier */
715129044dSC.J. Collierstruct cirbuf {
725129044dSC.J. Collier	unsigned int maxlen;    /**< total len of the fifo (number of elements) */
735129044dSC.J. Collier	unsigned int start;     /**< indice of the first elt */
745129044dSC.J. Collier	unsigned int end;       /**< indice of the last elt */
755129044dSC.J. Collier	unsigned int len;       /**< current len of fifo */
765129044dSC.J. Collier	char *buf;
775129044dSC.J. Collier};
785129044dSC.J. Collier
795129044dSC.J. Collier#ifdef RTE_LIBRTE_CMDLINE_DEBUG
805129044dSC.J. Collier#define dprintf_(fmt, ...) printf("line %3.3d - " fmt "%.0s", __LINE__, __VA_ARGS__)
815129044dSC.J. Collier#define dprintf(...) dprintf_(__VA_ARGS__, "dummy")
825129044dSC.J. Collier#else
835129044dSC.J. Collier#define dprintf(...) (void)0
845129044dSC.J. Collier#endif
855129044dSC.J. Collier
865129044dSC.J. Collier
875129044dSC.J. Collier/**
885129044dSC.J. Collier * Init the circular buffer
895129044dSC.J. Collier */
905129044dSC.J. Collierint cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen);
915129044dSC.J. Collier
925129044dSC.J. Collier
935129044dSC.J. Collier/**
945129044dSC.J. Collier * Return 1 if the circular buffer is full
955129044dSC.J. Collier */
965129044dSC.J. Collier#define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len)
975129044dSC.J. Collier
985129044dSC.J. Collier/**
995129044dSC.J. Collier * Return 1 if the circular buffer is empty
1005129044dSC.J. Collier */
1015129044dSC.J. Collier#define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0)
1025129044dSC.J. Collier
1035129044dSC.J. Collier/**
1045129044dSC.J. Collier * return current size of the circular buffer (number of used elements)
1055129044dSC.J. Collier */
1065129044dSC.J. Collier#define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len)
1075129044dSC.J. Collier
1085129044dSC.J. Collier/**
1095129044dSC.J. Collier * return size of the circular buffer (used + free elements)
1105129044dSC.J. Collier */
1115129044dSC.J. Collier#define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
1125129044dSC.J. Collier
1135129044dSC.J. Collier/**
1145129044dSC.J. Collier * return the number of free elts
1155129044dSC.J. Collier */
1165129044dSC.J. Collier#define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
1175129044dSC.J. Collier
1185129044dSC.J. Collier/**
1195129044dSC.J. Collier * Iterator for a circular buffer
1205129044dSC.J. Collier *   c: struct cirbuf pointer
1215129044dSC.J. Collier *   i: an integer type internally used in the macro
1225129044dSC.J. Collier *   e: char that takes the value for each iteration
1235129044dSC.J. Collier */
1245129044dSC.J. Collier#define CIRBUF_FOREACH(c, i, e)                                 \
1255129044dSC.J. Collier	for ( i=0, e=(c)->buf[(c)->start] ;                     \
1265129044dSC.J. Collier		i<((c)->len) ;                                  \
1275129044dSC.J. Collier		i ++,  e=(c)->buf[((c)->start+i)%((c)->maxlen)])
1285129044dSC.J. Collier
1295129044dSC.J. Collier
1305129044dSC.J. Collier/**
1315129044dSC.J. Collier * Add a character at head of the circular buffer. Return 0 on success, or
1325129044dSC.J. Collier * a negative value on error.
1335129044dSC.J. Collier */
1345129044dSC.J. Collierint cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
1355129044dSC.J. Collier
1365129044dSC.J. Collier/**
1375129044dSC.J. Collier * Add a character at head of the circular buffer. You _must_ check that you
1385129044dSC.J. Collier * have enough free space in the buffer before calling this func.
1395129044dSC.J. Collier */
1405129044dSC.J. Colliervoid cirbuf_add_head(struct cirbuf *cbuf, char c);
1415129044dSC.J. Collier
1425129044dSC.J. Collier/**
1435129044dSC.J. Collier * Add a character at tail of the circular buffer. Return 0 on success, or
1445129044dSC.J. Collier * a negative value on error.
1455129044dSC.J. Collier */
1465129044dSC.J. Collierint cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
1475129044dSC.J. Collier
1485129044dSC.J. Collier/**
1495129044dSC.J. Collier * Add a character at tail of the circular buffer. You _must_ check that you
1505129044dSC.J. Collier * have enough free space in the buffer before calling this func.
1515129044dSC.J. Collier */
1525129044dSC.J. Colliervoid cirbuf_add_tail(struct cirbuf *cbuf, char c);
1535129044dSC.J. Collier
1545129044dSC.J. Collier/**
1555129044dSC.J. Collier * Remove a char at the head of the circular buffer. Return 0 on
1565129044dSC.J. Collier * success, or a negative value on error.
1575129044dSC.J. Collier */
1585129044dSC.J. Collierint cirbuf_del_head_safe(struct cirbuf *cbuf);
1595129044dSC.J. Collier
1605129044dSC.J. Collier/**
1615129044dSC.J. Collier * Remove a char at the head of the circular buffer. You _must_ check
1625129044dSC.J. Collier * that buffer is not empty before calling the function.
1635129044dSC.J. Collier */
1645129044dSC.J. Colliervoid cirbuf_del_head(struct cirbuf *cbuf);
1655129044dSC.J. Collier
1665129044dSC.J. Collier/**
1675129044dSC.J. Collier * Remove a char at the tail of the circular buffer. Return 0 on
1685129044dSC.J. Collier * success, or a negative value on error.
1695129044dSC.J. Collier */
1705129044dSC.J. Collierint cirbuf_del_tail_safe(struct cirbuf *cbuf);
1715129044dSC.J. Collier
1725129044dSC.J. Collier/**
1735129044dSC.J. Collier * Remove a char at the tail of the circular buffer. You _must_ check
1745129044dSC.J. Collier * that buffer is not empty before calling the function.
1755129044dSC.J. Collier */
1765129044dSC.J. Colliervoid cirbuf_del_tail(struct cirbuf *cbuf);
1775129044dSC.J. Collier
1785129044dSC.J. Collier/**
1795129044dSC.J. Collier * Return the head of the circular buffer. You _must_ check that
1805129044dSC.J. Collier * buffer is not empty before calling the function.
1815129044dSC.J. Collier */
1825129044dSC.J. Collierchar cirbuf_get_head(struct cirbuf *cbuf);
1835129044dSC.J. Collier
1845129044dSC.J. Collier/**
1855129044dSC.J. Collier * Return the tail of the circular buffer. You _must_ check that
1865129044dSC.J. Collier * buffer is not empty before calling the function.
1875129044dSC.J. Collier */
1885129044dSC.J. Collierchar cirbuf_get_tail(struct cirbuf *cbuf);
1895129044dSC.J. Collier
1905129044dSC.J. Collier/**
1915129044dSC.J. Collier * Add a buffer at head of the circular buffer. 'c' is a pointer to a
1925129044dSC.J. Collier * buffer, and n is the number of char to add. Return the number of
1935129044dSC.J. Collier * copied bytes on success, or a negative value on error.
1945129044dSC.J. Collier */
1955129044dSC.J. Collierint cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n);
1965129044dSC.J. Collier
1975129044dSC.J. Collier/**
1985129044dSC.J. Collier * Add a buffer at tail of the circular buffer. 'c' is a pointer to a
1995129044dSC.J. Collier * buffer, and n is the number of char to add. Return the number of
2005129044dSC.J. Collier * copied bytes on success, or a negative value on error.
2015129044dSC.J. Collier */
2025129044dSC.J. Collierint cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n);
2035129044dSC.J. Collier
2045129044dSC.J. Collier/**
2055129044dSC.J. Collier * Remove chars at the head of the circular buffer. Return 0 on
2065129044dSC.J. Collier * success, or a negative value on error.
2075129044dSC.J. Collier */
2085129044dSC.J. Collierint cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size);
2095129044dSC.J. Collier
2105129044dSC.J. Collier/**
2115129044dSC.J. Collier * Remove chars at the tail of the circular buffer. Return 0 on
2125129044dSC.J. Collier * success, or a negative value on error.
2135129044dSC.J. Collier */
2145129044dSC.J. Collierint cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size);
2155129044dSC.J. Collier
2165129044dSC.J. Collier/**
2175129044dSC.J. Collier * Copy a maximum of 'size' characters from the head of the circular
2185129044dSC.J. Collier * buffer to a flat one pointed by 'c'. Return the number of copied
2195129044dSC.J. Collier * chars.
2205129044dSC.J. Collier */
2215129044dSC.J. Collierint cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size);
2225129044dSC.J. Collier
2235129044dSC.J. Collier/**
2245129044dSC.J. Collier * Copy a maximum of 'size' characters from the tail of the circular
2255129044dSC.J. Collier * buffer to a flat one pointed by 'c'. Return the number of copied
2265129044dSC.J. Collier * chars.
2275129044dSC.J. Collier */
2285129044dSC.J. Collierint cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size);
2295129044dSC.J. Collier
2305129044dSC.J. Collier
2315129044dSC.J. Collier/**
2325129044dSC.J. Collier * Set the start of the data to the index 0 of the internal buffer.
2335129044dSC.J. Collier */
2345129044dSC.J. Collierint cirbuf_align_left(struct cirbuf *cbuf);
2355129044dSC.J. Collier
2365129044dSC.J. Collier/**
2375129044dSC.J. Collier * Set the end of the data to the last index of the internal buffer.
2385129044dSC.J. Collier */
2395129044dSC.J. Collierint cirbuf_align_right(struct cirbuf *cbuf);
2405129044dSC.J. Collier
2415129044dSC.J. Collier#ifdef __cplusplus
2425129044dSC.J. Collier}
2435129044dSC.J. Collier#endif
2445129044dSC.J. Collier
2455129044dSC.J. Collier#endif /* _CIRBUF_H_ */
246