]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add DEFINE_ARRAY_DONE_FUNC
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Wed, 8 Apr 2026 15:17:32 +0000 (17:17 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Fri, 10 Apr 2026 13:08:02 +0000 (15:08 +0200)
This is a helper macro that defines a function to drop elements of an
array but not the array itself. I used the "_many" suffix because it
most closely matches what happens here: we are calling the cleanup
function a bunch of times.

src/fundamental/cleanup-fundamental.h
src/nspawn/nspawn.c
src/shared/format-table.h
src/vmspawn/vmspawn.c

index 86b9851fd54ab4e58343b0b4d7e00ca9c47d76b3..91d7cdd0e8986a19b471caa9dd6c8c3a078a9e75 100644 (file)
 #define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO(type, macro, empty)      \
         DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO_RENAME(type, macro, macro##p, empty)
 
+/* Clean up a NULL-terminated array by dropping all the items in it (up to the first NULL).
+ * The array itself is not deallocated. */
+#define DEFINE_ARRAY_DONE_FUNC(type, helper)                    \
+        void helper ## _many(type (*p)[]) {                     \
+                for (type *t = *ASSERT_PTR(p); *t; t++)         \
+                        *t = helper(*t);                        \
+        }
+
 typedef void (*free_array_func_t)(void *p, size_t n);
 
 /* An automatic _cleanup_-like logic for destroy arrays (i.e. pointers + size) when leaving scope */
index 10fda88c480547b50dbeb24f6ff2cdba423f64cf..accf448ea97f2876e8dde76e1473fb93c3b0a6d2 100644 (file)
@@ -364,11 +364,6 @@ static int parse_private_users(
         return 0;
 }
 
-static void unref_many_tables(Table* (*tablesp)[]) {
-        for (Table **t = *ASSERT_PTR(tablesp); *t; t++)
-                *t = table_unref(*t);
-}
-
 static int help(void) {
         _cleanup_free_ char *link = NULL;
         int r;
@@ -396,7 +391,7 @@ static int help(void) {
                 "Other",
         };
 
-        _cleanup_(unref_many_tables) Table* tables[ELEMENTSOF(groups) + 1] = {};
+        _cleanup_(table_unref_many) Table* tables[ELEMENTSOF(groups) + 1] = {};
 
         for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
                 r = option_parser_get_help_table_group(groups[i], &tables[i]);
index 0f52f80293d2e64750217cde0b2ea229a6f84f85..7665c93e593e61a0b62c8b088c7b04a281bccf00 100644 (file)
@@ -100,6 +100,7 @@ Table* table_new_vertical(void);
 Table* table_unref(Table *t);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
+static inline DEFINE_ARRAY_DONE_FUNC(Table*, table_unref);
 
 int table_add_cell_full(Table *t, TableCell **ret_cell, TableDataType dt, const void *data, size_t minimum_width, size_t maximum_width, unsigned weight, unsigned align_percent, unsigned ellipsize_percent);
 static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType dt, const void *data) {
index f5dc4f17a8ce30c843333631a71a53e6350a1593..5064f09c7d1a3a55778ec6d68b681c19797f9006 100644 (file)
@@ -203,11 +203,6 @@ STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_bind_user_shell, freep);
 STATIC_DESTRUCTOR_REGISTER(arg_bind_user_groups, strv_freep);
 
-static void unref_many_tables(Table* (*tablesp)[]) {
-        for (Table **t = *ASSERT_PTR(tablesp); *t; t++)
-                *t = table_unref(*t);
-}
-
 static int help(void) {
         _cleanup_free_ char *link = NULL;
         int r;
@@ -232,7 +227,7 @@ static int help(void) {
                 "Credentials",
         };
 
-        _cleanup_(unref_many_tables) Table* tables[ELEMENTSOF(groups) + 1] = {};
+        _cleanup_(table_unref_many) Table* tables[ELEMENTSOF(groups) + 1] = {};
 
         for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
                 r = option_parser_get_help_table_group(groups[i], &tables[i]);