Hi,
So I collected the ideas/suggestions and prepared another set of patches.
I grouped the masks array with the labels '\0' separated string into a
struct. They are always used together anyway and by grouping them we
save one parameter for every call.
Yeah, Linux BSD coding style is great, but its better to be consequent
so I typedef'ed as requested to fit better in busybox style.
The biggest win is in hdparm, but this time there was a win in both
fdisk and arp too so i submit pathes for those too.
There are probably more places print_flags() could be used but I could
not find them.
Bloatcheck on my hardened uclibc x86 gcc-3.4.6:
function old new delta
hdparm_main 1067 4894 +3827
print_flags_separated - 169 +169
print_flags - 76 +76
.rodata 31784 31833 +49
static.ml - 8 +8
static.ultra_modes2 - 4 +4
static.ultra_modes1 - 4 +4
static.pio_modes - 4 +4
static.labels - 4 +4
static.dma_wmodes - 4 +4
xbsd_print_disklabel 927 892 -35
arp_main 2474 2439 -35
ipaddr_list_or_flush 2732 2455 -277
process_dev 4446 - -4446
------------------------------------------------------------------------------
(add/remove: 8/1 grow/shrink: 2/3 up/down: 4149/-4793) Total: -644
bytes
text data bss dec hex filename
135156 2173 4500 141829 22a05 busybox_old
134540 2361 4500 141401 22859 busybox_unstripped
Thanks!
-nc
Index: networking/arp.c
===================================================================
--- networking/arp.c (revision 22247)
+++ networking/arp.c (working copy)
@@ -314,7 +314,25 @@
char *hwa, char *mask, char *dev)
{
const struct hwtype *xhw;
-
+ static const masks_labels_t labels = {
+ .masks = { ATF_PERM, ATF_PUBL,
+#ifdef HAVE_ATF_MAGIC
+ ATF_MAGIC,
+#endif
+#ifdef HAVE_ATF_DONTPUB
+ ATF_DONTPUB,
+#endif
+ ATF_USETRAILERS,
+ },
+ .labels = "PERM\0PUP\0"
+#ifdef HAVE_ATF_MAGIC
+ "AUTO\0"
+#endif
+#ifdef HAVE_ATF_DONTPUB
+ "DONTPUB\0"
+#endif
+ "TRAIL\0"
+ };
xhw = get_hwntype(type);
if (xhw == NULL)
xhw = get_hwtype(DFLT_HW);
@@ -333,22 +351,8 @@
if (arp_flags & ATF_NETMASK)
printf("netmask %s ", mask);
- if (arp_flags & ATF_PERM)
- printf("PERM ");
- if (arp_flags & ATF_PUBL)
- printf("PUP ");
-#ifdef HAVE_ATF_MAGIC
- if (arp_flags & ATF_MAGIC)
- printf("AUTO ");
-#endif
-#ifdef HAVE_ATF_DONTPUB
- if (arp_flags & ATF_DONTPUB)
- printf("DONTPUB ");
-#endif
- if (arp_flags & ATF_USETRAILERS)
- printf("TRAIL ");
-
- printf("on %s\n", dev);
+ print_flags_separated(&labels, arp_flags, " ");
+ printf(" on %s\n", dev);
}
/* Display the contents of the ARP cache in the kernel. */
Index: util-linux/fdisk_osf.c
===================================================================
--- util-linux/fdisk_osf.c (revision 22247)
+++ util-linux/fdisk_osf.c (working copy)
@@ -500,7 +500,10 @@
struct xbsd_disklabel *lp = &xbsd_dlabel;
struct xbsd_partition *pp;
int i, j;
-
+ static const masks_labels_t ml = {
+ .masks = { BSD_D_REMOVABLE, BSD_D_ECC, BSD_D_BADSECT },
+ .labels = "removable\0ecc\0badsect\0",
+ };
if (show_all) {
#if defined(__alpha__)
printf("# %s:\n", disk_device);
@@ -513,13 +516,8 @@
printf("type: %d\n", lp->d_type);
printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
- printf("flags:");
- if (lp->d_flags & BSD_D_REMOVABLE)
- printf(" removable");
- if (lp->d_flags & BSD_D_ECC)
- printf(" ecc");
- if (lp->d_flags & BSD_D_BADSECT)
- printf(" badsect");
+ printf("flags: ");
+ print_flags_separated(&ml, lp->d_flags, " ");
bb_putchar('\n');
/* On various machines the fields of *lp are short/int/long */
/* In order to avoid problems, we cast them all to long. */
Index: miscutils/hdparm.c
===================================================================
--- miscutils/hdparm.c (revision 22247)
+++ miscutils/hdparm.c (working copy)
@@ -1228,54 +1228,50 @@
if (id->tPIO >= 2) printf("pio2 ");
}
if (id->field_valid & 2) {
- if (id->eide_pio_modes & 1) printf("pio3 ");
- if (id->eide_pio_modes & 2) printf("pio4 ");
- if (id->eide_pio_modes &~3) printf("pio? ");
+ static const masks_labels_t pio_modes = {
+ .masks = { 1, 2, ~3 },
+ .labels = "pio3 \0pio4 \0pio? \0",
+ };
+ print_flags(&pio_modes, id->eide_pio_modes);
}
if (id->capability & 1) {
if (id->dma_1word | id->dma_mword) {
+ static masks_labels_t dma_wmodes = {
+ .masks = { 0x100, 1, 0x200, 2, 0x400, 4,
+ 0xf800, 0xf8, },
+ .labels = "*\0sdma0 \0*\0sdma1 \0*\0sdma2 \0"
+ "*\0sdma? \0",
+ };
+
printf("\n DMA modes: ");
- if (id->dma_1word & 0x100) bb_putchar('*');
- if (id->dma_1word & 1) printf("sdma0 ");
- if (id->dma_1word & 0x200) bb_putchar('*');
- if (id->dma_1word & 2) printf("sdma1 ");
- if (id->dma_1word & 0x400) bb_putchar('*');
- if (id->dma_1word & 4) printf("sdma2 ");
- if (id->dma_1word & 0xf800) bb_putchar('*');
- if (id->dma_1word & 0xf8) printf("sdma? ");
- if (id->dma_mword & 0x100) bb_putchar('*');
- if (id->dma_mword & 1) printf("mdma0 ");
- if (id->dma_mword & 0x200) bb_putchar('*');
- if (id->dma_mword & 2) printf("mdma1 ");
- if (id->dma_mword & 0x400) bb_putchar('*');
- if (id->dma_mword & 4) printf("mdma2 ");
- if (id->dma_mword & 0xf800) bb_putchar('*');
- if (id->dma_mword & 0xf8) printf("mdma? ");
+ print_flags(&dma_wmodes, id->dma_1word);
+ dma_wmodes.labels = "*\0mdma0\0*\0mdma1\0*\0mdma2\0"
+ "*\0mdma?\0";
+ print_flags(&dma_wmodes, id->dma_mword);
}
}
if (((id->capability & 8) || (id->field_valid & 2)) && id->field_valid & 4) {
+ static const masks_labels_t ultra_modes1 = {
+ .masks = { 0x100, 0x001, 0x200, 0x002, 0x400, 0x004 },
+ .labels = "*\0udma0 \0*\0udma1 \0*\0udma2 \0",
+ };
+
printf("\n UDMA modes: ");
- if (id->dma_ultra & 0x100) bb_putchar('*');
- if (id->dma_ultra & 0x001) printf("udma0 ");
- if (id->dma_ultra & 0x200) bb_putchar('*');
- if (id->dma_ultra & 0x002) printf("udma1 ");
- if (id->dma_ultra & 0x400) bb_putchar('*');
- if (id->dma_ultra & 0x004) printf("udma2 ");
+ print_flags(&ultra_modes1, id->dma_ultra);
#ifdef __NEW_HD_DRIVE_ID
if (id->hw_config & 0x2000) {
#else /* !__NEW_HD_DRIVE_ID */
if (id->word93 & 0x2000) {
#endif /* __NEW_HD_DRIVE_ID */
- if (id->dma_ultra & 0x0800) bb_putchar('*');
- if (id->dma_ultra & 0x0008) printf("udma3 ");
- if (id->dma_ultra & 0x1000) bb_putchar('*');
- if (id->dma_ultra & 0x0010) printf("udma4 ");
- if (id->dma_ultra & 0x2000) bb_putchar('*');
- if (id->dma_ultra & 0x0020) printf("udma5 ");
- if (id->dma_ultra & 0x4000) bb_putchar('*');
- if (id->dma_ultra & 0x0040) printf("udma6 ");
- if (id->dma_ultra & 0x8000) bb_putchar('*');
- if (id->dma_ultra & 0x0080) printf("udma7 ");
+ static const masks_labels_t ultra_modes2 = {
+ .masks = { 0x0800, 0x0008, 0x1000, 0x0010,
+ 0x2000, 0x0020, 0x4000, 0x0040,
+ 0x8000, 0x0080 },
+ .labels = "*\0udma3 \0*\0udma4 \0"
+ "*\0udma5 \0*\0udma6 \0"
+ "*\0udma7 \0"
+ };
+ print_flags(&ultra_modes2, id->dma_ultra);
}
}
printf("\n AdvancedPM=%s", (!(id_regs[83] & 8)) ? "no" : "yes");
Index: networking/libiproute/ipaddress.c
===================================================================
--- networking/libiproute/ipaddress.c (revision 22247)
+++ networking/libiproute/ipaddress.c (working copy)
@@ -45,16 +45,14 @@
static void print_link_flags(unsigned flags, unsigned mdown)
{
+ static const masks_labels_t ml = {
+ .masks = { IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT,
+ IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP },
+ .labels = "LOOPBACK\0BROADCAST\0POINTOPOINT\0"
+ "MULTICAST\0NOARP\0UP\0LOWER_UP\0",
+ };
bb_putchar('<');
flags &= ~IFF_RUNNING;
-#define _PF(f) if (flags & IFF_##f) { \
- flags &= ~IFF_##f; \
- printf(#f "%s", flags ? "," : ""); }
- _PF(LOOPBACK);
- _PF(BROADCAST);
- _PF(POINTOPOINT);
- _PF(MULTICAST);
- _PF(NOARP);
#if 0
_PF(ALLMULTI);
_PF(PROMISC);
@@ -66,9 +64,7 @@
_PF(PORTSEL);
_PF(NOTRAILERS);
#endif
- _PF(UP);
- _PF(LOWER_UP);
-#undef _PF
+ flags = print_flags_separated(&ml, flags, ",");
if (flags)
printf("%x", flags);
if (mdown)
Index: include/libbb.h
===================================================================
--- include/libbb.h (revision 22247)
+++ include/libbb.h (working copy)
@@ -1304,7 +1304,15 @@
/* "sh" */
#define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
+typedef struct masks_labels_t {
+ const char *labels;
+ const int masks[];
+} masks_labels_t;
+extern int print_flags_separated(const masks_labels_t *ml, int flags,
+ const char *separator);
+extern int print_flags(const masks_labels_t *ml, int flags);
+
#if ENABLE_FEATURE_DEVFS
# define CURRENT_VC "/dev/vc/0"
# define VC_1 "/dev/vc/1"
Index: libbb/print_flags.c
===================================================================
--- libbb/print_flags.c (revision 0)
+++ libbb/print_flags.c (revision 0)
@@ -0,0 +1,34 @@
+/* vi: set sw=4 ts=4: */
+/* Print string that matches bit masked flags
+ *
+ * Copyright (C) 2008 Natanael Copa <[EMAIL PROTECTED]>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include <libbb.h>
+
+/* returns a set with the flags not printed */
+int print_flags_separated(const masks_labels_t *ml, int flags, const char *separator)
+{
+ const char *need_separator = NULL;
+ const char *label = ml->labels;
+ const int *masks = ml->masks;
+ while (*label) {
+ if (flags & *masks) {
+ printf("%s%s", need_separator ? need_separator: "",
+ label);
+ need_separator = separator;
+ flags &= ~*masks;
+ masks++;
+ }
+ label += strlen(label) + 1;
+ }
+ return flags;
+}
+
+int print_flags(const masks_labels_t *ml, int flags)
+{
+ return print_flags_separated(ml, flags, NULL);
+}
+
Index: libbb/Kbuild
===================================================================
--- libbb/Kbuild (revision 22247)
+++ libbb/Kbuild (working copy)
@@ -68,6 +68,7 @@
lib-y += perror_nomsg_and_die.o
lib-y += pidfile.o
lib-y += printable.o
+lib-y += print_flags.o
lib-y += process_escape_sequence.o
lib-y += procps.o
lib-y += ptr_to_globals.o
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox