aboutsummaryrefslogtreecommitdiff
path: root/miscutils/i2c_tools.c
diff options
context:
space:
mode:
authorJames Byrne <james.byrne@origamienergy.com>2019-07-02 11:35:03 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-07-02 11:35:03 +0200
commit6937487be73cd4563b876413277a295a5fe2f32c (patch)
treef16cc9999a7c827891e6ec8d99c699fc791008ee /miscutils/i2c_tools.c
parentcaecfdc20d450686cd1f7e9b5f650322f894b3c2 (diff)
downloadbusybox-w32-6937487be73cd4563b876413277a295a5fe2f32c.tar.gz
busybox-w32-6937487be73cd4563b876413277a295a5fe2f32c.tar.bz2
busybox-w32-6937487be73cd4563b876413277a295a5fe2f32c.zip
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils/i2c_tools.c')
-rw-r--r--miscutils/i2c_tools.c44
1 files changed, 22 insertions, 22 deletions
diff --git a/miscutils/i2c_tools.c b/miscutils/i2c_tools.c
index 7a2e8534a..82a559f74 100644
--- a/miscutils/i2c_tools.c
+++ b/miscutils/i2c_tools.c
@@ -344,7 +344,7 @@ static void get_funcs_matrix(int fd, unsigned long *funcs)
344static void check_funcs_test_end(int funcs, int pec, const char *err) 344static void check_funcs_test_end(int funcs, int pec, const char *err)
345{ 345{
346 if (pec && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) 346 if (pec && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C)))
347 bb_error_msg("warning: adapter does not support PEC"); 347 bb_simple_error_msg("warning: adapter does not support PEC");
348 348
349 if (err) 349 if (err)
350 bb_error_msg_and_die( 350 bb_error_msg_and_die(
@@ -392,7 +392,7 @@ static void check_read_funcs(int fd, int mode, int data_addr, int pec)
392 break; 392 break;
393#endif /* ENABLE_I2CDUMP */ 393#endif /* ENABLE_I2CDUMP */
394 default: 394 default:
395 bb_error_msg_and_die("internal error"); 395 bb_simple_error_msg_and_die("internal error");
396 } 396 }
397 check_funcs_test_end(funcs, pec, err); 397 check_funcs_test_end(funcs, pec, err);
398} 398}
@@ -438,7 +438,7 @@ static void confirm_or_abort(void)
438{ 438{
439 fprintf(stderr, "Continue? [y/N] "); 439 fprintf(stderr, "Continue? [y/N] ");
440 if (!bb_ask_y_confirmation()) 440 if (!bb_ask_y_confirmation())
441 bb_error_msg_and_die("aborting"); 441 bb_simple_error_msg_and_die("aborting");
442} 442}
443 443
444/* 444/*
@@ -449,20 +449,20 @@ static void confirm_or_abort(void)
449 */ 449 */
450static void confirm_action(int bus_addr, int mode, int data_addr, int pec) 450static void confirm_action(int bus_addr, int mode, int data_addr, int pec)
451{ 451{
452 bb_error_msg("WARNING! This program can confuse your I2C bus"); 452 bb_simple_error_msg("WARNING! This program can confuse your I2C bus");
453 453
454 /* Don't let the user break his/her EEPROMs */ 454 /* Don't let the user break his/her EEPROMs */
455 if (bus_addr >= 0x50 && bus_addr <= 0x57 && pec) { 455 if (bus_addr >= 0x50 && bus_addr <= 0x57 && pec) {
456 bb_error_msg_and_die("this is I2C not smbus - using PEC on I2C " 456 bb_simple_error_msg_and_die("this is I2C not smbus - using PEC on I2C "
457 "devices may result in data loss, aborting"); 457 "devices may result in data loss, aborting");
458 } 458 }
459 459
460 if (mode == I2C_SMBUS_BYTE && data_addr >= 0 && pec) 460 if (mode == I2C_SMBUS_BYTE && data_addr >= 0 && pec)
461 bb_error_msg("WARNING! May interpret a write byte command " 461 bb_simple_error_msg("WARNING! May interpret a write byte command "
462 "with PEC as a write byte data command"); 462 "with PEC as a write byte data command");
463 463
464 if (pec) 464 if (pec)
465 bb_error_msg("PEC checking enabled"); 465 bb_simple_error_msg("PEC checking enabled");
466 466
467 confirm_or_abort(); 467 confirm_or_abort();
468} 468}
@@ -507,7 +507,7 @@ int i2cget_main(int argc UNUSED_PARAM, char **argv)
507 case 'w': mode = I2C_SMBUS_WORD_DATA; break; 507 case 'w': mode = I2C_SMBUS_WORD_DATA; break;
508 case 'c': mode = I2C_SMBUS_BYTE; break; 508 case 'c': mode = I2C_SMBUS_BYTE; break;
509 default: 509 default:
510 bb_error_msg("invalid mode"); 510 bb_simple_error_msg("invalid mode");
511 bb_show_usage(); 511 bb_show_usage();
512 } 512 }
513 pec = argv[3][1] == 'p'; 513 pec = argv[3][1] == 'p';
@@ -529,7 +529,7 @@ int i2cget_main(int argc UNUSED_PARAM, char **argv)
529 if (data_addr >= 0) { 529 if (data_addr >= 0) {
530 status = i2c_smbus_write_byte(fd, data_addr); 530 status = i2c_smbus_write_byte(fd, data_addr);
531 if (status < 0) 531 if (status < 0)
532 bb_error_msg("warning - write failed"); 532 bb_simple_error_msg("warning - write failed");
533 } 533 }
534 status = i2c_smbus_read_byte(fd); 534 status = i2c_smbus_read_byte(fd);
535 break; 535 break;
@@ -542,7 +542,7 @@ int i2cget_main(int argc UNUSED_PARAM, char **argv)
542 close(fd); 542 close(fd);
543 543
544 if (status < 0) 544 if (status < 0)
545 bb_perror_msg_and_die("read failed"); 545 bb_simple_perror_msg_and_die("read failed");
546 546
547 printf("0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status); 547 printf("0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
548 548
@@ -611,7 +611,7 @@ int i2cset_main(int argc, char **argv)
611 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; 611 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA;
612 break; 612 break;
613 default: 613 default:
614 bb_error_msg("invalid mode"); 614 bb_simple_error_msg("invalid mode");
615 bb_show_usage(); 615 bb_show_usage();
616 } 616 }
617 617
@@ -620,11 +620,11 @@ int i2cset_main(int argc, char **argv)
620 || mode == I2C_SMBUS_I2C_BLOCK_DATA 620 || mode == I2C_SMBUS_I2C_BLOCK_DATA
621 ) { 621 ) {
622 if (pec && mode == I2C_SMBUS_I2C_BLOCK_DATA) 622 if (pec && mode == I2C_SMBUS_I2C_BLOCK_DATA)
623 bb_error_msg_and_die( 623 bb_simple_error_msg_and_die(
624 "PEC not supported for I2C " 624 "PEC not supported for I2C "
625 "block writes"); 625 "block writes");
626 if (opts & opt_m) 626 if (opts & opt_m)
627 bb_error_msg_and_die( 627 bb_simple_error_msg_and_die(
628 "mask not supported for block " 628 "mask not supported for block "
629 "writes"); 629 "writes");
630 } 630 }
@@ -685,7 +685,7 @@ int i2cset_main(int argc, char **argv)
685 } 685 }
686 686
687 if (tmpval < 0) 687 if (tmpval < 0)
688 bb_perror_msg_and_die("can't read old value"); 688 bb_simple_perror_msg_and_die("can't read old value");
689 689
690 val = (val & mask) | (tmpval & ~mask); 690 val = (val & mask) | (tmpval & ~mask);
691 691
@@ -724,7 +724,7 @@ int i2cset_main(int argc, char **argv)
724 break; 724 break;
725 } 725 }
726 if (status < 0) 726 if (status < 0)
727 bb_perror_msg_and_die("write failed"); 727 bb_simple_perror_msg_and_die("write failed");
728 728
729 if (pec) 729 if (pec)
730 i2c_set_pec(fd, 0); /* Clear PEC. */ 730 i2c_set_pec(fd, 0); /* Clear PEC. */
@@ -978,12 +978,12 @@ int i2cdump_main(int argc UNUSED_PARAM, char **argv)
978 case 's': mode = I2C_SMBUS_BLOCK_DATA; break; 978 case 's': mode = I2C_SMBUS_BLOCK_DATA; break;
979 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break; 979 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break;
980 default: 980 default:
981 bb_error_msg_and_die("invalid mode"); 981 bb_simple_error_msg_and_die("invalid mode");
982 } 982 }
983 983
984 if (argv[2][1] == 'p') { 984 if (argv[2][1] == 'p') {
985 if (argv[2][0] == 'W' || argv[2][0] == 'i') { 985 if (argv[2][0] == 'W' || argv[2][0] == 'i') {
986 bb_error_msg_and_die( 986 bb_simple_error_msg_and_die(
987 "pec not supported for -W and -i"); 987 "pec not supported for -W and -i");
988 } else { 988 } else {
989 pec = 1; 989 pec = 1;
@@ -994,7 +994,7 @@ int i2cdump_main(int argc UNUSED_PARAM, char **argv)
994 if (opts & opt_r) { 994 if (opts & opt_r) {
995 first = strtol(opt_r_str, &dash, 0); 995 first = strtol(opt_r_str, &dash, 0);
996 if (dash == opt_r_str || *dash != '-' || first > 0xff) 996 if (dash == opt_r_str || *dash != '-' || first > 0xff)
997 bb_error_msg_and_die("invalid range"); 997 bb_simple_error_msg_and_die("invalid range");
998 last = xstrtou_range(++dash, 0, first, 0xff); 998 last = xstrtou_range(++dash, 0, first, 0xff);
999 999
1000 /* Range is not available for every mode. */ 1000 /* Range is not available for every mode. */
@@ -1007,7 +1007,7 @@ int i2cdump_main(int argc UNUSED_PARAM, char **argv)
1007 break; 1007 break;
1008 /* Fall through */ 1008 /* Fall through */
1009 default: 1009 default:
1010 bb_error_msg_and_die( 1010 bb_simple_error_msg_and_die(
1011 "range not compatible with selected mode"); 1011 "range not compatible with selected mode");
1012 } 1012 }
1013 } 1013 }
@@ -1032,7 +1032,7 @@ int i2cdump_main(int argc UNUSED_PARAM, char **argv)
1032 if (mode == I2C_SMBUS_BYTE) { 1032 if (mode == I2C_SMBUS_BYTE) {
1033 res = i2c_smbus_write_byte(fd, first); 1033 res = i2c_smbus_write_byte(fd, first);
1034 if (res < 0) 1034 if (res < 0)
1035 bb_perror_msg_and_die("write start address"); 1035 bb_simple_perror_msg_and_die("write start address");
1036 } 1036 }
1037 1037
1038 dump_data(fd, mode, first, last, block, blen); 1038 dump_data(fd, mode, first, last, block, blen);
@@ -1398,7 +1398,7 @@ static void check_i2c_func(int fd)
1398 get_funcs_matrix(fd, &funcs); 1398 get_funcs_matrix(fd, &funcs);
1399 1399
1400 if (!(funcs & I2C_FUNC_I2C)) 1400 if (!(funcs & I2C_FUNC_I2C))
1401 bb_error_msg_and_die("adapter does not support I2C transfers"); 1401 bb_simple_error_msg_and_die("adapter does not support I2C transfers");
1402} 1402}
1403 1403
1404//usage:#define i2ctransfer_trivial_usage 1404//usage:#define i2ctransfer_trivial_usage
@@ -1451,7 +1451,7 @@ int i2ctransfer_main(int argc UNUSED_PARAM, char **argv)
1451 char *end; 1451 char *end;
1452 1452
1453 if (nmsgs >= I2C_RDWR_IOCTL_MAX_MSGS) 1453 if (nmsgs >= I2C_RDWR_IOCTL_MAX_MSGS)
1454 bb_error_msg_and_die("too many messages, max: "I2C_RDWR_IOCTL_MAX_MSGS_STR); 1454 bb_simple_error_msg_and_die("too many messages, max: "I2C_RDWR_IOCTL_MAX_MSGS_STR);
1455 1455
1456 flags = 0; 1456 flags = 0;
1457 arg_ptr = *argv; 1457 arg_ptr = *argv;