Skip to content

Commit

Permalink
buffer: add buffer_move
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Peter Lieven <pl@kamp.de>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Message-id: 1446203414-4013-5-git-send-email-kraxel@redhat.com
  • Loading branch information
kraxel committed Nov 5, 2015
1 parent 4d1eb5f commit 830a958
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/qemu/buffer.h
Expand Up @@ -137,4 +137,14 @@ gboolean buffer_empty(Buffer *buffer);
*/
void buffer_move_empty(Buffer *to, Buffer *from);

/**
* buffer_move:
* @to: destination buffer object
* @from: source buffer object
*
* Moves buffer, copying data (unless 'to' buffer happens to be empty).
* 'from' buffer is empty and zero-sized on return.
*/
void buffer_move(Buffer *to, Buffer *from);

#endif /* QEMU_BUFFER_H__ */
16 changes: 16 additions & 0 deletions util/buffer.c
Expand Up @@ -91,3 +91,19 @@ void buffer_move_empty(Buffer *to, Buffer *from)
from->capacity = 0;
from->buffer = NULL;
}

void buffer_move(Buffer *to, Buffer *from)
{
if (to->offset == 0) {
buffer_move_empty(to, from);
return;
}

buffer_reserve(to, from->offset);
buffer_append(to, from->buffer, from->offset);

g_free(from->buffer);
from->offset = 0;
from->capacity = 0;
from->buffer = NULL;
}

0 comments on commit 830a958

Please sign in to comment.