Annotation of libelwix/src/iov.c, revision 1.1.2.2

1.1.2.1   misho       1: /*************************************************************************
                      2: * (C) 2021 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.1.2.2 ! misho       6: * $Id: iov.c,v 1.1.2.1 2021/03/18 13:57:21 misho Exp $
1.1.2.1   misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
                     15: Copyright 2004 - 2021
                     16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #include "global.h"
                     47: 
1.1.2.2 ! misho      48: 
        !            49: /*
        !            50:  * iov_Init() - Init new iovec array
        !            51:  *
        !            52:  * return: =NULL error, !=NULL ready array
        !            53:  */
        !            54: iovec_t *
        !            55: iov_Init()
        !            56: {
        !            57:        iovec_t *iov;
        !            58: 
        !            59:        iov = e_malloc(sizeof(iovec_t));
        !            60:        if (iov)
        !            61:                memset(iov, 0, sizeof(iovec_t));
        !            62: 
        !            63:        return iov;
        !            64: }
        !            65: 
        !            66: /*
        !            67:  * iov_Destroy() - Destroy iovec array
        !            68:  *
        !            69:  * @iov = iovec array
        !            70:  * return: none
        !            71:  */
        !            72: void
        !            73: iov_Destroy(iovec_t ** __restrict iov)
        !            74: {
        !            75:        if (iov && *iov) {
        !            76:                if ((*iov)->iov_array)
        !            77:                        e_free((*iov)->iov_array);
        !            78:                e_free((*iov));
        !            79:                *iov = NULL;
        !            80:        }
        !            81: }
        !            82: 
        !            83: /*
        !            84:  * iov_Get() - Get data and length from position
        !            85:  *
        !            86:  * @iov = iovec array
        !            87:  * @pos = position
        !            88:  * @data = data
        !            89:  * @datlen = data length
        !            90:  * return: -1 error, 0 ok
        !            91:  */
        !            92: int
        !            93: iov_Get(iovec_t * __restrict iov, u_int pos, void **data, size_t *datlen)
        !            94: {
        !            95:        if (!iov || !data || !datlen)
        !            96:                return -1;
        !            97: 
        !            98:        if (pos >= iov->iov_size)
        !            99:                return -1;
        !           100: 
        !           101:        *datlen = MIN(*datlen, iov->iov_array[pos].iov_len);
        !           102:        memcpy(*data, iov->iov_array[pos].iov_base, *datlen);
        !           103: 
        !           104:        return 0;
        !           105: }
        !           106: 
        !           107: /*
        !           108:  * iov_Insert() - Insert data at position into array
        !           109:  *
        !           110:  * @iov = iovec array
        !           111:  * @pos = position
        !           112:  * @data = data
        !           113:  * @datlen = data length
        !           114:  * return: -1 error, 0 ok
        !           115:  */
        !           116: int
        !           117: iov_Insert(iovec_t * __restrict iov, u_int pos, void *data, size_t datlen)
        !           118: {
        !           119:        struct iovec *iv;
        !           120: 
        !           121:        if (!iov)
        !           122:                return -1;
        !           123: 
        !           124:        if (pos >= iov->iov_size) {
        !           125:                iv = e_realloc(iov->iov_array, sizeof(struct iovec) * (pos + 1));
        !           126:                if (!iv)
        !           127:                        return -1;
        !           128: 
        !           129:                iov->iov_array = iv;
        !           130:                iov->iov_size = pos + 1;
        !           131:        }
        !           132: 
        !           133:        iov->iov_array[pos].iov_base = data;
        !           134:        iov->iov_array[pos].iov_len = datlen;
        !           135: 
        !           136:        return 0;
        !           137: }
        !           138: 
        !           139: /*
        !           140:  * iov_Delete() - Delete data at position into array
        !           141:  *
        !           142:  * @iov = iovec array
        !           143:  * @pos = position
        !           144:  * @mustfree = data must be free before delete
        !           145:  * return: -1 error, 0 ok
        !           146:  */
        !           147: int
        !           148: iov_Delete(iovec_t * __restrict iov, u_int pos, int mustfree)
        !           149: {
        !           150:        if (!iov)
        !           151:                return -1;
        !           152: 
        !           153:        if (pos < iov->iov_size) {
        !           154:                if (mustfree)
        !           155:                        e_free(iov->iov_array[pos].iov_base);
        !           156:                memset(iov->iov_array + pos, 0, sizeof(struct iovec));
        !           157:        }
        !           158: 
        !           159:        return 0;
        !           160: }
        !           161: 
        !           162: /*
        !           163:  * iov_Push() - Push data on first free position
        !           164:  *
        !           165:  * @iov = iovec array
        !           166:  * @data = data
        !           167:  * @datlen = data length
        !           168:  * return: -1 error, !=-1 pushed at position
        !           169:  */
        !           170: int
        !           171: iov_Push(iovec_t * __restrict iov, void *data, size_t datlen)
        !           172: {
        !           173:        register int pos;
        !           174: 
        !           175:        if (!iov)
        !           176:                return -1;
        !           177: 
        !           178:        for (pos = 0; pos < iov->iov_size; pos++)
        !           179:                if (!iov->iov_array[pos].iov_len && !iov->iov_array[pos].iov_base)
        !           180:                        break;
        !           181: 
        !           182:        if (iov_Insert(iov, pos, data, datlen))
        !           183:                return -1;
        !           184: 
        !           185:        return (int) pos;
        !           186: }
        !           187: 
        !           188: /*
        !           189:  * iov_Pop() - Pop data from last used position
        !           190:  *
        !           191:  * @iov = iovec array
        !           192:  * @data = data
        !           193:  * @datlen = data length
        !           194:  * @mustfree = data must be free before delete
        !           195:  * return: -1 error, !=-1 poped from position
        !           196:  */
        !           197: int
        !           198: iov_Pop(iovec_t * __restrict iov, void **data, size_t *datlen, int mustfree)
        !           199: {
        !           200:        register int pos;
        !           201: 
        !           202:        if (!iov)
        !           203:                return -1;
        !           204: 
        !           205:        for (pos = iov->iov_size - 1; pos >= 0; pos--)
        !           206:                if (iov->iov_array[pos].iov_base)
        !           207:                        break;
        !           208: 
        !           209:        iov_Get(iov, pos, data, datlen);
        !           210:        iov_Delete(iov, pos, mustfree);
        !           211:        return (int) pos;
        !           212: }
        !           213: 
        !           214: /*
        !           215:  * iov_PushPair() - Push pair/named data on first free position
        !           216:  *
        !           217:  * @iov = iovec array
        !           218:  * @name = name of data
        !           219:  * @data = data
        !           220:  * @datlen = data length
        !           221:  * return: -1 error, !=-1 pushed at position
        !           222:  */
        !           223: int
        !           224: iov_PushPair(iovec_t * __restrict iov, const char *name, void *data, size_t datlen)
        !           225: {
        !           226:        char *str;
        !           227:        int pos;
        !           228: 
        !           229:        if (!iov)
        !           230:                return -1;
        !           231: 
        !           232:        str = e_strdup(name);
        !           233:        if (!str)
        !           234:                return -1;
        !           235:        if (datlen == (size_t) -1) {
        !           236:                if (data)
        !           237:                        datlen = strlen(data) + 1;
        !           238:                else
        !           239:                        datlen = 0;
        !           240:        }
        !           241: 
        !           242:        if ((pos = iov_Push(iov, str, strlen(str) + 1)) == -1) {
        !           243:                e_free(str);
        !           244:                return -1;
        !           245:        }
        !           246:        if (iov_Push(iov, data, datlen) == -1) {
        !           247:                iov_Delete(iov, pos, 42);
        !           248:                return -1;
        !           249:        }
        !           250: 
        !           251:        return pos;
        !           252: }
        !           253: 
        !           254: /*
        !           255:  * iov_PopPair() - Pop pair/named data from last used position
        !           256:  *
        !           257:  * @iov = iovec array
        !           258:  * @name = name of data
        !           259:  * @namlen = name length
        !           260:  * @data = data
        !           261:  * @datlen = data length
        !           262:  * @mustfree = data must be free before delete
        !           263:  * return: -1 error, !=-1 poped from position
        !           264:  */
        !           265: int
        !           266: iov_PopPair(iovec_t * __restrict iov, char **name, size_t *namlen, void **data, size_t *datlen)
        !           267: {
        !           268:        int pos;
        !           269: 
        !           270:        if (!iov)
        !           271:                return -1;
        !           272: 
        !           273:        if (iov_Pop(iov, data, datlen, 0) == -1)
        !           274:                return -1;
        !           275:        if ((pos = iov_Pop(iov, (void**) name, namlen, 42)) == -1)
        !           276:                return -1;
        !           277: 
        !           278:        return pos;
        !           279: }
        !           280: 
        !           281: /*
        !           282:  * iov_Debug() - Debug of iovec array
        !           283:  *
        !           284:  * @iov = iovec array
        !           285:  * return: none
        !           286:  */
        !           287: void
        !           288: iov_Debug(iovec_t * __restrict iov)
        !           289: {
        !           290:        register size_t pos;
        !           291: 
        !           292:        if (!iov)
        !           293:                return;
        !           294: 
        !           295:        for (pos = 0; pos < iov->iov_size; pos++)
        !           296:                printf("IOVEC[%zu] base=%p len=%zu (%s)\n", pos, iov->iov_array[pos].iov_base, 
        !           297:                                iov->iov_array[pos].iov_len, (char*) iov->iov_array[pos].iov_base);
        !           298: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>