qemu/iov.c
<<
>>
Prefs
   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
  17size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
  18                    const void *buf, size_t size)
  19{
  20    size_t offset;
  21    unsigned int i;
  22
  23    offset = 0;
  24    for (i = 0; offset < size && i < iovcnt; i++) {
  25        size_t len;
  26
  27        len = MIN(iov[i].iov_len, size - offset);
  28
  29        memcpy(iov[i].iov_base, buf + offset, len);
  30        offset += len;
  31    }
  32    return offset;
  33}
  34
  35size_t iov_to_buf(const struct iovec *iov, const unsigned int iovcnt,
  36                  void *buf, size_t offset, size_t size)
  37{
  38    uint8_t *ptr;
  39    size_t iov_off, buf_off;
  40    unsigned int i;
  41
  42    ptr = buf;
  43    iov_off = 0;
  44    buf_off = 0;
  45    for (i = 0; i < iovcnt && size; i++) {
  46        if (offset < (iov_off + iov[i].iov_len)) {
  47            size_t len = MIN((iov_off + iov[i].iov_len) - offset , size);
  48
  49            memcpy(ptr + buf_off, iov[i].iov_base + (offset - iov_off), len);
  50
  51            buf_off += len;
  52            offset += len;
  53            size -= len;
  54        }
  55        iov_off += iov[i].iov_len;
  56    }
  57    return buf_off;
  58}
  59
  60size_t iov_size(const struct iovec *iov, const unsigned int iovcnt)
  61{
  62    size_t len;
  63    unsigned int i;
  64
  65    len = 0;
  66    for (i = 0; i < iovcnt; i++) {
  67        len += iov[i].iov_len;
  68    }
  69    return len;
  70}
  71