mt: eliminate vector of structures with pointers (bad for libbusybox).

It's a win for static build too:

function                                             old     new   delta
opcode_name                                            -     213    +213
opcode_value                                           -      68     +68
mt_main                                              281     256     -25
opcodes                                              280       -    -280
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 0/1 up/down: 281/-305)          Total: -24 bytes
   text    data     bss     dec     hex filename
 767403     974    9420  777797   bde45 busybox_old
 767224     974    9420  777618   bdd92 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-10-11 19:38:59 +00:00
parent 2ea8c40e8f
commit 8add068573

View File

@ -6,58 +6,87 @@
#include "libbb.h" #include "libbb.h"
#include <sys/mtio.h> #include <sys/mtio.h>
struct mt_opcodes { /* missing: eod/seod, stoptions, stwrthreshold, densities */
const char *name; static const short opcode_value[] = {
short value; MTBSF,
MTBSFM,
MTBSR,
MTBSS,
MTCOMPRESSION,
MTEOM,
MTERASE,
MTFSF,
MTFSFM,
MTFSR,
MTFSS,
MTLOAD,
MTLOCK,
MTMKPART,
MTNOP,
MTOFFL,
MTOFFL,
MTRAS1,
MTRAS2,
MTRAS3,
MTRESET,
MTRETEN,
MTREW,
MTSEEK,
MTSETBLK,
MTSETDENSITY,
MTSETDRVBUFFER,
MTSETPART,
MTTELL,
MTWSM,
MTUNLOAD,
MTUNLOCK,
MTWEOF,
MTWEOF
}; };
/* missing: eod/seod, stoptions, stwrthreshold, densities */ static const char opcode_name[] ALIGN1 =
static const struct mt_opcodes opcodes[] = { "bsf" "\0"
{"bsf", MTBSF}, "bsfm" "\0"
{"bsfm", MTBSFM}, "bsr" "\0"
{"bsr", MTBSR}, "bss" "\0"
{"bss", MTBSS}, "datacompression" "\0"
{"datacompression", MTCOMPRESSION}, "eom" "\0"
{"eom", MTEOM}, "erase" "\0"
{"erase", MTERASE}, "fsf" "\0"
{"fsf", MTFSF}, "fsfm" "\0"
{"fsfm", MTFSFM}, "fsr" "\0"
{"fsr", MTFSR}, "fss" "\0"
{"fss", MTFSS}, "load" "\0"
{"load", MTLOAD}, "lock" "\0"
{"lock", MTLOCK}, "mkpart" "\0"
{"mkpart", MTMKPART}, "nop" "\0"
{"nop", MTNOP}, "offline" "\0"
{"offline", MTOFFL}, "rewoffline" "\0"
{"rewoffline", MTOFFL}, "ras1" "\0"
{"ras1", MTRAS1}, "ras2" "\0"
{"ras2", MTRAS2}, "ras3" "\0"
{"ras3", MTRAS3}, "reset" "\0"
{"reset", MTRESET}, "retension" "\0"
{"retension", MTRETEN}, "rewind" "\0"
{"rewind", MTREW}, "seek" "\0"
{"seek", MTSEEK}, "setblk" "\0"
{"setblk", MTSETBLK}, "setdensity" "\0"
{"setdensity", MTSETDENSITY}, "drvbuffer" "\0"
{"drvbuffer", MTSETDRVBUFFER}, "setpart" "\0"
{"setpart", MTSETPART}, "tell" "\0"
{"tell", MTTELL}, "wset" "\0"
{"wset", MTWSM}, "unload" "\0"
{"unload", MTUNLOAD}, "unlock" "\0"
{"unlock", MTUNLOCK}, "eof" "\0"
{"eof", MTWEOF}, "weof" "\0";
{"weof", MTWEOF},
{0, 0}
};
int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int mt_main(int argc, char **argv) int mt_main(int argc, char **argv)
{ {
const char *file = "/dev/tape"; const char *file = "/dev/tape";
const struct mt_opcodes *code = opcodes;
struct mtop op; struct mtop op;
struct mtpos position; struct mtpos position;
int fd, mode; int fd, mode, idx;
if (argc < 2) { if (argc < 2) {
bb_show_usage(); bb_show_usage();
@ -72,24 +101,18 @@ int mt_main(int argc, char **argv)
argc -= 2; argc -= 2;
} }
while (code->name != 0) { idx = index_in_strings(opcode_name, argv[1]);
if (strcmp(code->name, argv[1]) == 0)
break;
code++;
}
if (code->name == 0) { if (idx < 0)
bb_error_msg("unrecognized opcode %s", argv[1]); bb_error_msg_and_die("unrecognized opcode %s", argv[1]);
return EXIT_FAILURE;
}
op.mt_op = code->value; op.mt_op = opcode_value[idx];
if (argc >= 3) if (argc >= 3)
op.mt_count = xatoi_u(argv[2]); op.mt_count = xatoi_u(argv[2]);
else else
op.mt_count = 1; /* One, not zero, right? */ op.mt_count = 1; /* One, not zero, right? */
switch (code->value) { switch (opcode_value[idx]) {
case MTWEOF: case MTWEOF:
case MTERASE: case MTERASE:
case MTWSM: case MTWSM:
@ -104,10 +127,10 @@ int mt_main(int argc, char **argv)
fd = xopen(file, mode); fd = xopen(file, mode);
switch (code->value) { switch (opcode_value[idx]) {
case MTTELL: case MTTELL:
ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file); ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
printf("At block %d.\n", (int) position.mt_blkno); printf("At block %d\n", (int) position.mt_blkno);
break; break;
default: default: