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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-04-10 15:08:02 +02:00
parent e2227c8a20
commit 74573799ca
4 changed files with 11 additions and 12 deletions
+8
View File
@@ -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 */
+1 -6
View 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]);
+1
View 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) {
+1 -6
View 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]);