libiproute: code and data shrink
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 13 Oct 2009 14:27:11 +0000 (16:27 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 13 Oct 2009 14:27:11 +0000 (16:27 +0200)
function                                             old     new   delta
rtnl_a2n                                               -     126    +126
ll_remember_index                                    233     263     +30
find_by_index                                         26      36     +10
rtnl_rtprot_initialize                                66      70      +4
static.unit_chars                                      7       9      +2
rtnl_rttable_initialize                               73      75      +2
rtnl_rtscope_initialize                               83      85      +2
rtnl_rtrealm_initialize                               43      45      +2
rtnl_rtdsfield_initialize                             43      45      +2
rtnl_rttable_n2a                                      62      63      +1
rtnl_rtscope_n2a                                      62      63      +1
rtnl_rtrealm_n2a                                      62      63      +1
rtnl_dsfield_n2a                                      70      71      +1
ll_init_map                                           36      33      -3
make_human_readable_str                              262     258      -4
static.fmt                                            97      92      -5
static.fmt_tenths                                     10       -     -10
static.str                                            21       4     -17
static.res                                            20       -     -20
static.cache                                          24       4     -20
idxmap                                                64       4     -60
rtnl_rttable_a2n                                     154      39    -115
rtnl_rtscope_a2n                                     159      39    -120
rtnl_rtrealm_a2n                                     159      39    -120
rtnl_rtprot_a2n                                      159      39    -120
rtnl_dsfield_a2n                                     162      39    -123
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 12/11 up/down: 184/-737)       Total: -553 bytes
   text           data            bss            dec            hex        filename
 820376            445           7668         828489          ca449        busybox_old
 819950            445           7548         827943          ca227        busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
networking/libiproute/ll_map.c
networking/libiproute/rt_names.c

index 62528cc835d2002c463fc2e269dd8a6054ecc848..3c4ef2c321d47f9064e60b3dff0cd6a5a38ffb06 100644 (file)
@@ -27,15 +27,16 @@ struct idxmap {
        char           name[16];
 };
 
-static struct idxmap *idxmap[16];
+static struct idxmap **idxmap; /* treat as *idxmap[16] */
 
 static struct idxmap *find_by_index(int idx)
 {
        struct idxmap *im;
 
-       for (im = idxmap[idx & 0xF]; im; im = im->next)
-               if (im->index == idx)
-                       return im;
+       if (idxmap)
+               for (im = idxmap[idx & 0xF]; im; im = im->next)
+                       if (im->index == idx)
+                               return im;
        return NULL;
 }
 
@@ -59,8 +60,10 @@ int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
        if (tb[IFLA_IFNAME] == NULL)
                return 0;
 
-       h = ifi->ifi_index & 0xF;
+       if (!idxmap)
+               idxmap = xzalloc(sizeof(idxmap[0]) * 16);
 
+       h = ifi->ifi_index & 0xF;
        for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
                if (im->index == ifi->ifi_index)
                        goto found;
@@ -152,13 +155,15 @@ int FAST_FUNC xll_name_to_index(const char *name)
                ret = icache;
                goto out;
        }
