diff --git a/include/qemu/buffer.h b/include/qemu/buffer.h index 0710e165f2..f53ee9e6af 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. + * '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 12bf2d788b..c7a39ec5da 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; +}