Repository navigation
Add new CMP(a, b) macro - #9789
Conversation
|
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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));
…There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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?
|
This is pretty, I like it. |
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()...
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.. |
I was thinking about that... How about a more general
So... Should I go ahead and replace with CMP()? Leave the comment on, remove it, rephrase it? Let me know what you'd prefer. |
@filbranden I do not have any strong opinion about that. |
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.
Follow up for PRs systemd#9764 and systemd#9760.
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.
|
Ok, so:
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! |
|
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);
^~~ |
|
Though it's not that big compared to what a C++ compiler usually emits :-) |
|
@yuwata thank you! I think it would be best if @filbranden reviewed it, because I haven't even finished reading the warning yet. |
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 useCMP()wherever possible.With two exceptions:
dns_resource_record_compare_func(): doesn't do full logic ofCMP()currently, has a comment about not implementing proper ordering.dns_name_compare_func(): has areturn r - qbut 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.