Skip to content

Commit 1befd34

Browse files
committed
Bring <qemu/iov.h> and <qemu/typedefs.h>
The location of qemu_iovec_xxx functions has moved from util/cutils.c to a new file util/iov.c, with a few changes in the function's interface, so update all callers. + Bring in <qemu/typedefs.h> and fix the few conflicts there. Change-Id: I851ad31c3e15a0e8a23266cbfd5d1a52630a66b7
1 parent e2678e1 commit 1befd34

File tree

17 files changed

+702
-237
lines changed

17 files changed

+702
-237
lines changed

Makefile.common

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ CORE_MISC_SOURCES = \
469469
util/aes.c \
470470
util/cutils.c \
471471
util/error.c \
472+
util/hexdump.c \
473+
util/iov.c \
472474
util/module.c \
473475
util/notify.c \
474476
util/osdep.c \

Makefile.target

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ common_LOCAL_SRC_FILES := \
342342
android/user-events-qemu.c \
343343
hw/core/loader.c \
344344
ui/keymaps.c \
345+
util/iov.c \
345346

346347

347348
# The following files cannot be in static libraries because they contain

block.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qemu-common.h"
2626
#include "monitor/monitor.h"
2727
#include "block/block_int.h"
28+
#include "qemu/iov.h"
2829
#include "qemu/module.h"
2930
//#include "qapi/qmp/types.h"
3031
#include "qapi/qmp/qjson.h"
@@ -2137,7 +2138,7 @@ static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
21372138
// Add the first request to the merged one. If the requests are
21382139
// overlapping, drop the last sectors of the first request.
21392140
size = (reqs[i].sector - reqs[outidx].sector) << 9;
2140-
qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2141+
qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
21412142

21422143
// We might need to add some zeros between the two requests
21432144
if (reqs[i].sector > oldreq_last) {
@@ -2149,7 +2150,7 @@ static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
21492150
}
21502151

21512152
// Add the second request
2152-
qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2153+
qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
21532154

21542155
reqs[outidx].nb_sectors = qiov->size >> 9;
21552156
reqs[outidx].qiov = qiov;
@@ -2307,7 +2308,7 @@ static void bdrv_aio_bh_cb(void *opaque)
23072308
BlockDriverAIOCBSync *acb = opaque;
23082309

23092310
if (!acb->is_write)
2310-
qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2311+
qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
23112312
qemu_vfree(acb->bounce);
23122313
acb->common.cb(acb->common.opaque, acb->ret);
23132314
qemu_bh_delete(acb->bh);
@@ -2335,7 +2336,7 @@ static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
23352336
acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
23362337

23372338
if (is_write) {
2338-
qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2339+
qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
23392340
acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
23402341
} else {
23412342
acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);

block/qcow.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
#include "qemu-common.h"
2525
#include "block/block_int.h"
26+
#include "qemu/iov.h"
2627
#include "qemu/module.h"
2728
#include <zlib.h>
2829
#include "qemu/aes.h"
@@ -529,7 +530,7 @@ static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
529530
if (qiov->niov > 1) {
530531
acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
531532
if (is_write)
532-
qemu_iovec_to_buffer(qiov, acb->buf);
533+
qemu_iovec_to_buf(qiov, 0, acb->buf, qiov->size);
533534
} else {
534535
acb->buf = (uint8_t *)qiov->iov->iov_base;
535536
}
@@ -623,7 +624,7 @@ static void qcow_aio_read_cb(void *opaque, int ret)
623624

624625
done:
625626
if (acb->qiov->niov > 1) {
626-
qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
627+
qemu_iovec_from_buf(acb->qiov, 0, acb->orig_buf, acb->qiov->size);
627628
qemu_vfree(acb->orig_buf);
628629
}
629630
acb->common.cb(acb->common.opaque, ret);

