From 74573799ca6d11b6f945bd900b2cb5f818b4a976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 8 Apr 2026 17:17:32 +0200 Subject: [PATCH] Add DEFINE_ARRAY_DONE_FUNC 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 | 8 ++++++++ src/nspawn/nspawn.c | 7 +------ src/shared/format-table.h | 1 + src/vmspawn/vmspawn.c | 7 +------ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/fundamental/cleanup-fundamental.h b/src/fundamental/cleanup-fundamental.h index 86b9851fd5..91d7cdd0e8 100644 --- a/src/fundamental/cleanup-fundamental.h +++ b/src/fundamental/cleanup-fundamental.h @@ -45,6 +45,14 @@ #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 */ diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 10fda88c48..accf448ea9 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -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]); diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 0f52f80293..7665c93e59 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -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) { diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index f5dc4f17a8..5064f09c7d 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -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]);