~ubuntu-branches/ubuntu/vivid/qemu-linaro/vivid

« back to all changes in this revision

Viewing changes to iov.c

  • Committer: Ricardo Salveti de Araujo
  • Date: 2012-09-20 18:39:31 UTC
  • mfrom: (12922.1.2 qemu-linaro)
  • Revision ID: ricardo.salveti@linaro.org-20120920183931-sp3cg6kpdl8dmwo9
* New upstream release.
  - support emulated systems with more than 2G of memory. (LP: #1030588)
* Drop powerpc-missing-include.patch - merged upstream.
* Update debian/control:
  - drop perl build dependency.
  - add libfdt-dev build dependency.
* Update debian/qemu-keymaps.install file.
* Update debian/rules:
  - update QEMU_CPU for ARM architecture: armv4l -> armv7l.
  - update conf_audio_drv: default to PulseAudio since PA is the default on
    Ubuntu.
  - enable KVM on ARM architecture.
  - enable flat device tree support (--enable-fdt). (LP: #1030594)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 * Author(s):
8
8
 *  Anthony Liguori <aliguori@us.ibm.com>
9
9
 *  Amit Shah <amit.shah@redhat.com>
 
10
 *  Michael Tokarev <mjt@tls.msk.ru>
10
11
 *
11
12
 * This work is licensed under the terms of the GNU GPL, version 2.  See
12
13
 * the COPYING file in the top-level directory.
17
18
 
18
19
#include "iov.h"
19
20
 
 
21
#ifdef _WIN32
 
22
# include <windows.h>
 
23
# include <winsock2.h>
 
24
#else
 
25
# include <sys/types.h>
 
26
# include <sys/socket.h>
 
27
#endif
 
28
 
20
29
size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
21
 
                    const void *buf, size_t iov_off, size_t size)
 
30
                    size_t offset, const void *buf, size_t bytes)
22
31
{
23
 
    size_t iovec_off, buf_off;
 
32
    size_t done;
24
33
    unsigned int i;
25
 
 
26
 
    iovec_off = 0;
27
 
    buf_off = 0;
28
 
    for (i = 0; i < iov_cnt && size; i++) {
29
 
        if (iov_off < (iovec_off + iov[i].iov_len)) {
30
 
            size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off, size);
31
 
 
32
 
            memcpy(iov[i].iov_base + (iov_off - iovec_off), buf + buf_off, len);
33
 
 
34
 
            buf_off += len;
35
 
            iov_off += len;
36
 
            size -= len;
 
34
    for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
 
35
        if (offset < iov[i].iov_len) {
 
36
            size_t len = MIN(iov[i].iov_len - offset, bytes - done);
 
37
            memcpy(iov[i].iov_base + offset, buf + done, len);
 
38
            done += len;
 
39
            offset = 0;
 
40
        } else {
 
41
            offset -= iov[i].iov_len;
37
42
        }
38
 
        iovec_off += iov[i].iov_len;
39
43
    }
40
 
    return buf_off;
 
44
    assert(offset == 0);
 
45
    return done;
41
46
}
42
47
 
43
48
size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
44
 
                  void *buf, size_t iov_off, size_t size)
 
49
                  size_t offset, void *buf, size_t bytes)
45
50
{
46
 
    uint8_t *ptr;
47
 
    size_t iovec_off, buf_off;
 
51
    size_t done;
48
52
    unsigned int i;
49
 
 
50
 
    ptr = buf;
51
 
    iovec_off = 0;
52
 
    buf_off = 0;
53
 
    for (i = 0; i < iov_cnt && size; i++) {
54
 
        if (iov_off < (iovec_off + iov[i].iov_len)) {
55
 
            size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
56
 
 
57
 
            memcpy(ptr + buf_off, iov[i].iov_base + (iov_off - iovec_off), len);
58
 
 
59
 
            buf_off += len;
60
 
            iov_off += len;
61
 
            size -= len;
 
53
    for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
 
54
        if (offset < iov[i].iov_len) {
 
55
            size_t len = MIN(iov[i].iov_len - offset, bytes - done);
 
56
            memcpy(buf + done, iov[i].iov_base + offset, len);
 
57
            done += len;
 
58
            offset = 0;
 
59
        } else {
 
60
            offset -= iov[i].iov_len;
62
61
        }
63
 
        iovec_off += iov[i].iov_len;
64
62
    }
65
 
    return buf_off;
 
63
    assert(offset == 0);
 
64
    return done;
66
65
}
67
66
 
68
 
size_t iov_clear(const struct iovec *iov, const unsigned int iov_cnt,
69
 
                 size_t iov_off, size_t size)
 
67
size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
 
68
                  size_t offset, int fillc, size_t bytes)
