--- libelwix/src/iov.c 2021/03/19 00:40:28 1.1.2.2 +++ libelwix/src/iov.c 2021/03/21 01:31:07 1.1.2.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: iov.c,v 1.1.2.2 2021/03/19 00:40:28 misho Exp $ +* $Id: iov.c,v 1.1.2.5 2021/03/21 01:31:07 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -90,7 +90,7 @@ iov_Destroy(iovec_t ** __restrict iov) * return: -1 error, 0 ok */ int -iov_Get(iovec_t * __restrict iov, u_int pos, void **data, size_t *datlen) +iov_Get(iovec_t * __restrict iov, u_int pos, void *data, size_t *datlen) { if (!iov || !data || !datlen) return -1; @@ -99,7 +99,7 @@ iov_Get(iovec_t * __restrict iov, u_int pos, void **da return -1; *datlen = MIN(*datlen, iov->iov_array[pos].iov_len); - memcpy(*data, iov->iov_array[pos].iov_base, *datlen); + memcpy(data, iov->iov_array[pos].iov_base, *datlen); return 0; } @@ -195,13 +195,16 @@ iov_Push(iovec_t * __restrict iov, void *data, size_t * return: -1 error, !=-1 poped from position */ int -iov_Pop(iovec_t * __restrict iov, void **data, size_t *datlen, int mustfree) +iov_Pop(iovec_t * __restrict iov, void *data, size_t *datlen, int mustfree) { register int pos; if (!iov) return -1; + if (iov->iov_size < 1) + return 0; + for (pos = iov->iov_size - 1; pos >= 0; pos--) if (iov->iov_array[pos].iov_base) break; @@ -263,22 +266,51 @@ iov_PushPair(iovec_t * __restrict iov, const char *nam * return: -1 error, !=-1 poped from position */ int -iov_PopPair(iovec_t * __restrict iov, char **name, size_t *namlen, void **data, size_t *datlen) +iov_PopPair(iovec_t * __restrict iov, char *name, size_t *namlen, + void *data, size_t *datlen, int mustfree) { int pos; if (!iov) return -1; - if (iov_Pop(iov, data, datlen, 0) == -1) + if (iov_Pop(iov, data, datlen, mustfree) == -1) return -1; - if ((pos = iov_Pop(iov, (void**) name, namlen, 42)) == -1) + if ((pos = iov_Pop(iov, name, namlen, 42)) == -1) return -1; return pos; } /* + * iov_FreePairs() - Free pairs/named data in iovec array + * + * @iov = iovec array + * @mustfree = data must be free before delete + * return: -1 error or 0 ok + */ +int +iov_FreePairs(iovec_t * __restrict iov, int mustfree) +{ + register int pos; + + if (!iov) + return -1; + + if (iov->iov_size < 1) + return 0; + + for (pos = 0; pos < (iov_Size(iov) & ~1); pos += 2) { + if (iov->iov_array[pos].iov_base) + iov_Delete(iov, pos, 42); + if (iov->iov_array[pos + 1].iov_base) + iov_Delete(iov, pos + 1, mustfree); + } + + return 0; +} + +/* * iov_Debug() - Debug of iovec array * * @iov = iovec array @@ -292,6 +324,7 @@ iov_Debug(iovec_t * __restrict iov) if (!iov) return; + printf("iov_size=%zu iov_array=%p\n", iov->iov_size, iov->iov_array); for (pos = 0; pos < iov->iov_size; pos++) printf("IOVEC[%zu] base=%p len=%zu (%s)\n", pos, iov->iov_array[pos].iov_base, iov->iov_array[pos].iov_len, (char*) iov->iov_array[pos].iov_base);