block/qcow2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ static void qcow_aio_read_cb(void *opaque, int ret)
482482
return;
483483
done:
484484
if (acb->qiov->niov > 1) {
485-
qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
485+
qemu_iovec_from_buf(acb->qiov, 0, acb->orig_buf, acb->qiov->size);
486486
qemu_vfree(acb->orig_buf);
487487
}
488488
acb->common.cb(acb->common.opaque, ret);
@@ -504,7 +504,7 @@ static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
504504
if (qiov->niov > 1) {
505505
acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
506506
if (is_write)
507-
qemu_iovec_to_buffer(qiov, acb->buf);
507+
qemu_iovec_to_buf(qiov, 0, acb->buf, qiov->size);
508508
} else {
509509
acb->buf = (uint8_t *)qiov->iov->iov_base;
510510
}

block/raw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
152152
b->opaque = opaque;
153153

154154
qemu_iovec_init(&b->qiov, qiov->nalloc);
155-
qemu_iovec_concat(&b->qiov, qiov, qiov->size);
155+
qemu_iovec_concat(&b->qiov, qiov, 0, qiov->size);
156156

157157
b->qiov.size -= 512;
158158
b->qiov.iov[first_buf_index].iov_base += 512;

hw/nvram/fw_cfg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ typedef struct _FWCfgEntry {
4545
FWCfgCallback callback;
4646
} FWCfgEntry;
4747

48-
typedef struct _FWCfgState {
48+
struct FWCfgState {
4949
FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
5050
uint16_t cur_entry;
5151
uint16_t cur_offset;
52-
} FWCfgState;
52+
};
5353

5454
static void fw_cfg_write(FWCfgState *s, uint8_t value)
5555
{

hw/pci/pci.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,10 @@ PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
839839
return NULL;
840840
}
841841

842-
typedef struct {
842+
struct PCIBridge {
843843
PCIDevice dev;
844844
PCIBus *bus;
845-
} PCIBridge;
845+
};
846846

847847
static void pci_bridge_write_config(PCIDevice *d,
848848
uint32_t address, uint32_t val, int len)

include/hw/qdev.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ typedef struct DeviceType DeviceType;
88

99
typedef struct DeviceProperty DeviceProperty;
1010

11-
typedef struct BusState BusState;
12-
1311
/* This structure should not be accessed directly. We declare it here
1412
so that it can be embedded in individual device state structures. */
1513
struct DeviceState {

include/qemu-common.h

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@
1414

1515
#include "qemu/compiler.h"
1616
#include "config-host.h"
17+
#include "qemu/typedefs.h"
1718

1819
#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
1920
#define WORDS_ALIGNED
2021
#endif
2122

2223
#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
2324

24-
25-
typedef struct QEMUTimer QEMUTimer;
26-
typedef struct QEMUFile QEMUFile;
27-
typedef struct QEMUBH QEMUBH;
28-
typedef struct DeviceState DeviceState;
29-
30-
struct Monitor;
31-
typedef struct Monitor Monitor;
32-
3325
/* we put basic includes here to avoid repeating them in device drivers */
3426
#include <stdlib.h>
3527
#include <stdio.h>
@@ -278,41 +270,6 @@ struct ParallelIOArg {
278270

279271
typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
280272

281-
/* A load of opaque types so that device init declarations don't have to
282-
pull in all the real definitions. */
283-
typedef struct NICInfo NICInfo;
284-
typedef struct HCIInfo HCIInfo;
285-
typedef struct AudioState AudioState;
286-
typedef struct BlockDriverState BlockDriverState;
287-
typedef struct DriveInfo DriveInfo;
288-
typedef struct DisplayState DisplayState;
289-
typedef struct DisplayChangeListener DisplayChangeListener;
290-
typedef struct DisplaySurface DisplaySurface;
291-
typedef struct DisplayAllocator DisplayAllocator;
292-
typedef struct PixelFormat PixelFormat;
293-
typedef struct TextConsole TextConsole;
294-
typedef TextConsole QEMUConsole;
295-
typedef struct CharDriverState CharDriverState;
296-
typedef struct MACAddr MACAddr;
297-
typedef struct VLANState VLANState;
298-
typedef struct VLANClientState VLANClientState;
299-
typedef struct i2c_bus i2c_bus;
300-
typedef struct i2c_slave i2c_slave;
301-
typedef struct SMBusDevice SMBusDevice;
302-
typedef struct PCIHostState PCIHostState;
303-
typedef struct PCIExpressHost PCIExpressHost;
304-
typedef struct PCIBus PCIBus;
305-
typedef struct PCIDevice PCIDevice;
306-
typedef struct SerialState SerialState;
307-
typedef struct IRQState *qemu_irq;
308-
typedef struct PCMCIACardState PCMCIACardState;
309-
typedef struct MouseTransformInfo MouseTransformInfo;
310-
typedef struct uWireSlave uWireSlave;
311-
typedef struct I2SCodec I2SCodec;
312-
typedef struct SSIBus SSIBus;
313-
typedef struct EventNotifier EventNotifier;
314-
typedef struct VirtIODevice VirtIODevice;
315-
316273
typedef uint64_t pcibus_t;
317274

318275
typedef enum {
@@ -377,16 +334,12 @@ typedef struct QEMUIOVector {
377334
void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
378335
void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
379336
void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
380-
void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
381-
size_t size);
382-
void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size);
337+
void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t offset, size_t size);
383338
void qemu_iovec_destroy(QEMUIOVector *qiov);
384339
void qemu_iovec_reset(QEMUIOVector *qiov);
385-
void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
386-
void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
387-
void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
388-
void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
389-
size_t skip);
340+
size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, void *buf, size_t count);
341+
size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset, const void *buf, size_t count);
342+
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset, int c, size_t count);
390343

