]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lscpu: (cputype) add ref-counting, allocate context
authorKarel Zak <kzak@redhat.com>
Tue, 17 Mar 2020 12:41:28 +0000 (13:41 +0100)
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

index e5d317f1fbb7ad84461613526e9eeb779a407290..bcf183e9e0acf8bbd79f184ae80ad8b66738f035 100644 (file)
@@ -77,10 +77,18 @@ struct lscpu_cxt {
        struct path_cxt *syscpu; /* _PATH_SYS_CPU path handler */
        struct path_cxt *procfs; /* /proc path handler */
 
-       size_t  ncputypes;
-       struct lscpu_cputype *cputypes;
+       size_t ncputypes;
+       struct lscpu_cputype **cputypes;
 };
 
+struct lscpu_cputype *lscpu_new_cputype(void);
+void lscpu_ref_cputype(struct lscpu_cputype *ct);
+void lscpu_unref_cputype(struct lscpu_cputype *ct);
+int lscpu_read_cputypes(struct lscpu_cxt *cxt);
+
+struct lscpu_cxt *lscpu_new_context(void);
+static void lscpu_free_context(struct lscpu_cxt *cxt);
+
 /*** endof-TODO ***/
 
 static void lscpu_init_debug(void)
@@ -108,17 +116,8 @@ static void context_init_paths(struct lscpu_cxt *cxt)
                ul_path_set_prefix(cxt->procfs, cxt->prefix);
 }
 
-static void free_context(struct lscpu_cxt *cxt)
-{
-       if (!cxt)
-               return;
-
-       DBG(MISC, ul_debugobj(cxt, "de-initialize paths"));
-       ul_unref_path(cxt->syscpu);
-       ul_unref_path(cxt->procfs);
-}
 
-static struct lscpu_cputype *lscpu_new_cputype(void)
+struct lscpu_cputype *lscpu_new_cputype(void)
 {
        struct lscpu_cputype *ct;
 
@@ -129,6 +128,49 @@ static struct lscpu_cputype *lscpu_new_cputype(void)
        return ct;
 }
 
+void lscpu_ref_cputype(struct lscpu_cputype *ct)
+{
+       if (ct)
+               ct->refcount++;
+}
+
+void lscpu_unref_cputype(struct lscpu_cputype *ct)
+{
+       if (!ct)
+               return;
+
+       if (--ct->refcount <= 0) {
+               DBG(TYPE, ul_debugobj(ct, " freeing"));
+               free(ct);
+       }
+}
+
+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 types"));
+       for (i = 0; i < cxt->ncputypes; i++)
+               lscpu_unref_cputype(cxt->cputypes[i]);
+
+       free(cxt->cputypes);
+       free(cxt);
+}
+
 int lscpu_read_cputypes(struct lscpu_cxt *cxt)
 {
        DBG(GATHER, ul_debugobj(cxt, "reading types"));
@@ -138,8 +180,9 @@ int lscpu_read_cputypes(struct lscpu_cxt *cxt)
 #ifdef TEST_PROGRAM_CPUTYPE
 int main(int argc, char **argv)
 {
-       struct lscpu_cxt _cxt = { .prefix = NULL },
-                        *cxt = &_cxt;
+       struct lscpu_cxt *cxt;
+
+       cxt = lscpu_new_context();
 
        if (argc == 3 && strcmp(argv[1], "--prefix") == 0)
                cxt->prefix = argv[2];
@@ -149,7 +192,7 @@ int main(int argc, char **argv)
 
        lscpu_read_cputypes(cxt);
 
-       free_context(cxt);
+       lscpu_free_context(cxt);
 
        return EXIT_SUCCESS;
 }