alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()

We recently started making more use of malloc_usable_size() and rely on
it (see the string_erase() story). Given that we don't really support
sytems where malloc_usable_size() cannot be trusted beyond statistics
anyway, let's go fully in and rework GREEDY_REALLOC() on top of it:
instead of passing around and maintaining the currenly allocated size
everywhere, let's just derive it automatically from
malloc_usable_size().

I am mostly after this for the simplicity this brings. It also brings
minor efficiency improvements I guess, but things become so much nicer
to look at if we can avoid these allocation size variables everywhere.

Note that the malloc_usable_size() man page says relying on it wasn't
"good programming practice", but I think it does this for reasons that
don't apply here: the greedy realloc logic specifically doesn't rely on
the returned extra size, beyond the fact that it is equal or larger than
what was requested.

(This commit was supposed to be a quick patch btw, but apparently we use
the greedy realloc stuff quite a bit across the codebase, so this ends
up touching *a*lot* of code.)
This commit is contained in:
Lennart Poettering
2021-05-19 16:42:37 +02:00
parent 99480504d4
commit 319a4f4bc4
127 changed files with 658 additions and 688 deletions
+6 -9
View File
@@ -136,7 +136,6 @@ struct Table {
size_t cell_height_max; /* Maximum number of lines per cell. (If there are more, ellipsis is shown. If SIZE_MAX then no limit is set, the default. == 0 is not allowed.) */
TableData **data;
size_t n_allocated;
size_t *display_map; /* List of columns to show (by their index). It's fine if columns are listed multiple times or not at all */
size_t n_display_map;
@@ -453,7 +452,7 @@ int table_add_cell_full(
return -ENOMEM;
}
if (!GREEDY_REALLOC(t->data, t->n_allocated, MAX(t->n_cells + 1, t->n_columns)))
if (!GREEDY_REALLOC(t->data, MAX(t->n_cells + 1, t->n_columns)))
return -ENOMEM;
if (ret_cell)
@@ -510,7 +509,7 @@ int table_dup_cell(Table *t, TableCell *cell) {
if (i >= t->n_cells)
return -ENXIO;
if (!GREEDY_REALLOC(t->data, t->n_allocated, MAX(t->n_cells + 1, t->n_columns)))
if (!GREEDY_REALLOC(t->data, MAX(t->n_cells + 1, t->n_columns)))
return -ENOMEM;
t->data[t->n_cells++] = table_data_ref(t->data[i]);
@@ -1094,19 +1093,18 @@ static int table_set_display_all(Table *t) {
}
int table_set_display_internal(Table *t, size_t first_column, ...) {
size_t allocated, column;
size_t column;
va_list ap;
assert(t);
allocated = t->n_display_map;
column = first_column;
va_start(ap, first_column);
for (;;) {
assert(column < t->n_columns);
if (!GREEDY_REALLOC(t->display_map, allocated, MAX(t->n_columns, t->n_display_map+1))) {
if (!GREEDY_REALLOC(t->display_map, MAX(t->n_columns, t->n_display_map+1))) {
va_end(ap);
return -ENOMEM;
}
@@ -1124,19 +1122,18 @@ int table_set_display_internal(Table *t, size_t first_column, ...) {
}
int table_set_sort_internal(Table *t, size_t first_column, ...) {
size_t allocated, column;
size_t column;
va_list ap;
assert(t);
allocated = t->n_sort_map;
column = first_column;
va_start(ap, first_column);
for (;;) {
assert(column < t->n_columns);
if (!GREEDY_REALLOC(t->sort_map, allocated, MAX(t->n_columns, t->n_sort_map+1))) {
if (!GREEDY_REALLOC(t->sort_map, MAX(t->n_columns, t->n_sort_map+1))) {
va_end(ap);
return -ENOMEM;
}