From owner-freebsd-usb@FreeBSD.ORG Thu Aug 17 15:19:29 2006 Return-Path: X-Original-To: usb@freebsd.org Delivered-To: freebsd-usb@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id F07DA16A4DA for ; Thu, 17 Aug 2006 15:19:29 +0000 (UTC) (envelope-from imp@bsdimp.com) Received: from harmony.bsdimp.com (vc4-2-0-87.dsl.netrack.net [199.45.160.85]) by mx1.FreeBSD.org (Postfix) with ESMTP id F0E0A43D49 for ; Thu, 17 Aug 2006 15:19:25 +0000 (GMT) (envelope-from imp@bsdimp.com) Received: from localhost (localhost.village.org [127.0.0.1] (may be forged)) by harmony.bsdimp.com (8.13.4/8.13.4) with ESMTP id k7HFGxPu012812; Thu, 17 Aug 2006 09:16:59 -0600 (MDT) (envelope-from imp@bsdimp.com) Date: Thu, 17 Aug 2006 09:17:00 -0600 (MDT) Message-Id: <20060817.091700.-432836888.imp@bsdimp.com> To: tofik@oxygen.az From: "M. Warner Losh" In-Reply-To: <35146.85.132.32.38.1155817977.squirrel@85.132.32.38> References: <35069.85.132.32.38.1155816624.squirrel@85.132.32.38> <20060817121307.GA804@gremlin.foo.is> <35146.85.132.32.38.1155817977.squirrel@85.132.32.38> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0 (harmony.bsdimp.com [127.0.0.1]); Thu, 17 Aug 2006 09:16:59 -0600 (MDT) Cc: usb@freebsd.org Subject: Re: USB_ATTACH_SETUP macros question X-BeenThere: freebsd-usb@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: FreeBSD support for USB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Aug 2006 15:19:30 -0000 In message: <35146.85.132.32.38.1155817977.squirrel@85.132.32.38> "Tofig Suleymanov" writes: : : > On Thu, Aug 17, 2006 at 05:10:02PM +0500, Tofig Suleymanov wrote: : >> Hello hackers, : >> : >> is there anybody to explain why do we have infinite loop inside of : >> USB_ATTACH_SETUP macros inside of usb_port.h ? How does his loop gets : >> escaped when we use it in some usb driver ? : >> : >> #define USB_ATTACH_SETUP \ : >> do { \ : >> sc->sc_dev = self; \ : >> device_set_desc_copy(self, devinfo); \ : >> device_printf(self, "%s\n", devinfo); \ : >> } while (0); : >> : >> Thank you ! : >> : >> Tofig Suleymanov : >> : > This isn't an infinite loop, in fact it will never be executed. : > : > Baldur : > : : Made a little test which shows that this code is going to be executed only : once.How did i miss that :) Now the other question comes next: : Why don not we use just: : #define USB_ATTACH_SETUP \ : sc->sc_dev = self; \ : device_set_desc_copy(self, devinfo); \ : device_printf(self, "%s\n", devinfo); Actually, it is neither an infinite loop, nor is it a never execute loop. the do { ... } while (0); is a way to make a macro lexically identical to a function call so that if you do something like: if (blah) USB_ATTACH_SETUP; it will parse as you intended. Which is to say it will parse exactly the same as: if (blah) { USB_ATTACH_STUP; } Without the do while construct, your example would cause different results in the latter and former cases. Warner