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:
@@ -131,7 +131,6 @@ int list_jobs(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_free_ struct job_info *jobs = NULL;
|
||||
const char *name, *type, *state;
|
||||
bool skipped = false;
|
||||
size_t size = 0;
|
||||
unsigned c = 0;
|
||||
sd_bus *bus;
|
||||
uint32_t id;
|
||||
@@ -157,7 +156,7 @@ int list_jobs(int argc, char *argv[], void *userdata) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!GREEDY_REALLOC(jobs, size, c + 1))
|
||||
if (!GREEDY_REALLOC(jobs, c + 1))
|
||||
return log_oom();
|
||||
|
||||
jobs[c++] = job;
|
||||
|
||||
@@ -93,7 +93,6 @@ static int get_machine_list(
|
||||
struct machine_info *machine_infos = NULL;
|
||||
_cleanup_strv_free_ char **m = NULL;
|
||||
_cleanup_free_ char *hn = NULL;
|
||||
size_t sz = 0;
|
||||
char **i;
|
||||
int c = 0, r;
|
||||
|
||||
@@ -102,7 +101,7 @@ static int get_machine_list(
|
||||
return log_oom();
|
||||
|
||||
if (output_show_machine(hn, patterns)) {
|
||||
if (!GREEDY_REALLOC0(machine_infos, sz, c+1))
|
||||
if (!GREEDY_REALLOC0(machine_infos, c+1))
|
||||
return log_oom();
|
||||
|
||||
machine_infos[c].is_host = true;
|
||||
@@ -126,7 +125,7 @@ static int get_machine_list(
|
||||
if (!streq_ptr(class, "container"))
|
||||
continue;
|
||||
|
||||
if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) {
|
||||
if (!GREEDY_REALLOC0(machine_infos, c+1)) {
|
||||
free_machines_list(machine_infos, c);
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
@@ -136,7 +136,6 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) {
|
||||
int list_unit_files(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
_cleanup_free_ UnitFileList *units = NULL;
|
||||
size_t size = 0;
|
||||
unsigned c = 0;
|
||||
const char *state;
|
||||
char *path;
|
||||
@@ -234,7 +233,7 @@ int list_unit_files(int argc, char *argv[], void *userdata) {
|
||||
|
||||
while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) {
|
||||
|
||||
if (!GREEDY_REALLOC(units, size, c + 1))
|
||||
if (!GREEDY_REALLOC(units, c + 1))
|
||||
return log_oom();
|
||||
|
||||
units[c] = (struct UnitFileList) {
|
||||
|
||||
@@ -432,7 +432,6 @@ int list_sockets(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_free_ UnitInfo *unit_infos = NULL;
|
||||
_cleanup_free_ struct socket_info *socket_infos = NULL;
|
||||
unsigned cs = 0;
|
||||
size_t size = 0;
|
||||
int r, n;
|
||||
sd_bus *bus;
|
||||
|
||||
@@ -468,7 +467,7 @@ int list_sockets(int argc, char *argv[], void *userdata) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (!GREEDY_REALLOC(socket_infos, size, cs + c)) {
|
||||
if (!GREEDY_REALLOC(socket_infos, cs + c)) {
|
||||
r = log_oom();
|
||||
goto cleanup;
|
||||
}
|
||||
@@ -695,7 +694,6 @@ int list_timers(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_strv_free_ char **timers_with_suffix = NULL;
|
||||
_cleanup_free_ struct timer_info *timer_infos = NULL;
|
||||
_cleanup_free_ UnitInfo *unit_infos = NULL;
|
||||
size_t size = 0;
|
||||
int n, c = 0;
|
||||
dual_timestamp nw;
|
||||
sd_bus *bus;
|
||||
@@ -736,7 +734,7 @@ int list_timers(int argc, char *argv[], void *userdata) {
|
||||
|
||||
get_last_trigger(bus, u->unit_path, &last);
|
||||
|
||||
if (!GREEDY_REALLOC(timer_infos, size, c+1)) {
|
||||
if (!GREEDY_REALLOC(timer_infos, c+1)) {
|
||||
r = log_oom();
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@@ -158,7 +158,6 @@ int get_unit_list(
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
size_t size = c;
|
||||
int r;
|
||||
bool fallback = false;
|
||||
|
||||
@@ -218,7 +217,7 @@ int get_unit_list(
|
||||
if (!output_show_unit(&u, fallback ? patterns : NULL))
|
||||
continue;
|
||||
|
||||
if (!GREEDY_REALLOC(*unit_infos, size, c+1))
|
||||
if (!GREEDY_REALLOC(*unit_infos, c+1))
|
||||
return log_oom();
|
||||
|
||||
(*unit_infos)[c++] = u;
|
||||
@@ -261,17 +260,16 @@ int expand_unit_names(sd_bus *bus, char **names, const char* suffix, char ***ret
|
||||
if (expanded) {
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
_cleanup_free_ UnitInfo *unit_infos = NULL;
|
||||
size_t allocated, n;
|
||||
size_t n;
|
||||
|
||||
r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
n = strv_length(mangled);
|
||||
allocated = n + 1;
|
||||
|
||||
for (int i = 0; i < r; i++) {
|
||||
if (!GREEDY_REALLOC(mangled, allocated, n+2))
|
||||
if (!GREEDY_REALLOC(mangled, n+2))
|
||||
return log_oom();
|
||||
|
||||
mangled[n] = strdup(unit_infos[i].id);
|
||||
|
||||
Reference in New Issue
Block a user