Annotation of qemu/iov.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * Helpers for getting linearized buffers from iov / filling buffers into iovs
                      3:  *
                      4:  * Copyright IBM, Corp. 2007, 2008
                      5:  * Copyright (C) 2010 Red Hat, Inc.
                      6:  *
                      7:  * Author(s):
                      8:  *  Anthony Liguori <aliguori@us.ibm.com>
                      9:  *  Amit Shah <amit.shah@redhat.com>
                     10:  *
                     11:  * This work is licensed under the terms of the GNU GPL, version 2.  See
                     12:  * the COPYING file in the top-level directory.
                     13:  */
                     14: 
                     15: #include "iov.h"
                     16: 
1.1.1.2 ! root       17: size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
        !            18:                     const void *buf, size_t iov_off, size_t size)
1.1       root       19: {
1.1.1.2 ! root       20:     size_t iovec_off, buf_off;
1.1       root       21:     unsigned int i;
                     22: 
1.1.1.2 ! root       23:     iovec_off = 0;
        !            24:     buf_off = 0;
        !            25:     for (i = 0; i < iov_cnt && size; i++) {
        !            26:         if (iov_off < (iovec_off + iov[i].iov_len)) {
        !            27:             size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off, size);
1.1       root       28: 
1.1.1.2 ! root       29:             memcpy(iov[i].iov_base + (iov_off - iovec_off), buf + buf_off, len);
1.1       root       30: 
1.1.1.2 ! root       31:             buf_off += len;
        !            32:             iov_off += len;
        !            33:             size -= len;
        !            34:         }
        !            35:         iovec_off += iov[i].iov_len;
1.1       root       36:     }
1.1.1.2 ! root       37:     return buf_off;
1.1       root       38: }
                     39: 
1.1.1.2 ! root       40: size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
        !            41:                   void *buf, size_t iov_off, size_t size)
1.1       root       42: {
                     43:     uint8_t *ptr;
1.1.1.2 ! root       44:     size_t iovec_off, buf_off;
1.1       root       45:     unsigned int i;
                     46: 
                     47:     ptr = buf;
1.1.1.2 ! root       48:     iovec_off = 0;
1.1       root       49:     buf_off = 0;
1.1.1.2 ! root       50:     for (i = 0; i < iov_cnt && size; i++) {
        !            51:         if (iov_off < (iovec_off + iov[i].iov_len)) {
        !            52:             size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
1.1       root       53: 
1.1.1.2 ! root       54:             memcpy(ptr + buf_off, iov[i].iov_base + (iov_off - iovec_off), len);
1.1       root       55: 
                     56:             buf_off += len;
1.1.1.2 ! root       57:             iov_off += len;
1.1       root       58:             size -= len;
                     59:         }
1.1.1.2 ! root       60:         iovec_off += iov[i].iov_len;
1.1       root       61:     }
                     62:     return buf_off;
                     63: }
                     64: 
1.1.1.2 ! root       65: size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
1.1       root       66: {
                     67:     size_t len;
                     68:     unsigned int i;
                     69: 
                     70:     len = 0;
1.1.1.2 ! root       71:     for (i = 0; i < iov_cnt; i++) {
1.1       root       72:         len += iov[i].iov_len;
                     73:     }
                     74:     return len;
                     75: }

unix.superglobalmegacorp.com