alloc-util: add free_many() helper
We often free an array of things. Let's create a common helper for this, and port some potential users over. (Not all, too lazy for that for now).
This commit is contained in:
@@ -242,4 +242,17 @@ static inline size_t malloc_sizeof_safe(void **xp) {
|
||||
(char*) memdupa_suffix0(_t, strnlen(_t, (n))); \
|
||||
})
|
||||
|
||||
/* Free every element of the array. */
|
||||
static inline void free_many(void **p, size_t n) {
|
||||
assert(p || n == 0);
|
||||
|
||||
FOREACH_ARRAY(i, p, n)
|
||||
*i = mfree(*i);
|
||||
}
|
||||
|
||||
/* Typesafe wrapper for char** rather than void**. Unfortunately C won't implicitly cast this. */
|
||||
static inline void free_many_charp(char **c, size_t n) {
|
||||
free_many((void**) c, n);
|
||||
}
|
||||
|
||||
#include "memory-util.h"
|
||||
|
||||
@@ -1915,9 +1915,7 @@ int cg_get_keyed_attribute_full(
|
||||
r = -ENXIO;
|
||||
|
||||
fail:
|
||||
for (i = 0; i < n; i++)
|
||||
free(v[i]);
|
||||
|
||||
free_many_charp(v, n);
|
||||
return r;
|
||||
|
||||
done:
|
||||
|
||||
@@ -272,11 +272,7 @@ int extract_many_words(const char **p, const char *separators, unsigned flags, .
|
||||
|
||||
r = extract_first_word(p, &l[c], separators, flags);
|
||||
if (r < 0) {
|
||||
int j;
|
||||
|
||||
for (j = 0; j < c; j++)
|
||||
free(l[j]);
|
||||
|
||||
free_many_charp(l, c);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -340,11 +340,7 @@ out:
|
||||
}
|
||||
|
||||
void locale_variables_free(char *l[_VARIABLE_LC_MAX]) {
|
||||
if (!l)
|
||||
return;
|
||||
|
||||
for (LocaleVariable i = 0; i < _VARIABLE_LC_MAX; i++)
|
||||
l[i] = mfree(l[i]);
|
||||
free_many_charp(l, _VARIABLE_LC_MAX);
|
||||
}
|
||||
|
||||
void locale_variables_simplify(char *l[_VARIABLE_LC_MAX]) {
|
||||
|
||||
@@ -359,13 +359,7 @@ int rlimit_from_string_harder(const char *s) {
|
||||
}
|
||||
|
||||
void rlimit_free_all(struct rlimit **rl) {
|
||||
int i;
|
||||
|
||||
if (!rl)
|
||||
return;
|
||||
|
||||
for (i = 0; i < _RLIMIT_MAX; i++)
|
||||
rl[i] = mfree(rl[i]);
|
||||
free_many((void**) rl, _RLIMIT_MAX);
|
||||
}
|
||||
|
||||
int rlimit_nofile_bump(int limit) {
|
||||
|
||||
+1
-3
@@ -212,9 +212,7 @@ int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates) {
|
||||
return (int) i;
|
||||
|
||||
rollback:
|
||||
for (size_t j = 0; j < i; j++)
|
||||
free(t[p + j]);
|
||||
|
||||
free_many_charp(t + p, i);
|
||||
t[p] = NULL;
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user