-       for (i = 0; i < 16; i++) {
-               for (im = idxmap[i]; im; im = im->next) {
-                       if (strcmp(im->name, name) == 0) {
-                               icache = im->index;
-                               strcpy(ncache, name);
-                               ret = im->index;
-                               goto out;
+       if (idxmap) {
+               for (i = 0; i < 16; i++) {
+                       for (im = idxmap[i]; im; im = im->next) {
+                               if (strcmp(im->name, name) == 0) {
+                                       icache = im->index;
+                                       strcpy(ncache, name);
+                                       ret = im->index;
+                                       goto out;
+                               }
                        }
                }
        }
@@ -195,6 +200,6 @@ int FAST_FUNC xll_name_to_index(const char *name)
 int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
 {
        xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
-       xrtnl_dump_filter(rth, ll_remember_index, &idxmap);
+       xrtnl_dump_filter(rth, ll_remember_index, NULL);
        return 0;
 }
index e4d10613bd504d2b91e4913864437f5c8a8f6fb6..2699dba3d7022eaf8d459ab21a0dbb264fd2e071 100644 (file)
@@ -9,20 +9,23 @@
  *
  * Authors:    Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  */
-
 #include "libbb.h"
 #include "rt_names.h"
 
-/* so far all callers have size == 256 */
-#define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab)
-#define size 256
-static void rtnl_tab_initialize(const char *file, const char **tab, int size)
+typedef struct rtnl_tab_t {
+       const char *cached_str;
+       unsigned cached_result;
+       const char *tab[256];
+} rtnl_tab_t;
+
+static void rtnl_tab_initialize(const char *file, const char **tab)
 {
        char *token[2];
        parser_t *parser = config_open2(file, fopen_for_read);
+
        while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
-               int id = bb_strtou(token[0], NULL, 0);
-               if (id < 0 || id > size) {
+               unsigned id = bb_strtou(token[0], NULL, 0);
+               if (id > 256) {
                        bb_error_msg("database %s is corrupted at line %d",
                                file, parser->lineno);
                        break;
@@ -31,9 +34,36 @@ static void rtnl_tab_initialize(const char *file, const char **tab, int size)
        }
        config_close(parser);
 }
-#undef size
 
-static const char **rtnl_rtprot_tab; /* [256] */
+static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
+{
+       unsigned i;
+
+       if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
+               *id = tab->cached_result;
+               return 0;
+       }
+
+       for (i = 0; i < 256; i++) {
+               if (tab->tab[i]
+                && strcmp(tab->tab[i], arg) == 0
+               ) {
+                       tab->cached_str = tab->tab[i];
+                       tab->cached_result = i;
+                       *id = i;
+                       return 0;
+               }
+       }
+
+       i = bb_strtou(arg, NULL, base);
+       if (i > 255)
+               return -1;
+       *id = i;
+       return 0;
+}
+
+
+static rtnl_tab_t *rtnl_rtprot_tab;
 
 static void rtnl_rtprot_initialize(void)
 {
@@ -52,13 +82,13 @@ static void rtnl_rtprot_initialize(void)
                "zebra",
                "bird",
        };
-       if (rtnl_rtprot_tab) return;
-       rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
-       memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
-       rtnl_tab_initialize("/etc/iproute2/rt_protos",
-                           rtnl_rtprot_tab, 256);
-}
 
+       if (rtnl_rtprot_tab)
+               return;
+       rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
+       memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
+       rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab);
+}
 
 const char* rtnl_rtprot_n2a(int id, char *buf, int len)
 {
@@ -69,59 +99,34 @@ const char* rtnl_rtprot_n2a(int id, char *buf, int len)
 
        rtnl_rtprot_initialize();
 
-       if (rtnl_rtprot_tab[id])
-               return rtnl_rtprot_tab[id];
+       if (rtnl_rtprot_tab->tab[id])
+               return rtnl_rtprot_tab->tab[id];
        snprintf(buf, len, "%d", id);
        return buf;
 }
 
 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
 {
-       static const char *cache = NULL;
-       static unsigned long res;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
-       }
-
        rtnl_rtprot_initialize();
-
-       for (i = 0; i < 256; i++) {
-               if (rtnl_rtprot_tab[i] &&
-                   strcmp(rtnl_rtprot_tab[i], arg) == 0) {
-                       cache = rtnl_rtprot_tab[i];
-                       res = i;
-                       *id = res;
-                       return 0;
-               }
-       }
-
-       res = bb_strtoul(arg, NULL, 0);
-       if (errno || res > 255)
-               return -1;
-       *id = res;
-       return 0;
+       return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
 }
 
 
-static const char **rtnl_rtscope_tab; /* [256] */
+static rtnl_tab_t *rtnl_rtscope_tab;
 
 static void rtnl_rtscope_initialize(void)
 {
-       if (rtnl_rtscope_tab) return;
-       rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
-       rtnl_rtscope_tab[0] = "global";
-       rtnl_rtscope_tab[255] = "nowhere";
-       rtnl_rtscope_tab[254] = "host";
-       rtnl_rtscope_tab[253] = "link";
-       rtnl_rtscope_tab[200] = "site";
-       rtnl_tab_initialize("/etc/iproute2/rt_scopes",
-                           rtnl_rtscope_tab, 256);
+       if (rtnl_rtscope_tab)
+               return;
+       rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
+       rtnl_rtscope_tab->tab[0] = "global";
+       rtnl_rtscope_tab->tab[255] = "nowhere";
+       rtnl_rtscope_tab->tab[254] = "host";
+       rtnl_rtscope_tab->tab[253] = "link";
+       rtnl_rtscope_tab->tab[200] = "site";
+       rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
 }
 
-
 const char* rtnl_rtscope_n2a(int id, char *buf, int len)
 {
        if (id < 0 || id >= 256) {
@@ -131,83 +136,33 @@ const char* rtnl_rtscope_n2a(int id, char *buf, int len)
 
        rtnl_rtscope_initialize();
 
-       if (rtnl_rtscope_tab[id])
-               return rtnl_rtscope_tab[id];
+       if (rtnl_rtscope_tab->tab[id])
+               return rtnl_rtscope_tab->tab[id];
        snprintf(buf, len, "%d", id);
        return buf;
 }
 
 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
 {
-       static const char *cache = NULL;
-       static unsigned long res;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
-       }
-
        rtnl_rtscope_initialize();
-
-       for (i = 0; i < 256; i++) {
-               if (rtnl_rtscope_tab[i] &&
-                   strcmp(rtnl_rtscope_tab[i], arg) == 0) {
-                       cache = rtnl_rtscope_tab[i];
-                       res = i;
-                       *id = res;
-                       return 0;
-               }
-       }
-
-       res = bb_strtoul(arg, NULL, 0);
-       if (errno || res > 255)
-               return -1;
-       *id = res;
-       return 0;
+       return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
 }
 
 
