From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L39Uh-0002y1-2R for qemu-devel@nongnu.org; Thu, 20 Nov 2008 08:22:51 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L39Uf-0002vf-7b for qemu-devel@nongnu.org; Thu, 20 Nov 2008 08:22:50 -0500 Received: from [199.232.76.173] (port=37217 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L39Uf-0002vU-1t for qemu-devel@nongnu.org; Thu, 20 Nov 2008 08:22:49 -0500 Received: from lizzard.sbs.de ([194.138.37.39]:16550) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1L39Uf-0000M7-1w for qemu-devel@nongnu.org; Thu, 20 Nov 2008 08:22:49 -0500 Received: from mail2.sbs.de (localhost [127.0.0.1]) by lizzard.sbs.de (8.12.11.20060308/8.12.11) with ESMTP id mAKDMiMO017364 for ; Thu, 20 Nov 2008 14:22:44 +0100 Received: from [146.254.149.50] (mchn012c.med.siemens.de [146.254.149.50] (may be forged)) by mail2.sbs.de (8.12.11.20060308/8.12.11) with ESMTP id mAKDMinU020825 for ; Thu, 20 Nov 2008 14:22:44 +0100 Message-ID: <49256477.9020003@siemens.com> Date: Thu, 20 Nov 2008 14:21:59 +0100 From: Jan Kiszka MIME-Version: 1.0 References: <20081119134857.26075.2417.stgit@mchn012c.ww002.siemens.net> <20081119134857.26075.9428.stgit@mchn012c.ww002.siemens.net> <492429F3.3010202@codemonkey.ws> <492435B9.3010106@siemens.com> <49244376.7020009@codemonkey.ws> In-Reply-To: <49244376.7020009@codemonkey.ws> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] Re: [PATCH 1/2] Add TAILQ_FOREACH_SAFE Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Anthony Liguori wrote: > Jan Kiszka wrote: >> Won't fly, next_var can become NULL and would be dereferenced without a >> prior check. Unless I'm totally blind now, there is no >> TAILQ_FOREACH_SAFE without "GCC-ism" > > Wouldn't: > > (var) ? ({ (next_var) = ((var)->field.tqe_next); 1;}) :0; > > Be equivalent to: > > (var) ? ((next_var = ((var)->field.tqe_next), var) : var > Yes, indeed. I mixed up that the evaluation order of ,-separated expressions is not guaranteed, while it is well-defined that the last operand delivers the result. So let's use this: ----------> Add TAILQ iterator that allows to safely remove elements while walking the list. Signed-off-by: Jan Kiszka --- sys-queue.h | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-) diff --git a/sys-queue.h b/sys-queue.h index 3d0773e..e2d3ae7 100644 --- a/sys-queue.h +++ b/sys-queue.h @@ -210,6 +210,11 @@ struct { \ (var); \ (var) = ((var)->field.tqe_next)) +#define TAILQ_FOREACH_SAFE(var, head, field, next_var) \ + for ((var) = ((head)->tqh_first); \ + (var) ? ((next_var) = ((var)->field.tqe_next), 1) : 0; \ + (var) = (next_var)) + #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \ (var); \