From 0a6cca945099b2b91cea2a7eb763ab44dd4129df Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 7 Dec 2015 14:28:09 +0100 Subject: [PATCH] device: use NM_UTILS_STRING_LOOKUP_TABLE() for reason_to_string() Showcase for the new macros NM_UTILS_STRING_LOOKUP_TABLE() and NM_UTILS_STRING_LOOKUP_TABLE_DEFINE_STATIC(). It changes behavior in case of looking up an invalid/unknown state reason. Previously it would just have returned "unknown" -- which was indistinguishable from a regular "unknown" value. Now it returns the numeric id as a string. The string is allocated with alloca(), which is desired but one should be aware of the pitfalls: - prevents the caller from being inlined - bad idea to do in a loop. --- src/devices/nm-device.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index a3e057c6d4..2eed430df0 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -442,7 +442,7 @@ state_to_string (NMDeviceState state) return queued_state_to_string (state) + strlen (QUEUED_PREFIX); } -static const char *reason_table[] = { +NM_UTILS_STRING_LOOKUP_TABLE_DEFINE_STATIC (_reason_to_string, NMDeviceStateReason, NULL, [NM_DEVICE_STATE_REASON_UNKNOWN] = "unknown", [NM_DEVICE_STATE_REASON_NONE] = "none", [NM_DEVICE_STATE_REASON_NOW_MANAGED] = "managed", @@ -506,15 +506,10 @@ static const char *reason_table[] = { [NM_DEVICE_STATE_REASON_NEW_ACTIVATION] = "new-activation", [NM_DEVICE_STATE_REASON_PARENT_CHANGED] = "parent-changed", [NM_DEVICE_STATE_REASON_PARENT_MANAGED_CHANGED] = "parent-managed-changed", -}; +); -static const char * -reason_to_string (NMDeviceStateReason reason) -{ - if ((gsize) reason < G_N_ELEMENTS (reason_table)) - return reason_table[reason]; - return reason_table[NM_DEVICE_STATE_REASON_UNKNOWN]; -} +#define reason_to_string(reason) \ + NM_UTILS_STRING_LOOKUP_TABLE (_reason_to_string, reason) /***********************************************************/