Skip to content

Add new CMP(a, b) macro - #9789

Merged
poettering merged 6 commits into
systemd:masterfrom
filbranden:cmp1
Aug 7, 2018
Merged

poettering merged 6 commits into
systemd:masterfrom
filbranden:cmp1

Conversation

@filbranden

Copy link
Copy Markdown
Contributor

Follow up for PRs #9764 and #9760.

Macro returns -1, 0, 1 depending on whether a < b, a == b or a > b.

Converted compare_funcs (functions named *_compare_func() to use CMP() wherever possible.

With two exceptions:

  • dns_resource_record_compare_func(): doesn't do full logic of CMP() currently, has a comment about not implementing proper ordering.
  • dns_name_compare_func(): has a return r - q but for a specific case (r < 0 && q < 0).

I'm open to converting these two as well, just wanted to check with you first...

I added test coverage for CMP() itself, confirmed it works with integer and pointer types, also safe on unsigned types.

@filbranden
filbranden requested review from keszybz and yuwata August 3, 2018 05:06
@yuwata

yuwata commented Aug 3, 2018

Copy link
Copy Markdown
Member

Looks great!

However, unfortunately, the macro does not decrease the number of lines so much. I am not sure that is acceptable widely, but I would like to also define CMP_return(a, b) which also returns the function when a and b are different. What do you think?

Comment thread src/basic/in-addr-util.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

if (x->family != y->family)
     return CMP(x->family, y->family)

?
This is slightly shorter and doesn't require a temporary variable. The compiler should optimize the repeated check away without any trouble.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think of that... But in the end I saw some compare_func's that use strcmp assigning to an r variable, which made me prefer to use the if (r != 0) form since it could match use of those functions too. I ended up used that form everywhere, for consistency.

See @yuwata's suggestion on CMP_return() and my follow up on return_if_nonzero() (which could also be used for strcmp and memcmp), I think that would be a more general way to solve this. WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so far we don't have a return_xyz() family of macros, but we do have assert_return_xyz() which might return internally. I am not really sure what the best approach here is, and if we should add generic return_if_nonzero(), since I usually don't like such "hidden" jumps too much... (I wasn't even sure about the assert_return_xyz() case, but I think it's more well defined, since it usually appears as part of the header of a function only).

Hence, maybe we should call it RETURN_CMP_VERDICT() or so, which hopefully signals that it only does something when a verdict is found (i.e. when the predicate is non-zero), but we make it specific enough for the comparison case so that we don't end up with it all over the place in other areas...

#define RETURN_CMP_VERDICT(x) \
        ({  \
                typeof(x) y = x; \
                if (y != 0) \
                        return y; \
        )}

or something like this?

You could then call it like this:

RETURN_CMP_VERDICT(CMP(foo, bar));
RETURN_CMP_VERDICT(strcmp(x, y));
…

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we start using || for this?

int in_addr_data_compare_func(const void *a, const void *b) {
        const struct in_addr_data *x = a, *y = b;
        return (CMP(x->family, y->family) ||
                memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family)));
}

And:

static int routing_policy_rule_compare_func(const void *_a, const void *_b) {
…
        case AF_INET6:
                return (CMP(a->from_prefixlen, b->from_prefixlen) ||
                        CMP(a->to_prefixlen, b->to_prefixlen) ||
                        CMP(a->tos, b->tos) ||
                        CMP(a->fwmask, b->fwmask) ||
                        CMP(a->table, b->table) ||
                        strcmp_ptr(a->iif, b->iif) ||
                        strcmp_ptr(a->oif, b->oif) ||
                        memcmp(&a->from, &b->from, FAMILY_ADDRESS_SIZE(a->family)) ||
                        memcmp(&a->to, &b->to, FAMILY_ADDRESS_SIZE(a->family)));

Only thing here is that you need to understand how || inteacts with 0 and non-zero integers... But considering we have variables that can take true, false or -1, that's not much different :-)

This accomplishes terseness on the code, without having to introduce macros that result in breaking control flow (returning sometimes, not always.)

We wouldn't solve all cases here, but many of them would work.

In any case, I'm leaning towards not doing this in the same PR, but just doing CMP() for now (maybe even in the cases where this increases the total number of lines from 2 to 3) and then we can revisit this in a follow up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not following? The || operator always resolves to 0 or 1, no? never to -1? what am i missing? when sorting we need the order between two values, and hence || can't work, or I am missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, my bad. I thought it worked the same as in Perl or Python, where it returns the first non-zero value...

Though maybe a variadic C macro that would do just that might be a good approach here, perhaps?

Comment thread src/basic/macro.h Outdated

@poettering poettering Aug 3, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need __extension__ really, we don't compile with -pedantic... We use all sorts of gcc extensions, hence we don't need to mark their use in particular...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was asking myself about it... But in the end decided to follow what MAX, MIN and the other friends do... Should we remove it from those as well?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i figure, yes, please

@poettering

Copy link
Copy Markdown
Member

This is pretty, I like it.

@poettering

Copy link
Copy Markdown
Member

dns_resource_record_compare_func(): doesn't do full logic of CMP() currently, has a comment about not implementing proper ordering.

This one is indeed a bit sloppy. Because we don't want to implement proper ordering (and don't need it) we just compare the pointers in the end. Which I figure you could also compare with CMP()...

