From: Karel Zak Date: Wed, 19 Aug 2020 11:08:44 +0000 (+0200) Subject: lscpu: move to main function to init context X-Git-Tag: v2.37-rc1~331 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=27c349f9d11c1e9e5274760c0b4bb39ef5e54c65;p=thirdparty%2Futil-linux.git lscpu: move to main function to init context Signed-off-by: Karel Zak --- diff --git a/sys-utils/lscpu-cputype.c b/sys-utils/lscpu-cputype.c index 00b184f6d0..293e4694b3 100644 --- a/sys-utils/lscpu-cputype.c +++ b/sys-utils/lscpu-cputype.c @@ -6,27 +6,6 @@ #include "fileutils.h" -static void context_init_paths(struct lscpu_cxt *cxt) -{ - DBG(MISC, ul_debugobj(cxt, "initialize paths")); - ul_path_init_debug(); - - /* /sys/devices/system/cpu */ - cxt->syscpu = ul_new_path(_PATH_SYS_CPU); - if (!cxt->syscpu) - err(EXIT_FAILURE, _("failed to initialize CPUs sysfs handler")); - if (cxt->prefix) - ul_path_set_prefix(cxt->syscpu, cxt->prefix); - - /* /proc */ - cxt->procfs = ul_new_path("/proc"); - if (!cxt->procfs) - err(EXIT_FAILURE, _("failed to initialize procfs handler")); - if (cxt->prefix) - ul_path_set_prefix(cxt->procfs, cxt->prefix); -} - - /* Lookup a pattern and get the value for format " : " */ int lookup(char *line, char *pattern, char **value) @@ -856,59 +835,6 @@ done: #ifdef TEST_PROGRAM_CPUTYPE /* TODO: move to lscpu.c */ -struct lscpu_cxt *lscpu_new_context(void) -{ - return xcalloc(1, sizeof(struct lscpu_cxt)); -} - -void lscpu_free_context(struct lscpu_cxt *cxt) -{ - size_t i; - - if (!cxt) - return; - - DBG(MISC, ul_debugobj(cxt, "freeing context")); - - DBG(MISC, ul_debugobj(cxt, " de-initialize paths")); - ul_unref_path(cxt->syscpu); - ul_unref_path(cxt->procfs); - - DBG(MISC, ul_debugobj(cxt, " freeing cpus")); - for (i = 0; i < cxt->npossibles; i++) { - lscpu_unref_cpu(cxt->cpus[i]); - cxt->cpus[i] = NULL; - } - DBG(MISC, ul_debugobj(cxt, " freeing types")); - for (i = 0; i < cxt->ncputypes; i++) { - lscpu_unref_cputype(cxt->cputypes[i]); - cxt->cputypes[i] = NULL; - } - - free(cxt->present); - free(cxt->online); - free(cxt->cputypes); - free(cxt->cpus); - - for (i = 0; i < cxt->nvuls; i++) { - free(cxt->vuls[i].name); - free(cxt->vuls[i].text); - } - free(cxt->vuls); - - for (i = 0; i < cxt->nnodes; i++) - free(cxt->nodemaps[i]); - - free(cxt->nodemaps); - free(cxt->idx2nodenum); - - lscpu_free_virtualization(cxt->virt); - lscpu_free_architecture(cxt->arch); - lscpu_free_caches(cxt->ecaches, cxt->necaches); - - free(cxt); -} - int main(int argc, char **argv) { struct lscpu_cxt *cxt; diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c index e51bb5147e..67e0752625 100644 --- a/sys-utils/lscpu.c +++ b/sys-utils/lscpu.c @@ -171,7 +171,6 @@ static void lscpu_init_debug(void) __UL_INIT_DEBUG_FROM_ENV(lscpu, LSCPU_DEBUG_, 0, LSCPU_DEBUG); } - static int cpu_column_name_to_id(const char *name, size_t namesz) { @@ -202,6 +201,79 @@ cache_column_name_to_id(const char *name, size_t namesz) return -1; } +static void lscpu_context_init_paths(struct lscpu_cxt *cxt) +{ + DBG(MISC, ul_debugobj(cxt, "initialize paths")); + ul_path_init_debug(); + + /* /sys/devices/system/cpu */ + cxt->syscpu = ul_new_path(_PATH_SYS_CPU); + if (!cxt->syscpu) + err(EXIT_FAILURE, _("failed to initialize CPUs sysfs handler")); + if (cxt->prefix) + ul_path_set_prefix(cxt->syscpu, cxt->prefix); + + /* /proc */ + cxt->procfs = ul_new_path("/proc"); + if (!cxt->procfs) + err(EXIT_FAILURE, _("failed to initialize procfs handler")); + if (cxt->prefix) + ul_path_set_prefix(cxt->procfs, cxt->prefix); +} + +static struct lscpu_cxt *lscpu_new_context(void) +{ + return xcalloc(1, sizeof(struct lscpu_cxt)); +} + +static void lscpu_free_context(struct lscpu_cxt *cxt) +{ + size_t i; + + if (!cxt) + return; + + DBG(MISC, ul_debugobj(cxt, "freeing context")); + + DBG(MISC, ul_debugobj(cxt, " de-initialize paths")); + ul_unref_path(cxt->syscpu); + ul_unref_path(cxt->procfs); + + DBG(MISC, ul_debugobj(cxt, " freeing cpus")); + for (i = 0; i < cxt->npossibles; i++) { + lscpu_unref_cpu(cxt->cpus[i]); + cxt->cpus[i] = NULL; + } + DBG(MISC, ul_debugobj(cxt, " freeing types")); + for (i = 0; i < cxt->ncputypes; i++) { + lscpu_unref_cputype(cxt->cputypes[i]); + cxt->cputypes[i] = NULL; + } + + free(cxt->present); + free(cxt->online); + free(cxt->cputypes); + free(cxt->cpus); + + for (i = 0; i < cxt->nvuls; i++) { + free(cxt->vuls[i].name); + free(cxt->vuls[i].text); + } + free(cxt->vuls); + + for (i = 0; i < cxt->nnodes; i++) + free(cxt->nodemaps[i]); + + free(cxt->nodemaps); + free(cxt->idx2nodenum); + + lscpu_free_virtualization(cxt->virt); + lscpu_free_architecture(cxt->arch); + lscpu_free_caches(cxt->ecaches, cxt->necaches); + + free(cxt); +} + #ifdef LSCPU_OLD_OUTPUT_CODE /* temporary disabled for revrite */ static char * @@ -1022,9 +1094,12 @@ static void __attribute__((__noreturn__)) usage(void) int main(int argc, char *argv[]) { + struct lscpu_cxt *cxt; lscpu_init_debug(); + cxt = lscpu_new_context(); + #ifdef LSCPU_OLD_OUTPUT_CODE struct lscpu_modifier _mod = { .mode = OUTPUT_SUMMARY }, *mod = &_mod; struct lscpu_desc _desc = { .flags = NULL }, *desc = &_desc; @@ -1276,5 +1351,8 @@ int main(int argc, char *argv[]) ul_unref_path(desc->syscpu); ul_unref_path(desc->procfs); #endif /* LSCPU_OLD_OUTPUT_CODE */ + + lscpu_free_context(cxt); + return EXIT_SUCCESS; } diff --git a/sys-utils/lscpu.h b/sys-utils/lscpu.h index 79e2e95d2d..8a71b8278a 100644 --- a/sys-utils/lscpu.h +++ b/sys-utils/lscpu.h @@ -249,9 +249,6 @@ struct lscpu_cpu *lscpu_cpus_loopup_by_type(struct lscpu_cxt *cxt, struct lscpu_ void lscpu_decode_arm(struct lscpu_cxt *cxt); -struct lscpu_cxt *lscpu_new_context(void); -void lscpu_free_context(struct lscpu_cxt *cxt); - int lookup(char *line, char *pattern, char **value); struct lscpu_dmi_header