From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52586) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zs8WX-0004Pl-Ud for qemu-devel@nongnu.org; Fri, 30 Oct 2015 08:11:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zs8WU-0002rH-Ov for qemu-devel@nongnu.org; Fri, 30 Oct 2015 08:11:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32902) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zs8WU-0002rC-Js for qemu-devel@nongnu.org; Fri, 30 Oct 2015 08:11:10 -0400 Date: Fri, 30 Oct 2015 21:11:01 +0900 From: "Daniel P. Berrange" Message-ID: <20151030121101.GC20961@redhat.com> References: <1446203414-4013-1-git-send-email-kraxel@redhat.com> <1446203414-4013-4-git-send-email-kraxel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1446203414-4013-4-git-send-email-kraxel@redhat.com> Subject: Re: [Qemu-devel] [PATCH 03/19] buffer: add buffer_move_empty Reply-To: "Daniel P. Berrange" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gerd Hoffmann Cc: pl@kamp.de, qemu-devel@nongnu.org On Fri, Oct 30, 2015 at 12:09:58PM +0100, Gerd Hoffmann wrote: > Signed-off-by: Gerd Hoffmann > Reviewed-by: Peter Lieven > --- > include/qemu/buffer.h | 10 ++++++++++ > util/buffer.c | 14 ++++++++++++++ > 2 files changed, 24 insertions(+) > > diff --git a/include/qemu/buffer.h b/include/qemu/buffer.h > index 0710e16..f53ee9e 100644 > --- a/include/qemu/buffer.h > +++ b/include/qemu/buffer.h > @@ -127,4 +127,14 @@ uint8_t *buffer_end(Buffer *buffer); > */ > gboolean buffer_empty(Buffer *buffer); > > +/** > + * buffer_move_empty: > + * @to: destination buffer object > + * @from: source buffer object > + * > + * Moves buffer, without copying data. 'to' buffer must be empty. Hmm, on the one hand the code do 'assert(to->offset) == 0', but on the other hand it does 'g_free(to->buffer)' as if it expected the buffer to have existing data. If you just remove the 'assert', then there's no real requirement for 'to' to be empty, as we'd do the right thin in free'ing the data. Then we can just change this API doc to say "Any existing data in "to" will be freed" > + * 'from' buffer is empty and zero-sized on return. > + */ > +void buffer_move_empty(Buffer *to, Buffer *from); > + > #endif /* QEMU_BUFFER_H__ */ > diff --git a/util/buffer.c b/util/buffer.c > index 12bf2d7..c7a39ec 100644 > --- a/util/buffer.c > +++ b/util/buffer.c > @@ -77,3 +77,17 @@ void buffer_advance(Buffer *buffer, size_t len) > (buffer->offset - len)); > buffer->offset -= len; > } > + > +void buffer_move_empty(Buffer *to, Buffer *from) > +{ > + assert(to->offset == 0); > + > + g_free(to->buffer); > + to->offset = from->offset; > + to->capacity = from->capacity; > + to->buffer = from->buffer; > + > + from->offset = 0; > + from->capacity = 0; > + from->buffer = NULL; > +} Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|