X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_cmdline%2Fcmdline_cirbuf.c;h=829a8af56355fdf13fee1715edf1c830f1b370cb;hb=add6c87ebe9337c66c3009f458c48fbccb727558;hp=f70a23144e8aaafe278a83d978c2d269570c901b;hpb=dada9ef6edc59015b6674b5a95258787c71401b0;p=dpdk.git diff --git a/lib/librte_cmdline/cmdline_cirbuf.c b/lib/librte_cmdline/cmdline_cirbuf.c index f70a23144e..829a8af563 100644 --- a/lib/librte_cmdline/cmdline_cirbuf.c +++ b/lib/librte_cmdline/cmdline_cirbuf.c @@ -1,78 +1,27 @@ -/*- - * BSD LICENSE - * - * Copyright(c) 2010-2012 Intel Corporation. All rights reserved. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Intel Corporation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -/* +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2014 Intel Corporation. * Copyright (c) 2009, Olivier MATZ * All rights reserved. - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the University of California, Berkeley nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include +#include #include "cmdline_cirbuf.h" -void +int cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen) { + if (!cbuf || !buf) + return -EINVAL; cbuf->maxlen = maxlen; cbuf->len = 0; cbuf->start = start; cbuf->end = start; cbuf->buf = buf; + return 0; } /* multiple add */ @@ -82,7 +31,7 @@ cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n) { unsigned int e; - if (!n || n > CIRBUF_GET_FREELEN(cbuf)) + if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf)) return -EINVAL; e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0; @@ -113,7 +62,7 @@ cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n) { unsigned int e; - if (!n || n > CIRBUF_GET_FREELEN(cbuf)) + if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf)) return -EINVAL; e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0; @@ -232,8 +181,12 @@ __cirbuf_shift_right(struct cirbuf *cbuf) } /* XXX we could do a better algorithm here... */ -void cirbuf_align_left(struct cirbuf * cbuf) +int +cirbuf_align_left(struct cirbuf * cbuf) { + if (!cbuf) + return -EINVAL; + if (cbuf->start < cbuf->maxlen/2) { while (cbuf->start != 0) { __cirbuf_shift_left(cbuf); @@ -244,11 +197,17 @@ void cirbuf_align_left(struct cirbuf * cbuf) __cirbuf_shift_right(cbuf); } } + + return 0; } /* XXX we could do a better algorithm here... */ -void cirbuf_align_right(struct cirbuf * cbuf) +int +cirbuf_align_right(struct cirbuf * cbuf) { + if (!cbuf) + return -EINVAL; + if (cbuf->start >= cbuf->maxlen/2) { while (cbuf->end != cbuf->maxlen-1) { __cirbuf_shift_left(cbuf); @@ -259,6 +218,8 @@ void cirbuf_align_right(struct cirbuf * cbuf) __cirbuf_shift_right(cbuf); } } + + return 0; } /* buffer del */ @@ -266,7 +227,7 @@ void cirbuf_align_right(struct cirbuf * cbuf) int cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size) { - if (!size || size > CIRBUF_GET_LEN(cbuf)) + if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf)) return -EINVAL; cbuf->len -= size; @@ -286,7 +247,7 @@ cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size) int cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size) { - if (!size || size > CIRBUF_GET_LEN(cbuf)) + if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf)) return -EINVAL; cbuf->len -= size; @@ -364,6 +325,9 @@ cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size) { unsigned int n; + if (!cbuf || !c) + return -EINVAL; + n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf); if (!n) @@ -374,13 +338,20 @@ cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size) memcpy(c, cbuf->buf + cbuf->start , n); } else { - dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, - cbuf->maxlen - cbuf->start); - dprintf("s[%d] -> d[%d] (%d)\n", 0,cbuf->maxlen - cbuf->start, - n - cbuf->maxlen + cbuf->start); - memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start); - memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf, - n - cbuf->maxlen + cbuf->start); + /* check if we need to go from end to the beginning */ + if (n <= cbuf->maxlen - cbuf->start) { + dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n); + memcpy(c, cbuf->buf + cbuf->start , n); + } + else { + dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, + cbuf->maxlen - cbuf->start); + dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start, + n - cbuf->maxlen + cbuf->start); + memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start); + memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf, + n - cbuf->maxlen + cbuf->start); + } } return n; } @@ -392,6 +363,9 @@ cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size) { unsigned int n; + if (!cbuf || !c) + return -EINVAL; + n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf); if (!n) @@ -402,15 +376,21 @@ cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size) memcpy(c, cbuf->buf + cbuf->end - n + 1, n); } else { - dprintf("s[%d] -> d[%d] (%d)\n", 0, - cbuf->maxlen - cbuf->start, cbuf->end + 1); - dprintf("s[%d] -> d[%d] (%d)\n", - cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1); - - memcpy(c + cbuf->maxlen - cbuf->start, - cbuf->buf, cbuf->end + 1); - memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1, - n - cbuf->end - 1); + /* check if we need to go from end to the beginning */ + if (n <= cbuf->end + 1) { + dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n); + memcpy(c, cbuf->buf + cbuf->end - n + 1, n); + } + else { + dprintf("s[%d] -> d[%d] (%d)\n", 0, + cbuf->maxlen - cbuf->start, cbuf->end + 1); + dprintf("s[%d] -> d[%d] (%d)\n", + cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1); + memcpy(c + cbuf->maxlen - cbuf->start, + cbuf->buf, cbuf->end + 1); + memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1, + n - cbuf->end - 1); + } } return n; } @@ -430,4 +410,3 @@ cirbuf_get_tail(struct cirbuf * cbuf) { return cbuf->buf[cbuf->end]; } -