4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
26 #include "qemu/sockets.h"
27 #include "block/coroutine.h"
28 #include "migration/migration.h"
29 #include "migration/qemu-file.h"
30 #include "migration/qemu-file-internal.h"
33 #define QSB_CHUNK_SIZE (1 << 10)
34 #define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
37 * Create a QEMUSizedBuffer
38 * This type of buffer uses scatter-gather lists internally and
39 * can grow to any size. Any data array in the scatter-gather list
40 * can hold different amount of bytes.
42 * @buffer: Optional buffer to copy into the QSB
43 * @len: size of initial buffer; if @buffer is given, buffer must
44 * hold at least len bytes
46 * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
48 QEMUSizedBuffer
*qsb_create(const uint8_t *buffer
, size_t len
)
51 size_t alloc_len
, num_chunks
, i
, to_copy
;
52 size_t chunk_size
= (len
> QSB_MAX_CHUNK_SIZE
)
56 num_chunks
= DIV_ROUND_UP(len
? len
: QSB_CHUNK_SIZE
, chunk_size
);
57 alloc_len
= num_chunks
* chunk_size
;
59 qsb
= g_try_new0(QEMUSizedBuffer
, 1);
64 qsb
->iov
= g_try_new0(struct iovec
, num_chunks
);
70 qsb
->n_iov
= num_chunks
;
72 for (i
= 0; i
< num_chunks
; i
++) {
73 qsb
->iov
[i
].iov_base
= g_try_malloc0(chunk_size
);
74 if (!qsb
->iov
[i
].iov_base
) {
75 /* qsb_free is safe since g_free can cope with NULL */
80 qsb
->iov
[i
].iov_len
= chunk_size
;
82 to_copy
= (len
- qsb
->used
) > chunk_size
83 ? chunk_size
: (len
- qsb
->used
);
84 memcpy(qsb
->iov
[i
].iov_base
, &buffer
[qsb
->used
], to_copy
);
89 qsb
->size
= alloc_len
;
95 * Free the QEMUSizedBuffer
97 * @qsb: The QEMUSizedBuffer to free
99 void qsb_free(QEMUSizedBuffer
*qsb
)
107 for (i
= 0; i
< qsb
->n_iov
; i
++) {
108 g_free(qsb
->iov
[i
].iov_base
);
115 * Get the number of used bytes in the QEMUSizedBuffer
117 * @qsb: A QEMUSizedBuffer
119 * Returns the number of bytes currently used in this buffer
121 size_t qsb_get_length(const QEMUSizedBuffer
*qsb
)
127 * Set the length of the buffer; the primary usage of this
128 * function is to truncate the number of used bytes in the buffer.
129 * The size will not be extended beyond the current number of
130 * allocated bytes in the QEMUSizedBuffer.
132 * @qsb: A QEMUSizedBuffer
133 * @new_len: The new length of bytes in the buffer
135 * Returns the number of bytes the buffer was truncated or extended
138 size_t qsb_set_length(QEMUSizedBuffer
*qsb
, size_t new_len
)
140 if (new_len
<= qsb
->size
) {
143 qsb
->used
= qsb
->size
;
149 * Get the iovec that holds the data for a given position @pos.
151 * @qsb: A QEMUSizedBuffer
152 * @pos: The index of a byte in the buffer
153 * @d_off: Pointer to an offset that this function will indicate
154 * at what position within the returned iovec the byte
157 * Returns the index of the iovec that holds the byte at the given
158 * index @pos in the byte stream; a negative number if the iovec
159 * for the given position @pos does not exist.
161 static ssize_t
qsb_get_iovec(const QEMUSizedBuffer
*qsb
,
162 off_t pos
, off_t
*d_off
)
167 if (pos
> qsb
->used
) {
171 for (i
= 0; i
< qsb
->n_iov
; i
++) {
172 if (curr
+ qsb
->iov
[i
].iov_len
> pos
) {
176 curr
+= qsb
->iov
[i
].iov_len
;
182 * Convert the QEMUSizedBuffer into a flat buffer.
184 * Note: If at all possible, try to avoid this function since it
185 * may unnecessarily copy memory around.
187 * @qsb: pointer to QEMUSizedBuffer
188 * @start: offset to start at
189 * @count: number of bytes to copy
190 * @buf: a pointer to a buffer to write into (at least @count bytes)
192 * Returns the number of bytes copied into the output buffer
194 ssize_t
qsb_get_buffer(const QEMUSizedBuffer
*qsb
, off_t start
,
195 size_t count
, uint8_t *buffer
)
197 const struct iovec
*iov
;
198 size_t to_copy
, all_copy
;
204 if (start
> qsb
->used
) {
208 all_copy
= qsb
->used
- start
;
209 if (all_copy
> count
) {
215 index
= qsb_get_iovec(qsb
, start
, &s_off
);
220 while (all_copy
> 0) {
221 iov
= &qsb
->iov
[index
];
225 to_copy
= iov
->iov_len
- s_off
;
226 if (to_copy
> all_copy
) {
229 memcpy(&buffer
[d_off
], &s
[s_off
], to_copy
);
242 * Grow the QEMUSizedBuffer to the given size and allocate
245 * @qsb: A QEMUSizedBuffer
246 * @new_size: The new size of the buffer
249 * a negative error code in case of memory allocation failure
251 * the new size of the buffer. The returned size may be greater or equal
254 static ssize_t
qsb_grow(QEMUSizedBuffer
*qsb
, size_t new_size
)
256 size_t needed_chunks
, i
;
258 if (qsb
->size
< new_size
) {
259 struct iovec
*new_iov
;
260 size_t size_diff
= new_size
- qsb
->size
;
261 size_t chunk_size
= (size_diff
> QSB_MAX_CHUNK_SIZE
)
262 ? QSB_MAX_CHUNK_SIZE
: QSB_CHUNK_SIZE
;
264 needed_chunks
= DIV_ROUND_UP(size_diff
, chunk_size
);
266 new_iov
= g_try_new(struct iovec
, qsb
->n_iov
+ needed_chunks
);
267 if (new_iov
== NULL
) {
271 /* Allocate new chunks as needed into new_iov */
272 for (i
= qsb
->n_iov
; i
< qsb
->n_iov
+ needed_chunks
; i
++) {
273 new_iov
[i
].iov_base
= g_try_malloc0(chunk_size
);
274 new_iov
[i
].iov_len
= chunk_size
;
275 if (!new_iov
[i
].iov_base
) {
278 /* Free previously allocated new chunks */
279 for (j
= qsb
->n_iov
; j
< i
; j
++) {
280 g_free(new_iov
[j
].iov_base
);
289 * Now we can't get any allocation errors, copy over to new iov
292 for (i
= 0; i
< qsb
->n_iov
; i
++) {
293 new_iov
[i
] = qsb
->iov
[i
];
296 qsb
->n_iov
+= needed_chunks
;
299 qsb
->size
+= (needed_chunks
* chunk_size
);
306 * Write into the QEMUSizedBuffer at a given position and a given
307 * number of bytes. This function will automatically grow the
310 * @qsb: A QEMUSizedBuffer
311 * @source: A byte array to copy data from
312 * @pos: The position within the @qsb to write data to
313 * @size: The number of bytes to copy into the @qsb
315 * Returns @size or a negative error code in case of memory allocation failure,
316 * or with an invalid 'pos'
318 ssize_t
qsb_write_at(QEMUSizedBuffer
*qsb
, const uint8_t *source
,
319 off_t pos
, size_t count
)
321 ssize_t rc
= qsb_grow(qsb
, pos
+ count
);
323 size_t all_copy
= count
;
324 const struct iovec
*iov
;
327 off_t d_off
, s_off
= 0;
333 if (pos
+ count
> qsb
->used
) {
334 qsb
->used
= pos
+ count
;
337 index
= qsb_get_iovec(qsb
, pos
, &d_off
);
342 while (all_copy
> 0) {
343 iov
= &qsb
->iov
[index
];
345 dest
= iov
->iov_base
;
347 to_copy
= iov
->iov_len
- d_off
;
348 if (to_copy
> all_copy
) {
352 memcpy(&dest
[d_off
], &source
[s_off
], to_copy
);
365 * Create a deep copy of the given QEMUSizedBuffer.
367 * @qsb: A QEMUSizedBuffer
369 * Returns a clone of @qsb or NULL on allocation failure
371 QEMUSizedBuffer
*qsb_clone(const QEMUSizedBuffer
*qsb
)
373 QEMUSizedBuffer
*out
= qsb_create(NULL
, qsb_get_length(qsb
));
382 for (i
= 0; i
< qsb
->n_iov
; i
++) {
383 res
= qsb_write_at(out
, qsb
->iov
[i
].iov_base
,
384 pos
, qsb
->iov
[i
].iov_len
);
395 typedef struct QEMUBuffer
{
396 QEMUSizedBuffer
*qsb
;
401 static int buf_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
403 QEMUBuffer
*s
= opaque
;
404 ssize_t len
= qsb_get_length(s
->qsb
) - pos
;
413 return qsb_get_buffer(s
->qsb
, pos
, len
, buf
);
416 static int buf_put_buffer(void *opaque
, const uint8_t *buf
,
417 int64_t pos
, int size
)
419 QEMUBuffer
*s
= opaque
;
421 return qsb_write_at(s
->qsb
, buf
, pos
, size
);
424 static int buf_close(void *opaque
)
426 QEMUBuffer
*s
= opaque
;
428 if (s
->qsb_allocated
) {
437 const QEMUSizedBuffer
*qemu_buf_get(QEMUFile
*f
)
448 static const QEMUFileOps buf_read_ops
= {
449 .get_buffer
= buf_get_buffer
,
453 static const QEMUFileOps buf_write_ops
= {
454 .put_buffer
= buf_put_buffer
,
458 QEMUFile
*qemu_bufopen(const char *mode
, QEMUSizedBuffer
*input
)
462 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') ||
464 error_report("qemu_bufopen: Argument validity check failed");
468 s
= g_malloc0(sizeof(QEMUBuffer
));
471 if (s
->qsb
== NULL
) {
472 s
->qsb
= qsb_create(NULL
, 0);
473 s
->qsb_allocated
= true;
477 error_report("qemu_bufopen: qsb_create failed");
482 if (mode
[0] == 'r') {
483 s
->file
= qemu_fopen_ops(s
, &buf_read_ops
);
485 s
->file
= qemu_fopen_ops(s
, &buf_write_ops
);