diff --git a/malloc/malloc.c b/malloc/malloc.c index 2c56c1f124..8dd7bbe4f4 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -2053,10 +2053,17 @@ free_perturb (char *p, size_t n) /* ----------- Routines dealing with transparent huge pages ----------- */ +static void thp_init (void); + static inline void madvise_thp (void *p, INTERNAL_SIZE_T size) { #ifdef MADV_HUGEPAGE + + /* Ensure thp_init () is invoked only once */ + if (mp_.thp_pagesize < DEFAULT_THP_PAGESIZE) + thp_init (); + /* Only use __madvise if the system is using 'madvise' mode. Otherwise the call is wasteful. */ if (mp_.thp_mode != malloc_thp_mode_madvise) @@ -2664,6 +2671,10 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av) previous calls. Otherwise, we correct to page-align below. */ + /* Ensure thp_init () is invoked only once */ + if (mp_.thp_pagesize < DEFAULT_THP_PAGESIZE) + thp_init (); + if (__glibc_unlikely (mp_.thp_pagesize != 0)) { uintptr_t lastbrk = (uintptr_t) MORECORE (0); @@ -5660,6 +5671,15 @@ do_set_hugetlb (size_t value) return 0; } +static __always_inline void +thp_init (void) +{ + /* thp_pagesize is set even if thp_mode is never. This reduces frequency + of MORECORE () invocation. */ + mp_.thp_pagesize = DEFAULT_THP_PAGESIZE; + mp_.thp_mode = __malloc_thp_mode (); +} + int __libc_mallopt (int param_number, int value) {