]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/configs: merge new_list_entry() and configs_add_filename()
authorKarel Zak <kzak@redhat.com>
Wed, 15 Oct 2025 12:59:44 +0000 (14:59 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 15 Oct 2025 12:59:44 +0000 (14:59 +0200)
Introduce configs_refer_filename() which merges the functionality
of new_list_entry() and configs_add_filename() while avoiding
unnecessary string duplication.

The new function takes ownership of the filename pointer directly
instead of using strdup(), which eliminates one allocation per
config file entry. Callers no longer need to free the filename
after adding it to the list, as ownership is transferred to the
list element.

This improves both performance and code clarity by making the
ownership semantics explicit.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/configs.c

index 07576d2f0aa96345781c95cc81937f6e6c143363..1bad26599583e7af34d524fc0abd55df89b4984d 100644 (file)
@@ -74,36 +74,16 @@ static char *main_configs(const char *root,
        return path;
 }
 
-static struct file_element *new_list_entry(const char *filename)
-{
-       struct file_element *file_element = calloc(1, sizeof(*file_element));
-
-       if (file_element == NULL)
-               return NULL;
-
-       INIT_LIST_HEAD(&file_element->file_list);
-
-       if (filename != NULL) {
-               file_element->filename = strdup(filename);
-               if (file_element->filename == NULL) {
-                       free(file_element);
-                       return NULL;
-               }
-       } else {
-               file_element->filename = NULL;
-       }
-
-       return file_element;
-}
-
-static int configs_add_filename(struct list_head *list, const char *filename)
+static int configs_refer_filename(struct list_head *list, char *filename)
 {
        struct file_element *e;
 
-       e = new_list_entry(filename);
+       e = calloc(1, sizeof(*e));
        if (e == NULL)
                return -ENOMEM;
 
+       INIT_LIST_HEAD(&e->file_list);
+       e->filename = filename;
        list_add_tail(&e->file_list, list);
        return 0;
 }
@@ -167,10 +147,11 @@ static int read_dir(struct list_head *file_list,
                        break;
                }
 
-               ret = configs_add_filename(file_list, filename);
-               free(filename);
-               if (ret < 0)
+               ret = configs_refer_filename(file_list, filename);
+               if (ret < 0) {
+                       free(filename);
                        break;
+               }
        }
 
 finish:
@@ -306,10 +287,11 @@ int ul_configs_file_list(struct list_head *file_list,
        if (main_file != NULL) {
                struct list_head *e;
 
-               ret = configs_add_filename(file_list, main_file);
-               free(main_file);
-               if (ret < 0)
+               ret = configs_refer_filename(file_list, main_file);
+               if (ret < 0) {
+                       free(main_file);
                        goto finish;
+               }
 
                /* Move last element (just added) to front */
                e = file_list->prev;