qcow2: do not allocate extra memory
PATCH v2 1/1 qcow2: do not allocate extra memory
There are no needs to allocate more than one cluster, as we set avail_out for deflate to one cluster.
Zlib docs (http://www.zlib.net/manual.html) says:
"deflate compresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full."
So, deflate will not write more than avail_out to output buffer. If there is no enough space in output buffer for compressed data (it may be larger than input data) deflate just returns Z_OK. (if all data is compressed and written to output buffer deflate returns Z_STREAM_END).
---
v2: improve commit message
block/qcow.c | 2 +- block/qcow2.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
block/qcow.c +1 -1
@@ -983,7 +983,7 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,return ret; } - out_buf =g_malloc (s->cluster_size + (s->cluster_size /1000 ) +128 ); + out_buf =g_malloc (s->cluster_size);/* best compression, small window, no zlib header */ memset (&strm,0 ,sizeof (strm));
block/qcow2.c +1 -1
@@ -2612,7 +2612,7 @@ static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,return ret; } - out_buf =g_malloc (s->cluster_size + (s->cluster_size /1000 ) +128 ); + out_buf =g_malloc (s->cluster_size);/* best compression, small window, no zlib header */ memset (&strm,0 ,sizeof (strm));
Signature · 1 line
1.8.3.1
Quoted 9 lines · Vladimir Sementsov-Ogievskiy
There are no needs to allocate more than one cluster, as we set
avail_outfor deflate to one cluster.Zlib docs (http://www.zlib.net/manual.html) says:
"deflate compresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full."So, deflate will not write more than
avail_outto output buffer. If there is no enough space in output buffer for compressed data (it may be
s/no/not/
Quoted 5 lines
larger than input data) deflate just returns
Z_OK. (if all data is compressed and written to output buffer deflate returnsZ_STREAM_END).Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 40 lines · Vladimir Sementsov-Ogievskiy
There are no needs to allocate more than one cluster, as we set
avail_outfor deflate to one cluster.Zlib docs (http://www.zlib.net/manual.html) says:
"deflate compresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full."So, deflate will not write more than
avail_outto output buffer. If there is no enough space in output buffer for compressed data (it may be larger than input data) deflate just returnsZ_OK. (if all data is compressed and written to output buffer deflate returnsZ_STREAM_END).Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
v2: improve commit message
block/qcow.c | 2 +- block/qcow2.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)block/qcow.c +1
-1@@ -983,7 +983,7 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,return ret; } - out_buf =g_malloc (s->cluster_size + (s->cluster_size /1000 ) +128 ); + out_buf =g_malloc (s->cluster_size);/* best compression, small window, no zlib header */ memset (&strm,0 ,sizeof (strm));block/qcow2.c +0
-0
block/qcow2.c static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num, · line 2618
Quoted 8 lines
block/qcow2.c +1
-1@@ -2612,7 +2612,7 @@ static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,return ret; } - out_buf =g_malloc (s->cluster_size + (s->cluster_size /1000 ) +128 ); + out_buf =g_malloc (s->cluster_size);/* best compression, small window, no zlib header */ memset (&strm,0 ,sizeof (strm));
Quoted 12 lines · Vladimir Sementsov-Ogievskiy
There are no needs to allocate more than one cluster, as we set
avail_outfor deflate to one cluster.Zlib docs (http://www.zlib.net/manual.html) says:
"deflate compresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full."So, deflate will not write more than
avail_outto output buffer. If there is no enough space in output buffer for compressed data (it may be larger than input data) deflate just returnsZ_OK. (if all data is compressed and written to output buffer deflate returnsZ_STREAM_END).
Quoted 8 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
v2: improve commit message
block/qcow.c | 2 +- block/qcow2.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Thanks Vladimir, applied to my block branch (with s/no/not/ as proposed by Eric):
https://github.com/XanClic/qemu/commits/block
Max