From mboxrd@z Thu Jan 1 00:00:00 1970 From: Anthony Liguori Subject: Re: question regarding gnu-isms Date: Thu, 27 Apr 2006 18:04:01 -0500 Message-ID: <44514DE1.3000201@us.ibm.com> References: <20060427223641.GG3572@vino.zko.hp.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <20060427223641.GG3572@vino.zko.hp.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org Aron Griffis wrote: > I understand most of the gcc extensions that I find in xen, for > example ({...}) in a #define. However I've come across a couple that > I'm not familiar with. Does somebody mind explaining the point of > these? > > from linux-2.6-xen-sparse/include/asm-ia64/hypervisor.h: > > #define __pte_ma(_x) ((pte_t) {(_x)}) > This is structure initialization assignment. pte_t is a struct. It can be initialized like: pte_t pte = {0}; If you want to assign to it with a similar structure, you cannot do: pte_t pte = {0}; pte = {3}; The GNU extension allows you to do this by doing: pte_t pte = {0}; pte = (pte_t){3}; > What's the point of the braces here? > > same file: > > #define xen_create_contiguous_region(vstart, order, address_bits) ({0;}) > This is a really common one that lets you make statements into expressions. Say you wanted to implement a min function over int's as a macro, to do it properly, you have to do something like: #define min(a, b) {int lhs = a; int rhs = b; return (lhs < rhs) ? lhs : rhs; } But clearly you cannot use return for this (and you cannot avoid making a statement here). The GNU ({}) syntax allows you to have statements within an expression and the value of the very last statement in the block becomes the value of the expression. The above could be written: #define min(a, b) ({int lhs = a; int rhs = b; (lhs < rhs) ? lhs : rhs;}) All of these are documented in the GCC Info page (see the section on C Extensions). Regards, Anthony Liguori > This looks like the usual ({...}) construction but seems > pointless. The nearest explanation I could gather for this > applies only to C++... surely I'm missing something. :-) > > Thanks, > Aron > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xensource.com > http://lists.xensource.com/xen-devel >