From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NGyRY-00062A-Do for qemu-devel@nongnu.org; Sat, 05 Dec 2009 12:29:16 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NGyRT-0005yn-Iv for qemu-devel@nongnu.org; Sat, 05 Dec 2009 12:29:15 -0500 Received: from [199.232.76.173] (port=34009 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NGyRT-0005yk-D0 for qemu-devel@nongnu.org; Sat, 05 Dec 2009 12:29:11 -0500 Received: from mail-gx0-f223.google.com ([209.85.217.223]:34501) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NGyRT-0008Pq-8d for qemu-devel@nongnu.org; Sat, 05 Dec 2009 12:29:11 -0500 Received: by gxk23 with SMTP id 23so704881gxk.2 for ; Sat, 05 Dec 2009 09:29:10 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <4B1A9359.8080305@redhat.com> References: <4B193DA5.6040507@codemonkey.ws> <4B1A9359.8080305@redhat.com> From: Blue Swirl Date: Sat, 5 Dec 2009 17:28:50 +0000 Message-ID: Subject: Re: [Qemu-devel] [PATCH] Permit zero-sized qemu_malloc() & friends Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Avi Kivity Cc: qemu-devel@nongnu.org, Paul Brook , Markus Armbruster On Sat, Dec 5, 2009 at 5:07 PM, Avi Kivity wrote: > On 12/04/2009 06:49 PM, Anthony Liguori wrote: >> >> I still believe that it is poor practice to pass size=3D=3D0 to *malloc(= ). =C2=A0I >> think actively discouraging this in qemu is a good thing because it's a >> broken idiom. > > Why? =C2=A0Unless we have a separate array allocator (like C++'s new and = new[]), > we need to support zero-element arrays without pushing the burden to call= ers > (in the same way that for () supports zero iteration loops without a > separate if ()). Running a loop zero or nonzero number of times always has a very clear and precise meaning. A pointer returned from allocating zero or nonzero number of items may be completely unusable or usable, respectively. I think Laurent's proposal would work. We even could go so far as rename the current function as qemu_malloc_possibly_broken (and adjust callers mechanically) and introduce two new versions, which handle the zero case in clearly advertised ways. Patches would fix the callers to use the correct one.