From 38de05f23e8556012be028c25f7a7a1571e34e1a Mon Sep 17 00:00:00 2001 From: Steven Luo Date: Sat, 29 May 2010 22:11:11 -0700 Subject: [PATCH] Avoid structure copies when looping through swb_config_options --- config-ui/browser-switchboard-config.c | 35 ++++++++++++++++---------------- config-ui/save-config.c | 25 ++++++++++++----------- config.c | 17 ++++++++-------- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/config-ui/browser-switchboard-config.c b/config-ui/browser-switchboard-config.c index c14b80f..befc113 100644 --- a/config-ui/browser-switchboard-config.c +++ b/config-ui/browser-switchboard-config.c @@ -22,6 +22,7 @@ #include +#include #include #include #include @@ -32,8 +33,8 @@ extern struct swb_config_option swb_config_options[]; static int get_config_value(char *name) { struct swb_config cfg; - int i; - struct swb_config_option optinfo; + struct swb_config_option *optinfo; + ptrdiff_t i; int retval = 1; swb_config_init(&cfg); @@ -41,12 +42,12 @@ static int get_config_value(char *name) { if (!swb_config_load(&cfg)) return 1; - for (i = 0; swb_config_options[i].name; ++i) { - optinfo = swb_config_options[i]; - if (strcmp(name, optinfo.name)) + for (optinfo = swb_config_options; optinfo->name; ++optinfo) { + if (strcmp(name, optinfo->name)) continue; - switch (optinfo.type) { + i = optinfo - swb_config_options; + switch (optinfo->type) { case SWB_CONFIG_OPT_STRING: printf("%s\n", *(char **)cfg.entries[i]); break; @@ -67,8 +68,8 @@ static int get_config_value(char *name) { static int set_config_value(char *name, char *value) { struct swb_config cfg; - int i; - struct swb_config_option optinfo; + struct swb_config_option *optinfo; + ptrdiff_t i; int retval = 1; swb_config_init(&cfg); @@ -76,39 +77,39 @@ static int set_config_value(char *name, char *value) { if (!swb_config_load(&cfg)) return 1; - for (i = 0; swb_config_options[i].name; ++i) { - optinfo = swb_config_options[i]; - if (strcmp(name, optinfo.name)) + for (optinfo = swb_config_options; optinfo->name; ++optinfo) { + if (strcmp(name, optinfo->name)) continue; - switch (optinfo.type) { + i = optinfo - swb_config_options; + switch (optinfo->type) { case SWB_CONFIG_OPT_STRING: /* Free any existing string */ - if (cfg.flags & optinfo.set_mask) + if (cfg.flags & optinfo->set_mask) free(*(char **)cfg.entries[i]); if (strlen(value) == 0) { /* If the new value is empty, clear the config setting */ *(char **)cfg.entries[i] = NULL; - cfg.flags &= ~optinfo.set_mask; + cfg.flags &= ~optinfo->set_mask; } else { /* Make a copy of the string -- it's not safe to free value, which comes from argv */ if (!(*(char **)cfg.entries[i] = strdup(value))) exit(1); - cfg.flags |= optinfo.set_mask; + cfg.flags |= optinfo->set_mask; } break; case SWB_CONFIG_OPT_INT: if (strlen(value) == 0) { /* If the new value is empty, clear the config setting */ - cfg.flags &= ~optinfo.set_mask; + cfg.flags &= ~optinfo->set_mask; } else { *(int *)cfg.entries[i] = atoi(value); - cfg.flags |= optinfo.set_mask; + cfg.flags |= optinfo->set_mask; } break; } diff --git a/config-ui/save-config.c b/config-ui/save-config.c index 4a066f5..4415b99 100644 --- a/config-ui/save-config.c +++ b/config-ui/save-config.c @@ -21,6 +21,7 @@ */ #include +#include #include #include #include @@ -35,28 +36,28 @@ extern struct swb_config_option swb_config_options[]; /* Outputs a config file line for the named option to a file descriptor */ static void swb_config_output_option(FILE *fp, unsigned int *oldcfg_seen, struct swb_config *cfg, char *name) { - int i; - struct swb_config_option opt; + struct swb_config_option *opt; + ptrdiff_t i; - for (i = 0; swb_config_options[i].name; ++i) { - opt = swb_config_options[i]; - if (strcmp(opt.name, name)) + for (opt = swb_config_options; opt->name; ++opt) { + if (strcmp(opt->name, name)) continue; - if (!(*oldcfg_seen & opt.set_mask) && - (cfg->flags & opt.set_mask)) { - switch (opt.type) { + i = opt - swb_config_options; + if (!(*oldcfg_seen & opt->set_mask) && + (cfg->flags & opt->set_mask)) { + switch (opt->type) { case SWB_CONFIG_OPT_STRING: fprintf(fp, "%s = \"%s\"\n", - opt.name, + opt->name, *(char **)cfg->entries[i]); - *oldcfg_seen |= opt.set_mask; + *oldcfg_seen |= opt->set_mask; break; case SWB_CONFIG_OPT_INT: fprintf(fp, "%s = %d\n", - opt.name, + opt->name, *(int *)cfg->entries[i]); - *oldcfg_seen |= opt.set_mask; + *oldcfg_seen |= opt->set_mask; break; } } diff --git a/config.c b/config.c index 0fbddf0..289374b 100644 --- a/config.c +++ b/config.c @@ -21,6 +21,7 @@ */ #include +#include #include #include "configfile.h" @@ -96,17 +97,17 @@ void swb_config_free(struct swb_config *cfg) { /* Load a value into the part of a struct swb_config indicated by name */ static int swb_config_load_option(struct swb_config *cfg, char *name, char *value) { - int i; - struct swb_config_option opt; + struct swb_config_option *opt; + ptrdiff_t i; int retval = 0; - for (i = 0; swb_config_options[i].name; ++i) { - opt = swb_config_options[i]; - if (strcmp(name, opt.name)) + for (opt = swb_config_options; opt->name; ++opt) { + if (strcmp(name, opt->name)) continue; - if (!(cfg->flags & opt.set_mask)) { - switch (opt.type) { + if (!(cfg->flags & opt->set_mask)) { + i = opt - swb_config_options; + switch (opt->type) { case SWB_CONFIG_OPT_STRING: *(char **)cfg->entries[i] = value; break; @@ -115,7 +116,7 @@ static int swb_config_load_option(struct swb_config *cfg, free(value); break; } - cfg->flags |= opt.set_mask; + cfg->flags |= opt->set_mask; } retval = 1; break; -- 1.7.9.5