From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43630) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YJgEV-0004OS-GN for qemu-devel@nongnu.org; Fri, 06 Feb 2015 05:34:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YJgEU-0000Go-Gu for qemu-devel@nongnu.org; Fri, 06 Feb 2015 05:33:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40267) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YJgEU-0000Gk-A2 for qemu-devel@nongnu.org; Fri, 06 Feb 2015 05:33:54 -0500 Date: Fri, 6 Feb 2015 10:33:47 +0000 From: "Dr. David Alan Gilbert" Message-ID: <20150206103347.GD2364@work-vm> References: <1422875149-13198-1-git-send-email-liang.z.li@intel.com> <1422875149-13198-5-git-send-email-liang.z.li@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1422875149-13198-5-git-send-email-liang.z.li@intel.com> Subject: Re: [Qemu-devel] [v4 04/13] qemu-file: Add compression functions to QEMUFile List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Liang Li Cc: quintela@redhat.com, armbru@redhat.com, qemu-devel@nongnu.org, lcapitulino@redhat.com, Yang Zhang , amit.shah@redhat.com, dgilbert@redhat.com * Liang Li (liang.z.li@intel.com) wrote: > qemu_put_compression_data() compress the data and put it to QEMUFile. > qemu_put_qemu_file() put the data in the buffer of source QEMUFile to > destination QEMUFile. > > Signed-off-by: Liang Li > Signed-off-by: Yang Zhang > --- > include/migration/qemu-file.h | 3 +++ > migration/qemu-file.c | 39 +++++++++++++++++++++++++++++++++++++++ > 2 files changed, 42 insertions(+) > > diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h > index d843c00..2204fb9 100644 > --- a/include/migration/qemu-file.h > +++ b/include/migration/qemu-file.h > @@ -159,6 +159,9 @@ void qemu_put_be32(QEMUFile *f, unsigned int v); > void qemu_put_be64(QEMUFile *f, uint64_t v); > int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset); > int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size); > +size_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size, > + int level); > +int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src); > /* > * Note that you can only peek continuous bytes from where the current pointer > * is; you aren't guaranteed to be able to peak to +n bytes unless you've > diff --git a/migration/qemu-file.c b/migration/qemu-file.c > index edc2830..de2da2d 100644 > --- a/migration/qemu-file.c > +++ b/migration/qemu-file.c > @@ -21,6 +21,7 @@ > * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN > * THE SOFTWARE. > */ > +#include > #include "qemu-common.h" > #include "qemu/iov.h" > #include "qemu/sockets.h" > @@ -529,3 +530,41 @@ uint64_t qemu_get_be64(QEMUFile *f) > v |= qemu_get_be32(f); > return v; > } > + > +/* compress size bytes of data start at p with specific compression > + * leve and store the compressed data to the buffer of f. Typo: 'leve' > + */ > + > +size_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size, > + int level) > +{ > + size_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); > + > + if (blen < compressBound(size)) { > + return 0; > + } Imagine that there were only 3 bytes space in the buffer; so that IO_BUF_SIZE - f->buf_index = 3 then you subtract sizeof(int32_t) and get -1, but size_t is unsigned so the calculation works out as a very big number, and that comparison fails and it carries on to call compress2. > + if (compress2(f->buf + f->buf_index + sizeof(int32_t), &blen, (Bytef *)p, > + size, level) != Z_OK) { > + error_report("Compress Failed!"); > + return 0; > + } > + qemu_put_be32(f, blen); > + f->buf_index += blen; > + return blen + sizeof(int32_t); > +} > + > +/* Put the data in the buffer of f_src to the buffer of f_des, and > + * then reset the buf_index of f_src to 0. > + */ > + > +int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) > +{ > + int len = 0; > + > + if (f_src->buf_index > 0) { > + len = f_src->buf_index; > + qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); > + f_src->buf_index = 0; > + } > + return len; > +} > -- > 1.9.1 > -- Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK