From: Lennart Poettering Date: Wed, 20 Mar 2019 09:31:38 +0000 (+0100) Subject: alloc-util: use malloc_usable_size() to determine allocated size X-Git-Tag: v242-rc1~103^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d4b604baeadbb2498e4f2c3e260260eed210f5d6;p=thirdparty%2Fsystemd.git alloc-util: use malloc_usable_size() to determine allocated size It's a glibc-specific API, but supported on FreeBSD and musl too at least, hence fairly common. This way we can reduce our calls to realloc() as much as possible. --- diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c index f4bd33f4e01..1e4ee722f20 100644 --- a/src/basic/alloc-util.c +++ b/src/basic/alloc-util.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include #include #include @@ -64,7 +65,7 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) { return NULL; *p = q; - *allocated = newalloc; + *allocated = _unlikely_(size == 0) ? newalloc : malloc_usable_size(q) / size; return q; }