From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 06/22] qcow2: simplify qcow2_cache_put() and qcow2_cache_entry_mark_dirty()
Date: Fri, 22 May 2015 17:26:24 +0200 [thread overview]
Message-ID: <1432308400-13958-7-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1432308400-13958-1-git-send-email-kwolf@redhat.com>
From: Alberto Garcia <berto@igalia.com>
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 <berto@igalia.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
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;
}
--
1.8.3.1
next prev parent reply other threads:[~2015-05-22 15:27 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-22 15:26 [Qemu-devel] [PULL 00/22] Block layer core and image format patches Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 01/22] qcow2: Flush pending discards before allocating cluster Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 02/22] nvme: support NVME_VOLATILE_WRITE_CACHE feature Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 03/22] vmdk: Fix next_cluster_sector for compressed write Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 04/22] vmdk: Fix overflow if l1_size is 0x20000000 Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 05/22] qcow2: use one single memory block for the L2/refcount cache tables Kevin Wolf
2015-05-22 15:26 ` Kevin Wolf [this message]
2015-05-22 15:26 ` [Qemu-devel] [PULL 07/22] qcow2: use an LRU algorithm to replace entries from the L2 cache Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 08/22] qcow2: remove qcow2_cache_find_entry_to_replace() Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 09/22] qcow2: use a hash to look for entries in the L2 cache Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 10/22] qcow2: make qcow2_cache_put() a void function Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 11/22] qcow2: style fixes in qcow2-cache.c Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 12/22] qemu-io: Use getopt() correctly Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 13/22] block: Detect multiplication overflow in bdrv_getlength Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 14/22] qemu-iotests: qemu-img info on afl VMDK image with a huge capacity Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 15/22] qemu-iotests: Make debugging python tests easier Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 16/22] qcow2/qcow: protect against uninitialized encryption key Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 17/22] util: move read_password method out of qemu-img into osdep/oslib Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 18/22] util: allow \n to terminate password input Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 19/22] qemu-io: prompt for encryption keys when required Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 20/22] tests: add test case for encrypted qcow2 read/write Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 21/22] MAINTAINERS: Add header files to Block Layer Core section Kevin Wolf
2015-05-22 15:26 ` [Qemu-devel] [PULL 22/22] MAINTAINERS: Split "Block QAPI, monitor, command line" off core Kevin Wolf
2015-05-26 10:30 ` [Qemu-devel] [PULL 00/22] Block layer core and image format patches Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1432308400-13958-7-git-send-email-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).