]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: get rid of strv_clear()
authorLennart Poettering <lennart@poettering.net>
Tue, 21 Jan 2020 09:07:34 +0000 (10:07 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 21 Jan 2020 09:07:34 +0000 (10:07 +0100)
Let's remove a function of questionnable utility.

strv_clear() frees the items of a string array, but not the array
itself. i.e. it half-drestructs a string array and makes it empty. This
is not too useful an operation since we almost never need to just do
that, we also want to free the whole thing. In fact, strv_clear() is
only used in one of our .c file, and there it appears like unnecessary
optimization, given that for each array with n elements it leaves the
number of free()s we need to at O(n) which is not really an optimization
at all (it goes from n+1 to n, that's all).

Prompted by the discussions on #14605

src/basic/strv.c
src/basic/strv.h
src/libsystemd/sd-hwdb/hwdb-util.c

index 74d20a9a95a6b5477bb2097f1dd764e0006814f9..096cb4e5d4d2fa545d99dba10d468d36471e4d9f 100644 (file)
@@ -57,20 +57,15 @@ char *strv_find_startswith(char * const *l, const char *name) {
         return NULL;
 }
 
-void strv_clear(char **l) {
+char **strv_free(char **l) {
         char **k;
 
         if (!l)
-                return;
+                return NULL;
 
         for (k = l; *k; k++)
                 free(*k);
 
-        *l = NULL;
-}
-
-char **strv_free(char **l) {
-        strv_clear(l);
         return mfree(l);
 }
 
index 85a49ab5c3fa24abd1eafeb3f4c688f6abf0714a..e7c2b1a604fc36835f7a13353f746a3794aa0043 100644 (file)
@@ -25,8 +25,6 @@ char **strv_free_erase(char **l);
 DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free_erase);
 #define _cleanup_strv_free_erase_ _cleanup_(strv_free_erasep)
 
-void strv_clear(char **l);
-
 char **strv_copy(char * const *l);
 size_t strv_length(char * const *l) _pure_;
 
index c83575c7c876c84234c1ba3cff97e29297031e7f..1a2da9c79bd9fc13abaa3e25b39f0746f886b9c2 100644 (file)
@@ -543,7 +543,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                                            "Property expected, ignoring record with no properties");
                                 r = -EINVAL;
                                 state = HW_NONE;
-                                strv_clear(match_list);
+                                match_list = strv_free(match_list);
                                 break;
                         }
 
@@ -571,7 +571,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                         if (len == 0) {
                                 /* end of record */
                                 state = HW_NONE;
-                                strv_clear(match_list);
+                                match_list = strv_free(match_list);
                                 break;
                         }
 
@@ -580,7 +580,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                                            "Property or empty line expected, got \"%s\", ignoring record", line);
                                 r = -EINVAL;
                                 state = HW_NONE;
-                                strv_clear(match_list);
+                                match_list = strv_free(match_list);
                                 break;
                         }