diff options
| author | Sebastien Bacher <seb128@ubuntu.com> | 2019-08-09 11:09:27 +0200 |
|---|---|---|
| committer | Sebastien Bacher <seb128@ubuntu.com> | 2019-08-09 11:18:39 +0200 |
| commit | 820498b2158e0b05421ce0922f203a0699927fa8 (patch) | |
| tree | 3bf6d1089b762535ec5e59738d47ae2d2f54750f /shared/systemd/src/basic/alloc-util.c | |
| parent | 1bd9f376796563dfb5016bada66c78b203f41d33 (diff) | |
| parent | 488dda3930545969f5644b57cfca81e21df031c3 (diff) | |
Merge remote-tracking branch 'salsa/master'
Diffstat (limited to 'shared/systemd/src/basic/alloc-util.c')
| -rw-r--r-- | shared/systemd/src/basic/alloc-util.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/shared/systemd/src/basic/alloc-util.c b/shared/systemd/src/basic/alloc-util.c index 92b350bc..c97d8700 100644 --- a/shared/systemd/src/basic/alloc-util.c +++ b/shared/systemd/src/basic/alloc-util.c @@ -66,8 +66,31 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) { if (!q) return NULL; + if (size > 0) { + size_t bn; + + /* Adjust for the 64 byte minimum */ + newalloc = a / size; + + bn = malloc_usable_size(q) / size; + if (bn > newalloc) { + void *qq; + + /* The actual size allocated is larger than what we asked for. Let's call realloc() again to + * take possession of the extra space. This should be cheap, since libc doesn't have to move + * the memory for this. */ + + qq = realloc(q, bn * size); + if (_likely_(qq)) { + *p = qq; + *allocated = bn; + return qq; + } + } + } + *p = q; - *allocated = _unlikely_(size == 0) ? newalloc : malloc_usable_size(q) / size; + *allocated = newalloc; return q; } |