391344
bool buffer_is_zero(const void *buf, size_t len);
392345

@@ -463,6 +416,14 @@ int64_t pow2floor(int64_t value);
463416
int uleb128_encode_small(uint8_t *out, uint32_t n);
464417
int uleb128_decode_small(const uint8_t *in, uint32_t *n);
465418

419+
/* unicode.c */
420+
int mod_utf8_codepoint(const char *s, size_t n, char **end);
421+
422+
/*
423+
* Hexdump a buffer to a file. An optional string prefix is added to every line
424+
*/
425+
426+
void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
466427

467428
/*
468429
* A fixer for timeout value passed to select() on Mac. The issue is that Mac's

include/qemu/iov.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Helpers for using (partial) iovecs.
3+
*
4+
* Copyright (C) 2010 Red Hat, Inc.
5+
*
6+
* Author(s):
7+
* Amit Shah <[email protected]>
8+
* Michael Tokarev <[email protected]>
9+
*
10+
* This work is licensed under the terms of the GNU GPL, version 2. See
11+
* the COPYING file in the top-level directory.
12+
*/
13+
14+
#ifndef IOV_H
15+
#define IOV_H
16+
17+
#include "qemu-common.h"
18+
19+
/**
20+
* count and return data size, in bytes, of an iovec
21+
* starting at `iov' of `iov_cnt' number of elements.
22+
*/
23+
size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
24+
25+
/**
26+
* Copy from single continuous buffer to scatter-gather vector of buffers
27+
* (iovec) and back like memcpy() between two continuous memory regions.
28+
* Data in single continuous buffer starting at address `buf' and
29+
* `bytes' bytes long will be copied to/from an iovec `iov' with
30+
* `iov_cnt' number of elements, starting at byte position `offset'
31+
* within the iovec. If the iovec does not contain enough space,
32+
* only part of data will be copied, up to the end of the iovec.
33+
* Number of bytes actually copied will be returned, which is
34+
* min(bytes, iov_size(iov)-offset)
35+
* `Offset' must point to the inside of iovec.
36+
* It is okay to use very large value for `bytes' since we're
37+
* limited by the size of the iovec anyway, provided that the
38+
* buffer pointed to by buf has enough space. One possible
39+
* such "large" value is -1 (sinice size_t is unsigned),
40+
* so specifying `-1' as `bytes' means 'up to the end of iovec'.
41+
*/
42+
size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
43+
size_t offset, const void *buf, size_t bytes);
44+
size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
45+
size_t offset, void *buf, size_t bytes);
46+
47+
/**
48+
* Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements,
49+
* starting at byte offset `start', to value `fillc', repeating it
50+
* `bytes' number of times. `Offset' must point to the inside of iovec.
51+
* If `bytes' is large enough, only last bytes portion of iovec,
52+
* up to the end of it, will be filled with the specified value.
53+
* Function return actual number of bytes processed, which is
54+
* min(size, iov_size(iov) - offset).
55+
* Again, it is okay to use large value for `bytes' to mean "up to the end".
56+
*/
57+
size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
58+
size_t offset, int fillc, size_t bytes);
59+
60+
/*
61+
* Send/recv data from/to iovec buffers directly
62+
*
63+
* `offset' bytes in the beginning of iovec buffer are skipped and
64+
* next `bytes' bytes are used, which must be within data of iovec.
65+
*
66+
* r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true);
67+
*
68+
* is logically equivalent to
69+
*
70+
* char *buf = malloc(bytes);
71+
* iov_to_buf(iov, iovcnt, offset, buf, bytes);
72+
* r = send(sockfd, buf, bytes, 0);
73+
* free(buf);
74+
*
75+
* For iov_send_recv() _whole_ area being sent or received
76+
* should be within the iovec, not only beginning of it.
77+
*/
78+
ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
79+
size_t offset, size_t bytes, bool do_send);
80+
#define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
81+
iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
82+
#define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
83+
iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
84+
85+
/**
86+
* Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements
87+
* in file `fp', prefixing each line with `prefix' and processing not more
88+
* than `limit' data bytes.
89+
*/
90+
void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
91+
FILE *fp, const char *prefix, size_t limit);
92+
93+
/*
94+
* Partial copy of vector from iov to dst_iov (data is not copied).
95+
* dst_iov overlaps iov at a specified offset.
96+
* size of dst_iov is at most bytes. dst vector count is returned.
97+
*/
98+
unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
99+
const struct iovec *iov, unsigned int iov_cnt,
100+
size_t offset, size_t bytes);
101+
102+
/*
103+
* Remove a given number of bytes from the front or back of a vector.
104+
* This may update iov and/or iov_cnt to exclude iovec elements that are
105+
* no longer required.
106+
*
107+
* The number of bytes actually discarded is returned. This number may be
108+
* smaller than requested if the vector is too small.
109+
*/
110+
size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
111+
size_t bytes);
112+
size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
113+
size_t bytes);
114+
115+
#endif

include/qemu/module.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ static void __attribute__((constructor)) do_qemu_init_ ## function(void) { \
2222

2323
typedef enum {
2424
MODULE_INIT_BLOCK,
25-
MODULE_INIT_DEVICE,
25+
MODULE_INIT_DEVICE, // TODO(digit): Remove this.
2626
MODULE_INIT_MACHINE,
27+
MODULE_INIT_QAPI,
28+
MODULE_INIT_QOM,
2729
MODULE_INIT_MAX
2830
} module_init_type;
2931

3032
#define block_init(function) module_init(function, MODULE_INIT_BLOCK)
31-
#define device_init(function) module_init(function, MODULE_INIT_DEVICE)
33+
#define device_init(function) module_init(function, MODULE_INIT_DEVICE) // TODO(digit): Remove this.
3234
#define machine_init(function) module_init(function, MODULE_INIT_MACHINE)
35+
#define qapi_init(function) module_init(function, MODULE_INIT_QAPI)
36+
#define type_init(function) module_init(function, MODULE_INIT_QOM)
3337

3438
void register_module_init(void (*fn)(void), module_init_type type);
3539

0 commit comments

Comments
 (0)