]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
alloc-util: use malloc_usable_size() to determine allocated size
authorLennart Poettering <lennart@poettering.net>
Wed, 20 Mar 2019 09:31:38 +0000 (10:31 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 20 Mar 2019 09:48:33 +0000 (10:48 +0100)
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.

src/basic/alloc-util.c

index f4bd33f4e014246a9c9e68f8f75e0023541a967d..1e4ee722f204c4748ae916aa485f8e102c245570 100644 (file)
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
+#include <malloc.h>
 #include <stdint.h>
 #include <string.h>
 
@@ -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;
 }