From 249a2737ba5d0a22f09f077b361d01d514eaf672 Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Wed, 22 Jul 2015 17:05:41 +0200 Subject: basic: better readable IN_SET macro Putting the set elements in an array variable and using ELEMENTSOF makes it clearer what's going on. Incidentally, it also makes gcc -O2 generate slightly smaller code: "size systemd", before: text data bss dec hex filename 1378318 128608 2632 1509558 1708b6 systemd After: text data bss dec hex filename 1377286 128608 2632 1508526 1704ae systemd --- src/basic/macro.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/basic/macro.h b/src/basic/macro.h index 5fa17ed208..8fae4d0ede 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -406,12 +406,12 @@ do { \ #define IN_SET(x, y, ...) \ ({ \ - const typeof(y) _y = (y); \ - const typeof(_y) _x = (x); \ + const typeof(y) _array[] = { (y), __VA_ARGS__ }; \ + const typeof(y) _x = (x); \ unsigned _i; \ bool _found = false; \ - for (_i = 0; _i < 1 + sizeof((const typeof(_x)[]) { __VA_ARGS__ })/sizeof(const typeof(_x)); _i++) \ - if (((const typeof(_x)[]) { _y, __VA_ARGS__ })[_i] == _x) { \ + for (_i = 0; _i < ELEMENTSOF(_array); _i++) \ + if (_array[_i] == _x) { \ _found = true; \ break; \ } \ -- cgit v1.2.3-54-g00ecf From 77247cba3e8f94a3f58d16ec338ae030eb9145af Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Wed, 22 Jul 2015 17:50:44 +0200 Subject: basic: more optimizable IN_SET macro Making the array static allows gcc -O2 to generate smaller code: "size systemd" before: text data bss dec hex filename 1377286 128608 2632 1508526 1704ae systemd After: text data bss dec hex filename 1374326 128572 2664 1505562 16f91a systemd (IN_SET still results in worse generated code than using "x == FOO || x == BAR || ...". I don't think we'll be able to match that with the C preprocessor.) This change limits the use of IN_SET to sets with constant elements. All present callers use constants. The compiler would report an "initializer element is not constant" error otherwise. --- src/basic/macro.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/basic/macro.h b/src/basic/macro.h index 8fae4d0ede..ea01d701d2 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -406,7 +406,7 @@ do { \ #define IN_SET(x, y, ...) \ ({ \ - const typeof(y) _array[] = { (y), __VA_ARGS__ }; \ + static const typeof(y) _array[] = { (y), __VA_ARGS__ }; \ const typeof(y) _x = (x); \ unsigned _i; \ bool _found = false; \ -- cgit v1.2.3-54-g00ecf