Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird if condition in c: if( ((void)(d), 0) ) {...}

Tags:

c

I read Xen source code and saw something like this:

#define is_domain_direct_mapped(d) ((void)(d), 0)

is_domain_direct_mapped is then used in a if statement as follows (d is a pointer to a struct variable):

if( is_domain_direct_mapped(d) ) {...}

So after compiler replaces is_domain_direct_mapped with its definition, we have something like:

if( ((void)(d), 0) ) {...}

The above if statement is very weird to me. This is the first time I see this kind of statement. How is it supposed to work?

like image 377
Bao Bui Avatar asked Jan 03 '23 09:01

Bao Bui


1 Answers

It’s meant to always produce 0 but also evaluate d, in case it’s something with side-effects like p++.

As for why you would want to always produce 0: the implementation is different on ARM.

#define is_domain_direct_mapped(d) ((d) == hardware_domain && dom0_11_mapping)

Notice how d is evaluated exactly once here too.

Some discussion from when this was first introduced as confirmation – Re: [Xen-devel] [PATCH v3 1/3] xen/x86: introduce is_domain_direct_mapped(d) as (0) on x86:

When I've implemented this defined in common/memory.c, Jan told me to use [1]:

#define is_domain_is_direct_mapped(d) ((void)(d), 0)

I suspect you want the same things here.

referencing Re: [v4] xen/arm: Allow balooning working with 1:1 memory mapping:

And you'll want to make sure (even if unlikely to be needed in practice) that you evaluate the macro argument exactly once in both flavors, i.e. in the case here

#define is_domain_direct_mapped(d) ((void)(d), 0)
like image 184
Ry- Avatar answered Jan 14 '23 00:01

Ry-