Move duplicated code into helper functions.
Signed-off-by: Gleb Natapov <address@hidden>
---
block-qcow2.c | 160 +++++++++++++++++++++++++--------------------------------
1 files changed, 70 insertions(+), 90 deletions(-)
diff --git a/block-qcow2.c b/block-qcow2.c
index 7f99921..0771281 100644
--- a/block-qcow2.c
+++ b/block-qcow2.c
@@ -61,6 +61,8 @@
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
+#define SIZE2CLUSTERS(Q, S) (((S) + ((Q)->cluster_size - 1)) >> (Q)->cluster_bits)
@@ -668,38 +693,23 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
cluster_offset = be64_to_cpu(l2_table[l2_index]);
- nb_available = s->cluster_sectors;
- l2_index++;
-
- if (!cluster_offset) {
-
- /* how many empty clusters ? */
-
- while (nb_available < nb_needed && !l2_table[l2_index]) {
- l2_index++;
- nb_available += s->cluster_sectors;
- }
- } else {
+ nb_clusters = SIZE2CLUSTERS(s, nb_needed << 9);
+ c = (!cluster_offset) ?
+ /* how many empty clusters ? */
+ count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]) :
/* how many allocated clusters ? */
+ count_contiguous_clusters(nb_clusters, s->cluster_size,
+ &l2_table[l2_index], QCOW_OFLAG_COPIED);
nb_available = nb_clusters << (s->cluster_bits - 9);
if (nb_available > n_end)
@@ -951,6 +937,7 @@ static uint64_t alloc_cluster_offset(BlockDriverState *bs,
cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
+
/* we must initialize the cluster content which won't be
written */