From: Eric Blake <eblake@redhat.com>
To: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>,
qemu-devel@nongnu.org
Cc: sanidhya.iiith@gmail.com, joel.schopp@amd.com,
stefanb@linux.vnet.ibm.com, arei.gonglei@huawei.com,
quintela@redhat.com
Subject: Re: [Qemu-devel] [PATCH v4 1/2] QEMUSizedBuffer based QEMUFile
Date: Fri, 26 Sep 2014 21:34:17 -0600 [thread overview]
Message-ID: <54263039.1080007@redhat.com> (raw)
In-Reply-To: <1410953167-9558-2-git-send-email-dgilbert@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 4499 bytes --]
On 09/17/2014 05:26 AM, Dr. David Alan Gilbert (git) wrote:
> From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
>
> This is based on Stefan and Joel's patch that creates a QEMUFile that goes
> to a memory buffer; from:
>
> http://lists.gnu.org/archive/html/qemu-devel/2013-03/msg05036.html
Sheesh - a year and a half ago, and still not ready to merge.
>
> Using the QEMUFile interface, this patch adds support functions for
> operating on in-memory sized buffers that can be written to or read from.
>
> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
> Signed-off-by: Joel Schopp <jschopp@linux.vnet.ibm.com>
>
> For fixes/tweeks I've done:
> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> ---
> include/migration/qemu-file.h | 28 +++
> include/qemu/typedefs.h | 1 +
> qemu-file.c | 457 ++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 486 insertions(+)
>
> +QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
> + for (i = 0; i < num_chunks; i++) {
> + qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
> + if (!qsb->iov[i].iov_base) {
> + size_t j;
> +
> + for (j = 0; j < i; j++) {
> + g_free(qsb->iov[j].iov_base);
> + }
> + g_free(qsb->iov);
> + g_free(qsb);
> + return NULL;
Rather than inlining all this cleanup, you could just call
qsb_free(qsb). But I can live with this version too.
> +/**
> + * Grow the QEMUSizedBuffer to the given size and allocated
> + * memory for it.
s/allocated/allocate/ ?
> + *
> + * @qsb: A QEMUSizedBuffer
> + * @new_size: The new size of the buffer
> + *
> + * Returns an error code in case of memory allocation failure
s/an error code/a negative error code/ ?
> + * or the new size of the buffer otherwise. The returned size
> + * may be greater or equal to @new_size.
> + */
> +static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
> +{
> + size_t needed_chunks, i;
> +
> + if (qsb->size < new_size) {
> + struct iovec *new_iov;
> + size_t size_diff = new_size - qsb->size;
> + size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
> + ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
> +
> + needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
> +
> + new_iov = g_try_malloc_n(qsb->n_iov + needed_chunks,
> + sizeof(struct iovec));
Indentation is off.
> +
> +const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
> +{
> + QEMUBuffer *p;
> +
> + qemu_fflush(f);
> +
> + p = (QEMUBuffer *)f->opaque;
Cast is not necessary (this is C, after all, not C++).
> +
> + return p->qsb;
> +}
> +
> +static const QEMUFileOps buf_read_ops = {
> + .get_buffer = buf_get_buffer,
> + .close = buf_close
> +};
I think we prefer trailing commas, if only so that future additions
don't have to modify existing lines.
> +
> +static const QEMUFileOps buf_write_ops = {
> + .put_buffer = buf_put_buffer,
> + .close = buf_close
> +};
and again.
> +
> +QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
> +{
> + QEMUBuffer *s;
> +
> + if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
I prefer '\0' over 0 when comparing characters. Or shorthand such as
'|| *mode[1]'. But it works as is.
> + error_report("qemu_bufopen: Argument validity check failed");
> + return NULL;
> + }
> +
> + s = g_malloc0(sizeof(QEMUBuffer));
> + if (mode[0] == 'r') {
> + s->qsb = input;
> + }
> +
> + if (s->qsb == NULL) {
> + s->qsb = qsb_create(NULL, 0);
> + }
> + if (!s->qsb) {
> + error_report("qemu_bufopen: qsb_create failed");
> + return NULL;
Memory leak of s.
> + }
> +
> +
> + if (mode[0] == 'r') {
> + s->file = qemu_fopen_ops(s, &buf_read_ops);
> + } else {
> + s->file = qemu_fopen_ops(s, &buf_write_ops);
> + }
> + return s->file;
> +}
>
Closer; most of my findings are minor, but the memleak needs fixing. If
the only changes you make are in relation to my findings, then v5 can
start life with Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]
next prev parent reply other threads:[~2014-09-27 3:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-17 11:26 [Qemu-devel] [PATCH v4 0/2] In memory QEMUFile Dr. David Alan Gilbert (git)
2014-09-17 11:26 ` [Qemu-devel] [PATCH v4 1/2] QEMUSizedBuffer based QEMUFile Dr. David Alan Gilbert (git)
2014-09-27 3:34 ` Eric Blake [this message]
2014-09-29 8:45 ` Dr. David Alan Gilbert
2014-09-17 11:26 ` [Qemu-devel] [PATCH v4 2/2] Tests: QEMUSizedBuffer/QEMUBuffer Dr. David Alan Gilbert (git)
2014-09-27 3:36 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=54263039.1080007@redhat.com \
--to=eblake@redhat.com \
--cc=arei.gonglei@huawei.com \
--cc=dgilbert@redhat.com \
--cc=joel.schopp@amd.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=sanidhya.iiith@gmail.com \
--cc=stefanb@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).