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.
This commit is contained in:
Lennart Poettering 2019-03-20 10:31:38 +01:00
parent 23964f7faf
commit d4b604baea
1 changed files with 2 additions and 1 deletions

View 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;
}