]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lscpu: move to main function to init context
authorKarel Zak <kzak@redhat.com>
Wed, 19 Aug 2020 11:08:44 +0000 (13:08 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 13 Nov 2020 08:19:02 +0000 (09:19 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/lscpu-cputype.c
sys-utils/lscpu.c
sys-utils/lscpu.h

index 00b184f6d00aad61fc7fde67c01084c54814d3a6..293e4694b3cfccc0f5e7c578e5376585dfaf42c2 100644 (file)
@@ -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  "<pattern> : <key>"
  */
 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;
index e51bb5147ee0068db1337df050d0c317eb2ec269..67e07526255a46935f6111323f79308688f7781a 100644 (file)
@@ -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;
 }
index 79e2e95d2d0ed4018deaca40b7097ee9c44a4df5..8a71b8278aaf463e4eafb4fd93bf8aa92bc684ad 100644 (file)
@@ -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