-static const char **rtnl_rtrealm_tab; /* [256] */
+static rtnl_tab_t *rtnl_rtrealm_tab;
 
 static void rtnl_rtrealm_initialize(void)
 {
        if (rtnl_rtrealm_tab) return;
-       rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
-       rtnl_rtrealm_tab[0] = "unknown";
-       rtnl_tab_initialize("/etc/iproute2/rt_realms",
-                           rtnl_rtrealm_tab, 256);
+       rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
+       rtnl_rtrealm_tab->tab[0] = "unknown";
+       rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
 }
 
-
 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
 {
-       static const char *cache = NULL;
-       static unsigned long res;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
-       }
-
        rtnl_rtrealm_initialize();
-
-       for (i = 0; i < 256; i++) {
-               if (rtnl_rtrealm_tab[i] &&
-                   strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
-                       cache = rtnl_rtrealm_tab[i];
-                       res = i;
-                       *id = res;
-                       return 0;
-               }
-       }
-
-       res = bb_strtoul(arg, NULL, 0);
-       if (errno || res > 255)
-               return -1;
-       *id = res;
-       return 0;
+       return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
 }
 
 #if ENABLE_FEATURE_IP_RULE
@@ -220,26 +175,24 @@ const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
 
        rtnl_rtrealm_initialize();
 
-       if (rtnl_rtrealm_tab[id])
-               return rtnl_rtrealm_tab[id];
+       if (rtnl_rtrealm_tab->tab[id])
+               return rtnl_rtrealm_tab->tab[id];
        snprintf(buf, len, "%d", id);
        return buf;
 }
 #endif
 
 
-static const char **rtnl_rtdsfield_tab; /* [256] */
+static rtnl_tab_t *rtnl_rtdsfield_tab;
 
 static void rtnl_rtdsfield_initialize(void)
 {
        if (rtnl_rtdsfield_tab) return;
-       rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
-       rtnl_rtdsfield_tab[0] = "0";
-       rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
-                           rtnl_rtdsfield_tab, 256);
+       rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
+       rtnl_rtdsfield_tab->tab[0] = "0";
+       rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
 }
 
-
 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
 {
        if (id < 0 || id >= 256) {
@@ -249,59 +202,33 @@ const char * rtnl_dsfield_n2a(int id, char *buf, int len)
 
        rtnl_rtdsfield_initialize();
 
-       if (rtnl_rtdsfield_tab[id])
-               return rtnl_rtdsfield_tab[id];
+       if (rtnl_rtdsfield_tab->tab[id])
+               return rtnl_rtdsfield_tab->tab[id];
        snprintf(buf, len, "0x%02x", id);
        return buf;
 }
 
-
 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
 {
-       static const char *cache = NULL;
-       static unsigned long res;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
-       }
-
        rtnl_rtdsfield_initialize();
-
-       for (i = 0; i < 256; i++) {
-               if (rtnl_rtdsfield_tab[i] &&
-                   strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
-                       cache = rtnl_rtdsfield_tab[i];
-                       res = i;
-                       *id = res;
-                       return 0;
-               }
-       }
-
-       res = bb_strtoul(arg, NULL, 16);
-       if (errno || res > 255)
-               return -1;
-       *id = res;
-       return 0;
+       return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
 }
 
 
 #if ENABLE_FEATURE_IP_RULE
-static const char **rtnl_rttable_tab; /* [256] */
+static rtnl_tab_t *rtnl_rttable_tab;
 
 static void rtnl_rttable_initialize(void)
 {
        if (rtnl_rtdsfield_tab) return;
-       rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
-       rtnl_rttable_tab[0] = "unspec";
-       rtnl_rttable_tab[255] = "local";
-       rtnl_rttable_tab[254] = "main";
-       rtnl_rttable_tab[253] = "default";
-       rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
+       rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
+       rtnl_rttable_tab->tab[0] = "unspec";
+       rtnl_rttable_tab->tab[255] = "local";
+       rtnl_rttable_tab->tab[254] = "main";
+       rtnl_rttable_tab->tab[253] = "default";
+       rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
 }
 
-
 const char *rtnl_rttable_n2a(int id, char *buf, int len)
 {
        if (id < 0 || id >= 256) {
@@ -311,39 +238,16 @@ const char *rtnl_rttable_n2a(int id, char *buf, int len)
 
        rtnl_rttable_initialize();
 
-       if (rtnl_rttable_tab[id])
-               return rtnl_rttable_tab[id];
+       if (rtnl_rttable_tab->tab[id])
+               return rtnl_rttable_tab->tab[id];
        snprintf(buf, len, "%d", id);
        return buf;
 }
 
-int rtnl_rttable_a2n(uint32_t * id, char *arg)
+int rtnl_rttable_a2n(uint32_t *id, char *arg)
 {
-       static char *cache = NULL;
-       static unsigned long res;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
-       }
-
        rtnl_rttable_initialize();
-
-       for (i = 0; i < 256; i++) {
-               if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
-                       cache = (char*)rtnl_rttable_tab[i];
-                       res = i;
-                       *id = res;
-                       return 0;
-               }
-       }
-
-       i = bb_strtoul(arg, NULL, 0);
-       if (errno || i > 255)
-               return -1;
-       *id = i;
-       return 0;
+       return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
 }
 
 #endif