Refactoring. Improve code reuse ratio.

Add ARRAY_SIZE() macro.

Use the new macro in all places where array size is calculated by
means of sizeof(array)/sizeof(array[0]).

Fix minor bugs and warnings in the affected code.

Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
This commit is contained in:
Alexander Amelkin 2018-08-14 16:41:52 +03:00
parent 232773d171
commit bb1a4cc805
No known key found for this signature in database
GPG Key ID: E893587B5B74178D
6 changed files with 16 additions and 16 deletions

View File

@ -38,6 +38,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#ifndef TRUE #ifndef TRUE
#define TRUE 1 #define TRUE 1
#endif #endif

View File

@ -327,8 +327,7 @@ pef_b2s_generic_ER[] __attribute__((unused)) = {
&pef_b2s_gentype_11, &pef_b2s_gentype_11,
&pef_b2s_gentype_12, &pef_b2s_gentype_12,
}; };
#define PEF_B2S_GENERIC_ER_ENTRIES \ #define PEF_B2S_GENERIC_ER_ENTRIES ARRAY_SIZE(pef_b2s_generic_ER)
(sizeof(pef_b2s_generic_ER) / sizeof(pef_b2s_generic_ER[0]))
#ifdef HAVE_PRAGMA_PACK #ifdef HAVE_PRAGMA_PACK
#pragma pack(1) #pragma pack(1)

View File

@ -898,6 +898,7 @@ fru_area_print_chassis(struct ipmi_intf * intf, struct fru_info * fru,
uint8_t * fru_data; uint8_t * fru_data;
uint32_t fru_len, i; uint32_t fru_len, i;
uint8_t tmp[2]; uint8_t tmp[2];
size_t chassis_type;
fru_len = 0; fru_len = 0;
@ -931,10 +932,10 @@ fru_area_print_chassis(struct ipmi_intf * intf, struct fru_info * fru,
*/ */
i = 2; i = 2;
printf(" Chassis Type : %s\n", chassis_type = (fru_data[i] > ARRAY_SIZE(chassis_type_desc) - 1)
chassis_type_desc[fru_data[i] > ? 2
(sizeof(chassis_type_desc)/sizeof(chassis_type_desc[0])) - 1 ? : fru_data[i];
2 : fru_data[i]]); printf(" Chassis Type : %s\n", chassis_type_desc[chassis_type]);
i++; i++;

View File

@ -1027,9 +1027,7 @@ static void lanp_print_usage(int cmd)
printf("\n available parameters:\n"); printf("\n available parameters:\n");
/* 'save' shall use 'write' filter, since it outputs a block /* 'save' shall use 'write' filter, since it outputs a block
* of 'set's */ * of 'set's */
ipmi_cfgp_usage(lan_cfgp, ipmi_cfgp_usage(lan_cfgp, ARRAY_SIZE(lan_cfgp), cmd != LANP_CMD_PRINT);
sizeof(lan_cfgp)/sizeof(lan_cfgp[0]),
cmd != LANP_CMD_PRINT);
} }
} }
@ -1161,8 +1159,8 @@ ipmi_lan6_main(struct ipmi_intf *intf, int argc, char **argv)
*/ */
ipmi_cfgp_init(&ctx, lan_cfgp, ipmi_cfgp_init(&ctx, lan_cfgp,
sizeof(lan_cfgp)/sizeof(lan_cfgp[0]), "lan6 set nolock", ARRAY_SIZE(lan_cfgp), "lan6 set nolock",
lanp_ip6_cfgp, &lp); lanp_ip6_cfgp, &lp);
ret = ipmi_cfgp_parse_sel(&ctx, argc, (const char **)argv, &sel); ret = ipmi_cfgp_parse_sel(&ctx, argc, (const char **)argv, &sel);
if (ret == -1) { if (ret == -1) {

View File

@ -241,12 +241,12 @@ serial_bm_open(struct ipmi_intf * intf)
return -1; return -1;
} }
for (i = 0; i < sizeof(rates) / sizeof(rates[0]); i++) { for (i = 0; i < ARRAY_SIZE(rates); i++) {
if (rates[i].baudrate == rate) { if (rates[i].baudrate == rate) {
break; break;
} }
} }
if (i >= sizeof(rates) / sizeof(rates[0])) { if (i >= ARRAY_SIZE(rates)) {
lprintf(LOG_ERR, "Unsupported baud rate %i specified", rate); lprintf(LOG_ERR, "Unsupported baud rate %i specified", rate);
return -1; return -1;
} }

View File

@ -149,7 +149,7 @@ ipmi_serial_term_open(struct ipmi_intf * intf)
struct termios ti; struct termios ti;
unsigned int rate = 9600; unsigned int rate = 9600;
char *p; char *p;
int i; size_t i;
if (!intf->devfile) { if (!intf->devfile) {
lprintf(LOG_ERR, "Serial device is not specified"); lprintf(LOG_ERR, "Serial device is not specified");
@ -188,12 +188,12 @@ ipmi_serial_term_open(struct ipmi_intf * intf)
return -1; return -1;
} }
for (i = 0; i < sizeof(rates) / sizeof(rates[0]); i++) { for (i = 0; i < ARRAY_SIZE(rates); i++) {
if (rates[i].baudrate == rate) { if (rates[i].baudrate == rate) {
break; break;
} }
} }
if (i >= sizeof(rates) / sizeof(rates[0])) { if (i >= ARRAY_SIZE(rates)) {
lprintf(LOG_ERR, "Unsupported baud rate %i specified", rate); lprintf(LOG_ERR, "Unsupported baud rate %i specified", rate);
return -1; return -1;
} }