about summary refs log tree commit diff
path: root/shared/systemd/src/basic/alloc-util.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2019-07-31 10:52:41 +0200
committerMichael Biebl <biebl@debian.org>2019-07-31 10:52:41 +0200
commit3a99ba5fd066fe42bd5461360a7411766496619c (patch)
treeb43863fbe867d7b0f9f9032fd40664c521c88fcb /shared/systemd/src/basic/alloc-util.c
parent13cfe714153013158bdf3e74ab58ab713a04223c (diff)
parent2e5fa45ddfbb5cffa1e78221f1cea706e2f298af (diff)
Update upstream source from tag 'upstream/1.19.90'
Update to upstream version '1.19.90'
with Debian dir 6d1a5b9902866beee1365904d425141b5d4c944c
Diffstat (limited to 'shared/systemd/src/basic/alloc-util.c')
-rw-r--r--shared/systemd/src/basic/alloc-util.c25
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;
 }