Received: from eggs.gnu.org ([2001:4830:134:3::10]:52484) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YoByx-0002BC-8Q for qemu-devel@nongnu.org; Fri, 01 May 2015 10:32:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YoByt-0006k3-59 for qemu-devel@nongnu.org; Fri, 01 May 2015 10:31:59 -0400 Date: Fri, 1 May 2015 15:31:52 +0100 From: Stefan Hajnoczi Message-ID: <20150501143152.GC2386@stefanha-thinkpad.redhat.com> References: <65c04848cf88f3342e229464e72d1124ce3d8de9.1430388393.git.berto@igalia.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="1SQmhf2mF2YjsYvc" Content-Disposition: inline In-Reply-To: <65c04848cf88f3342e229464e72d1124ce3d8de9.1430388393.git.berto@igalia.com> Subject: Re: [Qemu-devel] [PATCH 2/6] qcow2: simplify qcow2_cache_put() and qcow2_cache_entry_mark_dirty() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alberto Garcia Cc: Kevin Wolf , Stefan Hajnoczi , qemu-devel@nongnu.org, qemu-block@nongnu.org --1SQmhf2mF2YjsYvc Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Apr 30, 2015 at 01:11:41PM +0300, Alberto Garcia wrote: > int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table) > { > - int i; > + int i =3D (*table - c->table_array) / c->table_size; > =20 > - for (i =3D 0; i < c->size; i++) { > - if (table_addr(c, i) =3D=3D *table) { > - goto found; > - } > + if (c->entries[i].offset =3D=3D 0) { > + return -ENOENT; > } > - return -ENOENT; > =20 > -found: > c->entries[i].ref--; > *table =3D NULL; > =20 When is this function called with a bogus table pointer? My hunch is there are no callers like that. I might be wrong. If there are no callers like that, then you can extract this and include an assertion instead: static int qcow2_cache_get_table_idx(Qcow2Cache *c, void *table) { assert(table >=3D c->table_array &&); table < c->table_array + c->size * c->table_size); return (table - c->table_array) / c->table_size; } Functions that go from void *table to int i should use this helper. And the qcow2_cache_put() users can drop their error handling code. A lot of them ignore the return value anyway, so it simplifies things even more. Stefan --1SQmhf2mF2YjsYvc Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJVQ45YAAoJEJykq7OBq3PI6dcIAIMQlVRH47OIk/cv6hU/SgCU feuMndZo+r2KvQz59M02PuluT31QDJjAX5DuvjYhl0GR1v91p5GLY74dZuceXbpd +j02Y71uQjbnjPbF8YrM/MPcCqY0xdaqj3DbFKhJ2/XXD+zZRcijhudHn4bWvUPK dqHCmi13Wh9WLSp/syGGiJN1YcZmVFV0qxXQR/zHUqfGiPKyEJQXVCsZDFGW5j9H IR2vQE1wXUQRmtn4YFLQxI37rlxw0rGPjLKClKaNlC/G7vdLhASDUmYQPYnFGyMV +sdTSQlJ62gzYseIPzz9QjHponhFZMgnvlkT9tGCZupzUxx1q8rS59vZIWatTzc= =aqUO -----END PGP SIGNATURE----- --1SQmhf2mF2YjsYvc--