History

UAPI: ndctl: Fix g++-unsupported initialisation in headers

The following code in the linux/ndctl header file: static inline const char *nvdimm_bus_cmd_name(unsigned cmd) { static const char * const names[] = { [ND_CMD_ARS_CAP] = "ars_cap", [ND_CMD_ARS_START] = "ars_start", [ND_CMD_ARS_STATUS] = "ars_status", [ND_CMD_CLEAR_ERROR] = "clear_error", [ND_CMD_CALL] = "cmd_call", }; if (cmd < ARRAY_SIZE(names) && names[cmd]) return names[cmd]; return "unknown"; } is broken in a number of ways: (1) ARRAY_SIZE() is not generally defined. (2) g++ does not support "non-trivial" array initialisers fully yet. (3) Every file that calls this function will acquire a copy of names[]. The same goes for nvdimm_cmd_name(). Fix all three by converting to a switch statement where each case returns a string. That way if cmd is a constant, the compiler can trivially reduce it and, if not, the compiler can use a shared lookup table if it thinks that is more efficient. A better way would be to remove these functions and their arrays from the header entirely.

David Howells committed 8 years ago

Commit 96078711 parent f1101761 file · +21 −27Browse files

Showing the changes to include/uapi/linux (1 of 1 file). Show all

Unified · Split

include/uapi/linux/ndctl.h+21−27
@@ -128,37 +128,31 @@ enum { · 127 unmodified lines
128 128
129static inline const char *nvdimm_bus_cmd_name(unsigned cmd)129static inline const char *nvdimm_bus_cmd_name(unsigned cmd)
130{130{
131 static const char * const names[] = {131 switch (cmd) {
132 [ND_CMD_ARS_CAP] = "ars_cap",132 case ND_CMD_ARS_CAP: return "ars_cap";
133 [ND_CMD_ARS_START] = "ars_start",133 case ND_CMD_ARS_START: return "ars_start";
134 [ND_CMD_ARS_STATUS] = "ars_status",134 case ND_CMD_ARS_STATUS: return "ars_status";
135 [ND_CMD_CLEAR_ERROR] = "clear_error",135 case ND_CMD_CLEAR_ERROR: return "clear_error";
136 [ND_CMD_CALL] = "cmd_call",136 case ND_CMD_CALL: return "cmd_call";
137 };137 default: return "unknown";
138 138 }
139 if (cmd < ARRAY_SIZE(names) && names[cmd])
140 return names[cmd];
141 return "unknown";
142}139}
143 140
144static inline const char *nvdimm_cmd_name(unsigned cmd)141static inline const char *nvdimm_cmd_name(unsigned cmd)
145{142{
146 static const char * const names[] = {143 switch (cmd) {
147 [ND_CMD_SMART] = "smart",144 case ND_CMD_SMART: return "smart";
148 [ND_CMD_SMART_THRESHOLD] = "smart_thresh",145 case ND_CMD_SMART_THRESHOLD: return "smart_thresh";
149 [ND_CMD_DIMM_FLAGS] = "flags",146 case ND_CMD_DIMM_FLAGS: return "flags";
150 [ND_CMD_GET_CONFIG_SIZE] = "get_size",147 case ND_CMD_GET_CONFIG_SIZE: return "get_size";
151 [ND_CMD_GET_CONFIG_DATA] = "get_data",148 case ND_CMD_GET_CONFIG_DATA: return "get_data";
152 [ND_CMD_SET_CONFIG_DATA] = "set_data",149 case ND_CMD_SET_CONFIG_DATA: return "set_data";
153 [ND_CMD_VENDOR_EFFECT_LOG_SIZE] = "effect_size",150 case ND_CMD_VENDOR_EFFECT_LOG_SIZE: return "effect_size";
154 [ND_CMD_VENDOR_EFFECT_LOG] = "effect_log",151 case ND_CMD_VENDOR_EFFECT_LOG: return "effect_log";
155 [ND_CMD_VENDOR] = "vendor",152 case ND_CMD_VENDOR: return "vendor";
156 [ND_CMD_CALL] = "cmd_call",153 case ND_CMD_CALL: return "cmd_call";
157 };154 default: return "unknown";
158 155 }
159 if (cmd < ARRAY_SIZE(names) && names[cmd])
160 return names[cmd];
161 return "unknown";
162}156}
163 157
164#define ND_IOCTL 'N'158#define ND_IOCTL 'N'