diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-05-18 23:01:32 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-05-19 16:42:37 +0200 |
commit | 319a4f4bc46b230fc660321e99aaac1bc449deea (patch) | |
tree | 9eca2e1352df29aeeef91e4fd4bcc12ad4ea44e9 /src/basic/hashmap.c | |
parent | 99480504d47c0a6bbb1ac230cc33df12bbd36c54 (diff) | |
download | systemd-319a4f4bc46b230fc660321e99aaac1bc449deea.tar.gz |
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.)
Diffstat (limited to 'src/basic/hashmap.c')
-rw-r--r-- | src/basic/hashmap.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index e354c67adc..34fed69d99 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -230,7 +230,7 @@ struct Set { typedef struct CacheMem { const void **ptr; - size_t n_populated, n_allocated; + size_t n_populated; bool active:1; } CacheMem; @@ -1897,10 +1897,10 @@ int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags } /* expand the cachemem if needed, return true if newly (re)activated. */ -static int cachemem_maintain(CacheMem *mem, unsigned size) { +static int cachemem_maintain(CacheMem *mem, size_t size) { assert(mem); - if (!GREEDY_REALLOC(mem->ptr, mem->n_allocated, size)) { + if (!GREEDY_REALLOC(mem->ptr, size)) { if (size > 0) return -ENOMEM; } @@ -1915,7 +1915,7 @@ static int cachemem_maintain(CacheMem *mem, unsigned size) { int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries) { bool sync_keys = false, sync_values = false; - unsigned size; + size_t size; int r; assert(cache); @@ -1988,8 +1988,8 @@ IteratedCache* iterated_cache_free(IteratedCache *cache) { } int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **ret) { - size_t separator_len, allocated = 0, len = 0; _cleanup_free_ char *str = NULL; + size_t separator_len, len = 0; const char *value; bool first; @@ -2013,7 +2013,7 @@ int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char ** if (l == 0) continue; - if (!GREEDY_REALLOC(str, allocated, len + l + (first ? 0 : separator_len) + (wrap_with_separator ? separator_len : 0) + 1)) + if (!GREEDY_REALLOC(str, len + l + (first ? 0 : separator_len) + (wrap_with_separator ? separator_len : 0) + 1)) return -ENOMEM; if (separator_len > 0 && !first) { |