From patchwork Wed May 6 13:39:26 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 468937 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 3C6F41402BF for ; Wed, 6 May 2015 23:44:04 +1000 (AEST) Received: from localhost ([::1]:45177 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpzcI-0005b5-Hc for incoming@patchwork.ozlabs.org; Wed, 06 May 2015 09:44:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39931) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpzYw-0000AU-4Q for qemu-devel@nongnu.org; Wed, 06 May 2015 09:40:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YpzYp-0001Qw-ML for qemu-devel@nongnu.org; Wed, 06 May 2015 09:40:33 -0400 Received: from smtp3.mundo-r.com ([212.51.32.191]:22610 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpzYo-00017H-V1; Wed, 06 May 2015 09:40:27 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtQFAEMZSlVbdWOb/2dsb2JhbABcgwyBL7NtAQEBAwUBgQKYSgKBJUwBAQEBAQGBC4QhAQEEJ1IQUTwbGYgwAcUYAQEIIoYXiicHFoQXBYZUhTWQdIxUiWQjYIEnHIFVOzGCRQEBAQ X-IPAS-Result: AtQFAEMZSlVbdWOb/2dsb2JhbABcgwyBL7NtAQEBAwUBgQKYSgKBJUwBAQEBAQGBC4QhAQEEJ1IQUTwbGYgwAcUYAQEIIoYXiicHFoQXBYZUhTWQdIxUiWQjYIEnHIFVOzGCRQEBAQ X-IronPort-AV: E=Sophos;i="5.13,379,1427752800"; d="scan'208";a="354101277" Received: from fanzine.igalia.com ([91.117.99.155]) by smtp4.mundo-r.com with ESMTP; 06 May 2015 15:39:48 +0200 Received: from maestria.local.igalia.com ([192.168.10.14] helo=mail.igalia.com) by fanzine.igalia.com with esmtps (Cipher TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim) id 1YpzYC-0003bF-93; Wed, 06 May 2015 15:39:48 +0200 Received: from fanzine.local.igalia.com ([192.168.10.13] helo=perseus.local) by mail.igalia.com with esmtps (Cipher TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim) id 1YpzYB-0006RB-Nr; Wed, 06 May 2015 15:39:48 +0200 Received: from berto by perseus.local with local (Exim 4.85) (envelope-from ) id 1YpzYA-0006u0-7t; Wed, 06 May 2015 16:39:46 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Wed, 6 May 2015 16:39:26 +0300 Message-Id: <1af76cc582835127135d1e2bbb7a26dbceafd15f.1430919406.git.berto@igalia.com> 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, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 2/7] 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; }