70
69
{
71
 
    size_t iovec_off, buf_off;
 
70
    size_t done;
72
71
    unsigned int i;
73
 
 
74
 
    iovec_off = 0;
75
 
    buf_off = 0;
76
 
    for (i = 0; i < iov_cnt && size; i++) {
77
 
        if (iov_off < (iovec_off + iov[i].iov_len)) {
78
 
            size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
79
 
 
80
 
            memset(iov[i].iov_base + (iov_off - iovec_off), 0, len);
81
 
 
82
 
            buf_off += len;
83
 
            iov_off += len;
84
 
            size -= len;
 
72
    for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
 
73
        if (offset < iov[i].iov_len) {
 
74
            size_t len = MIN(iov[i].iov_len - offset, bytes - done);
 
75
            memset(iov[i].iov_base + offset, fillc, len);
 
76
            done += len;
 
77
            offset = 0;
 
78
        } else {
 
79
            offset -= iov[i].iov_len;
85
80
        }
86
 
        iovec_off += iov[i].iov_len;
87
81
    }
88
 
    return buf_off;
 
82
    assert(offset == 0);
 
83
    return done;
89
84
}
90
85
 
91
86
size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
100
95
    return len;
101
96
}
102
97
 
 
98
/* helper function for iov_send_recv() */
 
99
static ssize_t
 
100
do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
 
101
{
 
102
#if defined CONFIG_IOVEC && defined CONFIG_POSIX
 
103
    ssize_t ret;
 
104
    struct msghdr msg;
 
105
    memset(&msg, 0, sizeof(msg));
 
106
    msg.msg_iov = iov;
 
107
    msg.msg_iovlen = iov_cnt;
 
108
    do {
 
109
        ret = do_send
 
110
            ? sendmsg(sockfd, &msg, 0)
 
111
            : recvmsg(sockfd, &msg, 0);
 
112
    } while (ret < 0 && errno == EINTR);
 
113
    return ret;
 
114
#else
 
115
    /* else send piece-by-piece */
 
116
    /*XXX Note: windows has WSASend() and WSARecv() */
 
117
    unsigned i = 0;
 
118
    ssize_t ret = 0;
 
119
    while (i < iov_cnt) {
 
120
        ssize_t r = do_send
 
121
            ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
 
122
            : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
 
123
        if (r > 0) {
 
124
            ret += r;
 
125
        } else if (!r) {
 
126
            break;
 
127
        } else if (errno == EINTR) {
 
128
            continue;
 
129
        } else {
 
130
            /* else it is some "other" error,
 
131
             * only return if there was no data processed. */
 
132
            if (ret == 0) {
 
133
                ret = -1;
 
134
            }
 
135
            break;
 
136
        }
 
137
        i++;
 
138
    }
 
139
    return ret;
 
140
#endif
 
141
}
 
142
 
 
143
ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
 
144
                      size_t offset, size_t bytes,
 
145
                      bool do_send)
 
146
{
 
147
    ssize_t ret;
 
148
    unsigned si, ei;            /* start and end indexes */
 
149
 
 
150
    /* Find the start position, skipping `offset' bytes:
 
151
     * first, skip all full-sized vector elements, */
 
152
    for (si = 0; si < iov_cnt && offset >= iov[si].iov_len; ++si) {
 
153
        offset -= iov[si].iov_len;
 
154
    }
 
155
    if (offset) {
 
156
        assert(si < iov_cnt);
 
157
        /* second, skip `offset' bytes from the (now) first element,
 
158
         * undo it on exit */
 
159
        iov[si].iov_base += offset;
 
160
        iov[si].iov_len -= offset;
 
161
    }
 
162
    /* Find the end position skipping `bytes' bytes: */
 
163
    /* first, skip all full-sized elements */
 
164
    for (ei = si; ei < iov_cnt && iov[ei].iov_len <= bytes; ++ei) {
 
165
        bytes -= iov[ei].iov_len;
 
166
    }
 
167
    if (bytes) {
 
168
        /* second, fixup the last element, and remember
 
169
         * the length we've cut from the end of it in `bytes' */
 
170
        size_t tail;
 
171
        assert(ei < iov_cnt);
 
172
        assert(iov[ei].iov_len > bytes);
 
173
        tail = iov[ei].iov_len - bytes;
 
174
        iov[ei].iov_len = bytes;
 
175
        bytes = tail;  /* bytes is now equal to the tail size */
 
176
        ++ei;
 
177
    }
 
178
 
 
179
    ret = do_send_recv(sockfd, iov + si, ei - si, do_send);
 
180
 
 
181
    /* Undo the changes above */
 
182
    if (offset) {
 
183
        iov[si].iov_base -= offset;
 
184
        iov[si].iov_len += offset;
 
185
    }
 
186
    if (bytes) {
 
187
        iov[ei-1].iov_len += bytes;
 
188
    }
 
189
 
 
190
    return ret;
 
191
}
 
192
 
 
193
 
103
194
void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
104
195
                 FILE *fp, const char *prefix, size_t limit)
105
196
{