X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_cmdline%2Fcmdline_cirbuf.c;h=f506f88059b2706574e8273d29d3bb039838697d;hb=eb6d5a0af9a05bf940ba19ec1ddbe575b5e7540b;hp=ccc51fc40c5425dc72acc87dfee337e07e34aaf8;hpb=af75078fece3615088e561357c1e97603e43a5fe;p=dpdk.git diff --git a/lib/librte_cmdline/cmdline_cirbuf.c b/lib/librte_cmdline/cmdline_cirbuf.c index ccc51fc40c..f506f88059 100644 --- a/lib/librte_cmdline/cmdline_cirbuf.c +++ b/lib/librte_cmdline/cmdline_cirbuf.c @@ -1,36 +1,34 @@ /*- * BSD LICENSE - * - * Copyright(c) 2010-2012 Intel Corporation. All rights reserved. + * + * Copyright(c) 2010-2014 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 + * + * 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 + * + * * 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 + * * 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 + * * 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 + * + * 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. - * - * version: DPDK.L.1.2.3-3 */ /* @@ -62,18 +60,22 @@ #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 */ @@ -83,7 +85,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; @@ -114,7 +116,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; @@ -233,8 +235,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); @@ -245,11 +251,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); @@ -260,6 +272,8 @@ void cirbuf_align_right(struct cirbuf * cbuf) __cirbuf_shift_right(cbuf); } } + + return 0; } /* buffer del */ @@ -267,7 +281,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; @@ -287,7 +301,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; @@ -365,6 +379,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) @@ -375,13 +392,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; } @@ -393,6 +417,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) @@ -403,15 +430,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; } @@ -431,4 +464,3 @@ cirbuf_get_tail(struct cirbuf * cbuf) { return cbuf->buf[cbuf->end]; } -