Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 18 Aug 2006 11:38:01 +0500 (AZST)
From:      "Tofig Suleymanov" <tofik@oxygen.az>
To:        "M. Warner Losh" <imp@bsdimp.com>
Cc:        usb@freebsd.org
Subject:   Re: USB_ATTACH_SETUP macros question
Message-ID:  <38357.85.132.32.38.1155883103.squirrel@85.132.32.38>
In-Reply-To: <20060817.091700.-432836888.imp@bsdimp.com>
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> <20060817.091700.-432836888.imp@bsdimp.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> In message: <35146.85.132.32.38.1155817977.squirrel@85.132.32.38>
>             "Tofig Suleymanov" <tofik@oxygen.az> 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
> _______________________________________________
> freebsd-usb@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-usb
> To unsubscribe, send any mail to "freebsd-usb-unsubscribe@freebsd.org"
>
>

Thank you,
now it is clear to me.

Sincerely,
Tofig Suleymanov




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?38357.85.132.32.38.1155883103.squirrel>