diff --git a/include/qemu/buffer.h b/include/qemu/buffer.h index f53ee9e6af1d19db949a46cdadef2c31bc01e8fc..1358df1aac4e1a2164fc4064536fcb5f348c92ef 100644 --- a/include/qemu/buffer.h +++ b/include/qemu/buffer.h @@ -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__ */ diff --git a/util/buffer.c b/util/buffer.c index c7a39ec5daae56971b453efc3fb5f735c95e8228..e8f798e62010d60aa259f1d31a124639b2812b45 100644 --- a/util/buffer.c +++ b/util/buffer.c @@ -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; +}