dns_name_compare_func(): has a return r - q but for a specific case (r < 0 && q < 0).

Yeah, it's a dirty hack took. comparison functions are not supposed to fail, so if the unescaping fails we ultimately just compare error codes to make things stable... i figure you could use CMP for this bit too..

@filbranden

Copy link
Copy Markdown
Contributor Author

I would like to also define CMP_return(a, b) which also returns the function when a and b are different. What do you think?

I was thinking about that... How about a more general return_if_nonzero(...) that could be used in more places, such as with strcmp and memcmp too?

dns_resource_record_compare_func().

This one is indeed a bit sloppy. Because we don't want to implement proper ordering (and don't need it) we just compare the pointers in the end. Which I figure you could also compare with CMP()...

So... Should I go ahead and replace with CMP()? Leave the comment on, remove it, rephrase it? Let me know what you'd prefer.

@yuwata

yuwata commented Aug 6, 2018

Copy link
Copy Markdown
Member

I was thinking about that... How about a more general return_if_nonzero(...) that could be used in more places, such as with strcmp and memcmp too?

@filbranden I do not have any strong opinion about that.

@poettering

Copy link
Copy Markdown
Member

So... Should I go ahead and replace with CMP()? Leave the comment on, remove it, rephrase it? Let me know what you'd prefer.

leave comment, but make the CMP change

Drop __extension__, since we don't use gcc -Wpedantic or -ansi.

Reformat code for spacing. Add spaces after commas almost everywhere.
Reindent code blocks in macro definitions, for consistency.
Macro returns -1, 0, 1 depending on whether a < b, a == b or a > b.

It's safe to use on unsigned types.

Add tests to confirm corner cases are properly covered.
Looked for definitions of functions using the *_compare_func() suffix.

Tested:
- Unit tests passed (ninja -C build/ test)
- Installed this build and booted with it.
This function doesn't really implement ordering, but CMP() is still fine to use
there. Keep the comment in place, just update it slightly to indicate that.
@filbranden

Copy link
Copy Markdown
Contributor Author

Ok, so:

  1. Dropped __extension__ and did some whitespace treatment on macro.h
  2. Fixed the two extra cases.

I'm leaving the other ideas of RETURN_IF_NONZERO, RETURN_CMP_VERDICT or || for a follow up... (Looking into spatch, see if maybe I can cook a recipe for it, let's see...) I'd prefer if we can settle on introducing CMP() now, and then refactor this in a follow up.

Cheers!
Filipe

@poettering
poettering merged commit 2ed7449 into systemd:master Aug 7, 2018
@evverx

evverx commented Aug 7, 2018

Copy link
Copy Markdown
Contributor

There seems to be a huge warning at https://objectstorage.prodstack4-5.canonical.com/v1/AUTH_77e2ada1e7a84929a74ba3b87153c0ac/autopkgtest-bionic-pitti-systemd-semaphore/bionic/amd64/s/systemd-upstream/20180807_040022_7c819@/log.gz that probably has something to do with the PR:

util@exe/src_test_test-util.c.o' -c ../src/test/test-util.c
In file included from ../src/basic/util.h:27:0,
                 from ../src/basic/def.h:4,
                 from ../src/test/test-util.c:8:
../src/test/test-util.c: In function ‘test_max’:
../src/basic/macro.h:230:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 :    \
                               ^
../src/basic/macro.h:26:44: note: in definition of macro ‘_unlikely_’
 #define _unlikely_(x) (__builtin_expect(!!(x), 0))
                                            ^
../src/basic/macro.h:301:25: note: in expansion of macro ‘assert_message_se’
 #define assert_se(expr) assert_message_se(expr, #expr)
                         ^~~~~~~~~~~~~~~~~
../src/test/test-util.c:96:9: note: in expansion of macro ‘assert_se’
         assert_se(CMP(UINT64_MAX, 0L) == 1);
         ^~~~~~~~~
../src/basic/macro.h:225:19: note: in expansion of macro ‘__CMP’
 #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
                   ^~~~~
../src/test/test-util.c:96:19: note: in expansion of macro ‘CMP’
         assert_se(CMP(UINT64_MAX, 0L) == 1);
                   ^~~
../src/basic/macro.h:231:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0;  \
                               ^
../src/basic/macro.h:26:44: note: in definition of macro ‘_unlikely_’
 #define _unlikely_(x) (__builtin_expect(!!(x), 0))
                                            ^
../src/basic/macro.h:301:25: note: in expansion of macro ‘assert_message_se’
 #define assert_se(expr) assert_message_se(expr, #expr)
                         ^~~~~~~~~~~~~~~~~
../src/test/test-util.c:96:9: note: in expansion of macro ‘assert_se’
         assert_se(CMP(UINT64_MAX, 0L) == 1);
         ^~~~~~~~~
../src/basic/macro.h:225:19: note: in expansion of macro ‘__CMP’
 #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
                   ^~~~~
../src/test/test-util.c:96:19: note: in expansion of macro ‘CMP’
         assert_se(CMP(UINT64_MAX, 0L) == 1);
                   ^~~
../src/basic/macro.h:230:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 :    \
                               ^
../src/basic/macro.h:26:44: note: in definition of macro ‘_unlikely_’
 #define _unlikely_(x) (__builtin_expect(!!(x), 0))
                                            ^
../src/basic/macro.h:301:25: note: in expansion of macro ‘assert_message_se’
 #define assert_se(expr) assert_message_se(expr, #expr)
                         ^~~~~~~~~~~~~~~~~
../src/test/test-util.c:97:9: note: in expansion of macro ‘assert_se’
         assert_se(CMP(0L, UINT64_MAX) == -1);
         ^~~~~~~~~
../src/basic/macro.h:225:19: note: in expansion of macro ‘__CMP’
 #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
                   ^~~~~
../src/test/test-util.c:97:19: note: in expansion of macro ‘CMP’
         assert_se(CMP(0L, UINT64_MAX) == -1);
                   ^~~
../src/basic/macro.h:231:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0;  \
                               ^
../src/basic/macro.h:26:44: note: in definition of macro ‘_unlikely_’
 #define _unlikely_(x) (__builtin_expect(!!(x), 0))
                                            ^
../src/basic/macro.h:301:25: note: in expansion of macro ‘assert_message_se’
 #define assert_se(expr) assert_message_se(expr, #expr)
                         ^~~~~~~~~~~~~~~~~
../src/test/test-util.c:97:9: note: in expansion of macro ‘assert_se’
         assert_se(CMP(0L, UINT64_MAX) == -1);
         ^~~~~~~~~
../src/basic/macro.h:225:19: note: in expansion of macro ‘__CMP’
 #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
                   ^~~~~
../src/test/test-util.c:97:19: note: in expansion of macro ‘CMP’
         assert_se(CMP(0L, UINT64_MAX) == -1);
                   ^~~

@evverx

evverx commented Aug 7, 2018

Copy link
Copy Markdown
Contributor

Though it's not that big compared to what a C++ compiler usually emits :-)

yuwata added a commit to yuwata/systemd that referenced this pull request Aug 7, 2018
@yuwata

yuwata commented Aug 7, 2018

Copy link
Copy Markdown
Member

@evverx I've created PR #9823.

@evverx

evverx commented Aug 7, 2018

Copy link
Copy Markdown
Contributor

@yuwata thank you! I think it would be best if @filbranden reviewed it, because I haven't even finished reading the warning yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants