From 3b18b6c03d0243d9d161d17b875b0bb4c6587f1e Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Tue, 21 May 2019 04:48:30 +0200 Subject: lib/table: remove nonsensical const, add pure Passing the struct route_table *ptr as const doesn't really help; if anything it semantically would imply that the returned route_node is const too since constness should propagate (but it doesn't in C.) The right thing to do here - which actually helps the compiler optimize the code too - is to tag functions with __attribute__((pure)). The compiler does this automatically if it has the function body (and the body of all called functions) available. That should cover most "static inline" functions in headers, as well as functions in the same file. However, this doesn't work (at least without LTO) for extern functions. Hence, add "ext_pure" for this case. (Built-in "extern" to make lines shorter.) Signed-off-by: David Lamparter --- lib/compiler.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/compiler.h') diff --git a/lib/compiler.h b/lib/compiler.h index 474adc7c8b..c2e57db7f8 100644 --- a/lib/compiler.h +++ b/lib/compiler.h @@ -76,6 +76,15 @@ extern "C" { #define _DEPRECATED(x) deprecated #endif +/* pure = function does not modify memory & return value is the same if + * memory hasn't changed (=> allows compiler to optimize) + * + * Mostly autodetected by the compiler if function body is available (i.e. + * static inline functions in headers). Since that implies it should only be + * used in headers for non-inline functions, the "extern" is included here. + */ +#define ext_pure extern __attribute__((pure)) + /* for helper functions defined inside macros */ #define macro_inline static inline __attribute__((unused)) #define macro_pure static inline __attribute__((unused, pure)) -- cgit v1.2.3