summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-05-06 17:17:22 +0200
committerGitHub <noreply@github.com>2019-05-06 17:17:22 +0200
commit3759c0bdd39135033400696c84f58d220a1a128d (patch)
tree164feae2df0d5bd07634d479cf5aabd695a0c6ea
parent5797a12223707f0fc9afd68811a7d1f993908f81 (diff)
parent413644757628952af3c9c503be579f094517093d (diff)
downloadsystemd-3759c0bdd39135033400696c84f58d220a1a128d.tar.gz
Merge pull request #12440 from poettering/realloc-again
another shot at the malloc_usable_size() thing
-rw-r--r--src/basic/alloc-util.c24
-rw-r--r--src/test/test-alloc-util.c27
2 files changed, 45 insertions, 6 deletions
diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c
index f4bd33f4e0..a16db6824f 100644
--- a/src/basic/alloc-util.c
+++ b/src/basic/alloc-util.c
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include <malloc.h>
#include <stdint.h>
#include <string.h>
@@ -63,6 +64,29 @@ 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 = newalloc;
return q;
diff --git a/src/test/test-alloc-util.c b/src/test/test-alloc-util.c
index ad10eb178a..2dfdfe35ec 100644
--- a/src/test/test-alloc-util.c
+++ b/src/test/test-alloc-util.c
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include <malloc.h>
#include <stdint.h>
#include "alloc-util.h"
@@ -21,21 +22,35 @@ static void test_alloca(void) {
static void test_GREEDY_REALLOC(void) {
_cleanup_free_ int *a = NULL, *b = NULL;
- size_t n_allocated = 0, i;
+ size_t n_allocated = 0, i, j;
- /* Give valgrind a chance to verify our realloc operations */
+ /* Give valgrind a chance to verify our realloc() operations */
- for (i = 0; i < 2048; i++) {
+ for (i = 0; i < 20480; i++) {
assert_se(GREEDY_REALLOC(a, n_allocated, i + 1));
- a[i] = i;
+ assert_se(n_allocated >= i + 1);
+ assert_se(malloc_usable_size(a) >= (i + 1) * sizeof(int));
+ a[i] = (int) i;
assert_se(GREEDY_REALLOC(a, n_allocated, i / 2));
+ assert_se(n_allocated >= i / 2);
+ assert_se(malloc_usable_size(a) >= (i / 2) * sizeof(int));
}
- for (i = 30, n_allocated = 0; i < 2048; i+=7) {
+ for (j = 0; j < i / 2; j++)
+ assert_se(a[j] == (int) j);
+
+ for (i = 30, n_allocated = 0; i < 20480; i += 7) {
assert_se(GREEDY_REALLOC(b, n_allocated, i + 1));
- b[i] = i;
+ assert_se(n_allocated >= i + 1);
+ assert_se(malloc_usable_size(b) >= (i + 1) * sizeof(int));
+ b[i] = (int) i;
assert_se(GREEDY_REALLOC(b, n_allocated, i / 2));
+ assert_se(n_allocated >= i / 2);
+ assert_se(malloc_usable_size(b) >= (i / 2) * sizeof(int));
}
+
+ for (j = 30; j < i / 2; j += 7)
+ assert_se(b[j] == (int) j);
}
static void test_memdup_multiply_and_greedy_realloc(void) {