From 511aae45c248f9475591c1361338f5c0e89d3b88 Mon Sep 17 00:00:00 2001 From: KOSAKI Motohiro Date: Sun, 14 Apr 2013 08:55:32 -0700 Subject: [PATCH] add MALLOC_DISCARD_HEAP Signed-off-by: KOSAKI Motohiro --- malloc/arena.c | 17 ++++++++++++++--- malloc/malloc.c | 7 ++++++- malloc/malloc.h | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/malloc/arena.c b/malloc/arena.c index 12a48ad..ae26e0c 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -438,6 +438,15 @@ ptmalloc_init (void) } break; #endif + case 12: + if (! __builtin_expect (__libc_enable_secure, 0)) + { + if (memcmp (envline, "DISCARD_HEAP", 12) == 0) + __libc_mallopt(M_DISCARD_HEAP, atoi(&envline[13])); + } + break; + + case 15: if (! __builtin_expect (__libc_enable_secure, 0)) { @@ -628,9 +637,11 @@ shrink_heap(heap_info *h, long diff) return -2; h->mprotect_size = new_size; } - else - __madvise ((char *)h + new_size, diff, MADV_DONTNEED); - /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/ + else if (__builtin_expect (mp_.discard_heap > 0, 1)) + { + __madvise ((char *)h + new_size, diff, MADV_DONTNEED); + /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/ + } h->size = new_size; return 0; diff --git a/malloc/malloc.c b/malloc/malloc.c index 70b9329..1b2e638 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1730,6 +1730,7 @@ struct malloc_par { INTERNAL_SIZE_T arena_test; INTERNAL_SIZE_T arena_max; #endif + INTERNAL_SIZE_T discard_heap; /* Memory map support */ int n_mmaps; @@ -1773,8 +1774,9 @@ static struct malloc_par mp_ = .trim_threshold = DEFAULT_TRIM_THRESHOLD, #ifdef PER_THREAD # define NARENAS_FROM_NCORES(n) ((n) * (sizeof(long) == 4 ? 2 : 8)) - .arena_test = NARENAS_FROM_NCORES (1) + .arena_test = NARENAS_FROM_NCORES (1), #endif + .discard_heap = 1 }; @@ -4728,6 +4730,9 @@ int __libc_mallopt(int param_number, int value) mp_.arena_max = value; break; #endif + case M_DISCARD_HEAP: + mp_.discard_heap = value; + break; } (void)mutex_unlock(&av->mutex); return res; diff --git a/malloc/malloc.h b/malloc/malloc.h index b8b0ca3..2c98b83 100644 --- a/malloc/malloc.h +++ b/malloc/malloc.h @@ -116,6 +116,7 @@ extern struct mallinfo mallinfo (void) __THROW; #define M_PERTURB -6 #define M_ARENA_TEST -7 #define M_ARENA_MAX -8 +#define M_DISCARD_HEAP -9 /* General SVID/XPG interface to tunable parameters. */ extern int mallopt (int __param, int __val) __THROW; -- 1.7.1