Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MA1Fu-00069Q-6d for qemu-devel@nongnu.org; Fri, 29 May 2009 08:32:14 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MA1Fp-00066H-Cv for qemu-devel@nongnu.org; Fri, 29 May 2009 08:32:13 -0400 Received: from [199.232.76.173] (port=54320 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MA1Fp-000669-42 for qemu-devel@nongnu.org; Fri, 29 May 2009 08:32:09 -0400 Received: from mx2.redhat.com ([66.187.237.31]:34251) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MA1Fo-0005eM-IE for qemu-devel@nongnu.org; Fri, 29 May 2009 08:32:08 -0400 Subject: Re: [Qemu-devel] [PATCH] use qemu_malloc and friends consistently References: <9974029.69061243598442059.JavaMail.root@srv-05.w4a.fr> From: Markus Armbruster Date: Fri, 29 May 2009 14:32:04 +0200 In-Reply-To: <9974029.69061243598442059.JavaMail.root@srv-05.w4a.fr> (jcd@tribudubois.net's message of "Fri\, 29 May 2009 13\:00\:42 +0100 \(GMT+01\:00\)") Message-ID: <87hbz4t7i3.fsf@pike.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: jcd@tribudubois.net Cc: Kevin Wolf , qemu-devel@nongnu.org jcd@tribudubois.net writes: [...] > It seems to be just a fact that all malloc implementation are not > compatible in their behavior. None of these implementations are > necessarily wrong, but we just need to decide what is the allowed > behavior for the qemu "platform" and then make sure that the > qemu_malloc() implemetation is consistent on all underlying platforms. No. malloc() implements the same, well-known spec everywhere. Some people don't understand or don't like the spec, but that doesn't make implementations of the spec incompatible. Here's what you can expect from malloc(): * After p = malloc(SIZE), you can access p[i] for 0 <= i < SIZE. It follows that you must not dereference p at all if SIZE == 0. * If malloc(SIZE) returns a null pointer, and SIZE != 0, then malloc() failed. * Any result of malloc() can safely be passed to free(). A wrapper like qemu_malloc() can implement a different spec, of course. But only if the spec is compatible, the wrapper can be used as a drop-in replacement. Making it gratuitously incompatible is foolish, because it *breaks* code that works just fine with malloc(). We already agreed on one compatible difference: qemu_malloc() shall not fail, but just terminate the program. We're debating another difference, namely what qemu_malloc(0) shall do. Two fine compatible options have been proposed here: 1. qemu_malloc(0) shall return a null pointer. 2. qemu_malloc(0) shall return a non-null pointer that can be passed to qemu_free() The one that got committed is incompatible.