From patchwork Mon May 11 12:54:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 470770 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5B712140157 for ; Mon, 11 May 2015 22:59:29 +1000 (AEST) Received: from localhost ([::1]:37147 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YrnIt-0005AO-H8 for incoming@patchwork.ozlabs.org; Mon, 11 May 2015 08:59:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59054) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YrnFc-0007k1-3j for qemu-devel@nongnu.org; Mon, 11 May 2015 08:56:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YrnFW-0005WB-SX for qemu-devel@nongnu.org; Mon, 11 May 2015 08:56:03 -0400 Received: from smtp3.mundo-r.com ([212.51.32.191]:32157 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YrnFW-0005LU-Ha; Mon, 11 May 2015 08:55:58 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ai0IAKOlUFVbdWOb/2dsb2JhbABcgw+BMrJvAQEBAQEBBQGBBAGYVgKBL0wBAQEBAQGBC4QhAQEEJ1IQPxI8GxmIMAHHMAErhhaKKAcWhBcFhl+FOJEJjGiJbyNhZkIcgVVsgkYBAQE X-IPAS-Result: Ai0IAKOlUFVbdWOb/2dsb2JhbABcgw+BMrJvAQEBAQEBBQGBBAGYVgKBL0wBAQEBAQGBC4QhAQEEJ1IQPxI8GxmIMAHHMAErhhaKKAcWhBcFhl+FOJEJjGiJbyNhZkIcgVVsgkYBAQE X-IronPort-AV: E=Sophos;i="5.13,407,1427752800"; d="scan'208";a="348478592" Received: from fanzine.igalia.com ([91.117.99.155]) by smtp4.mundo-r.com with ESMTP; 11 May 2015 14:55:20 +0200 Received: from dsl-hkibrasgw4-50df50-128.dhcp.inet.fi ([80.223.80.128] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim) id 1YrnEt-0006Et-TR; Mon, 11 May 2015 14:55:20 +0200 Received: from berto by perseus.local with local (Exim 4.85) (envelope-from ) id 1YrnEg-0002ML-Mp; Mon, 11 May 2015 15:55:06 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Mon, 11 May 2015 15:54:54 +0300 Message-Id: X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 212.51.32.191 Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 2/8] qcow2: simplify qcow2_cache_put() and qcow2_cache_entry_mark_dirty() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Since all tables are now stored together, it is possible to obtain the position of a particular table directly from its address, so the operation becomes O(1). Signed-off-by: Alberto Garcia Reviewed-by: Stefan Hajnoczi Reviewed-by: Max Reitz --- block/qcow2-cache.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index f0dfb69..6e78c8f 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -49,6 +49,16 @@ static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs, return (uint8_t *) c->table_array + (size_t) table * s->cluster_size; } +static inline int qcow2_cache_get_table_idx(BlockDriverState *bs, + Qcow2Cache *c, void *table) +{ + BDRVQcowState *s = bs->opaque; + ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array; + int idx = table_offset / s->cluster_size; + assert(idx >= 0 && idx < c->size && table_offset % s->cluster_size == 0); + return idx; +} + Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) { BDRVQcowState *s = bs->opaque; @@ -337,16 +347,12 @@ int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table) { - int i; + int i = qcow2_cache_get_table_idx(bs, c, *table); - for (i = 0; i < c->size; i++) { - if (qcow2_cache_get_table_addr(bs, c, i) == *table) { - goto found; - } + if (c->entries[i].offset == 0) { + return -ENOENT; } - return -ENOENT; -found: c->entries[i].ref--; *table = NULL; @@ -357,15 +363,7 @@ found: void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c, void *table) { - int i; - - for (i = 0; i < c->size; i++) { - if (qcow2_cache_get_table_addr(bs, c, i) == table) { - goto found; - } - } - abort(); - -found: + int i = qcow2_cache_get_table_idx(bs, c, table); + assert(c->entries[i].offset != 0); c->entries[i].dirty = true; }