chiark / gitweb /
util: use a shared lookup function for string tables
authorBruno Bottazzini <bruno.bottazzini@intel.com>
Fri, 13 Feb 2015 20:40:50 +0000 (18:40 -0200)
committerDavid Herrmann <dh.herrmann@gmail.com>
Sat, 14 Feb 2015 13:32:27 +0000 (14:32 +0100)
Macro DEFINE_STRING_TABLE_LOOKUP expands to a new function for each
of the almost 120 tables throghout the code.
Move the its implementation to a function (guaranteed to never be inlined),
and make the macro expand to an inlined function that calls this function.
This saves a few kilobytes from the systemd binary

(David: - fix coding-style
        - use 'ssize_t' to fix 32bit to 64bit propagation
        - use streq_ptr())

src/shared/util.c
src/shared/util.h

index 3a633512473d7d7c93d0574ed9b27064490f7a3f..ba035caed064315363af1f2358fb6a5b16f246ad 100644 (file)
@@ -8089,3 +8089,16 @@ int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
         *p += k;
         return 1;
 }
+
+ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) {
+        size_t i;
+
+        if (!key)
+                return -1;
+
+        for (i = 0; i < len; ++i)
+                if (streq_ptr(table[i], key))
+                        return (ssize_t)i;
+
+        return -1;
+}
index b56ffbde45657c3121c82450c6330c53afde3153..a83b5882214bf933f94e7c5da0360986fa8a185b 100644 (file)
@@ -355,16 +355,11 @@ static inline uint32_t random_u32(void) {
                 return name##_table[i];                                 \
         }
 
-#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
-        scope type name##_from_string(const char *s) {                  \
-                type i;                                                 \
-                if (!s)                                                 \
-                        return (type) -1;                               \
-                for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
-                        if (name##_table[i] &&                          \
-                            streq(name##_table[i], s))                  \
-                                return i;                               \
-                return (type) -1;                                       \
+ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
+
+#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)                                \
+        scope inline type name##_from_string(const char *s) {                                   \
+                return (type)string_table_lookup(name##_table, ELEMENTSOF(name##_table), s);    \
         }
 
 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                    \