From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54385) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xu0hT-0000xP-H8 for qemu-devel@nongnu.org; Thu, 27 Nov 2014 10:09:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xu0hK-0003r5-Fh for qemu-devel@nongnu.org; Thu, 27 Nov 2014 10:09:43 -0500 Received: from mail-wi0-x22a.google.com ([2a00:1450:400c:c05::22a]:47633) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xu0hK-0003qz-91 for qemu-devel@nongnu.org; Thu, 27 Nov 2014 10:09:34 -0500 Received: by mail-wi0-f170.google.com with SMTP id bs8so18753452wib.5 for ; Thu, 27 Nov 2014 07:09:33 -0800 (PST) Date: Thu, 27 Nov 2014 15:09:31 +0000 From: Stefan Hajnoczi Message-ID: <20141127150931.GF15586@stefanha-thinkpad.lan> References: <1416503198-17031-1-git-send-email-mreitz@redhat.com> <1416503198-17031-7-git-send-email-mreitz@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="KJY2Ze80yH5MUxol" Content-Disposition: inline In-Reply-To: <1416503198-17031-7-git-send-email-mreitz@redhat.com> Subject: Re: [Qemu-devel] [PATCH v3 06/22] qcow2: Helper for refcount array reallocation List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi --KJY2Ze80yH5MUxol Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Nov 20, 2014 at 06:06:22PM +0100, Max Reitz wrote: > +/** > + * Reallocates *array so that it can hold new_size entries. *size must contain > + * the current number of entries in *array. If the reallocation fails, *array > + * and *size will not be modified and -errno will be returned. If the > + * reallocation is successful, *array will be set to the new buffer and *size > + * will be set to new_size. The size of the reallocated refcount array buffer > + * will be aligned to a cluster boundary, and the newly allocated area will be > + * zeroed. > + */ > +static int realloc_refcount_array(BDRVQcowState *s, uint16_t **array, > + int64_t *size, int64_t new_size) > +{ > + /* Round to clusters so the array can be directly written to disk */ > + size_t old_byte_size = ROUND_UP(refcount_array_byte_size(s, *size), > + s->cluster_size); > + size_t new_byte_size = ROUND_UP(refcount_array_byte_size(s, new_size), > + s->cluster_size); > + uint16_t *new_ptr; > + > + if (new_byte_size <= old_byte_size) { > + *size = new_size; > + return 0; > + } Why not realloc the array to the new smaller size? ... > + > + assert(new_byte_size > 0); > + > + new_ptr = g_try_realloc(*array, new_byte_size); > + if (!new_ptr) { > + return -ENOMEM; > + } > + > + memset((void *)((uintptr_t)new_ptr + old_byte_size), 0, > + new_byte_size - old_byte_size); ...we just need to skip the memset in when new_byte_size is smaller than old_byte_size. --KJY2Ze80yH5MUxol Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJUdz6rAAoJEJykq7OBq3PIH/AIALPZ98mKiYnrf0cO8wkgu+Ly WsI5jR5vJviX9YLHUN9eAZVWLEORi9zGhnnuKy37P1nVf3eo1sZzyY9et7c63BOE fd4/391vlD+/MLkUFfuwnxuVT34QiljxH9e4/xmHgu2F1XWJGjBM2eROJ/DD4+yU 1gSpp7mWuH0Vhlpi7Y1dTEZqHoILBiq+gXXo4KIKRHW+M53jphQAirfCUta0YuKQ ikDKMHp0moTWDm0/cReoVOrtDStg+1Hecic+2J/MCVfxWQ71wPCOTP2xaQjCa0xd TxLLe4NrSoNHvE7Ozhlmi2KW2tklVFDksVTcCIaFuO2P/dF3j7Rgv0cOfa+Cgk8= =U+X0 -----END PGP SIGNATURE----- --KJY2Ze80yH5MUxol--