From b84af9b499891f250637eabb0837f3ae31be353e Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 22 Mar 2021 12:24:26 -0400 Subject: [PATCH] add wrapper for madvise --- h_malloc.c | 4 ++-- memory.c | 8 ++++++++ memory.h | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/h_malloc.c b/h_malloc.c index d5daf64..d25c37a 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -1773,7 +1773,7 @@ EXPORT int h_malloc_trim(UNUSED size_t pad) { for (size_t i = 0; i < slab_quarantine_random_length; i++) { void *p = c->quarantine_random[i]; if (p != NULL) { - madvise(p, size, MADV_DONTNEED); + memory_purge(p, size); } } #endif @@ -1783,7 +1783,7 @@ EXPORT int h_malloc_trim(UNUSED size_t pad) { for (size_t i = 0; i < slab_quarantine_queue_length; i++) { void *p = c->quarantine_queue[i]; if (p != NULL) { - madvise(p, size, MADV_DONTNEED); + memory_purge(p, size); } } #endif diff --git a/memory.c b/memory.c index 7c5abb3..523e482 100644 --- a/memory.c +++ b/memory.c @@ -92,6 +92,14 @@ int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size) { } #endif +int memory_purge(void *ptr, size_t size) { + int ret = madvise(ptr, size, MADV_DONTNEED); + if (unlikely(ret) && errno != ENOMEM) { + fatal_error("non-ENOMEM MADV_DONTNEED madvise failure"); + } + return ret; +} + void memory_set_name(UNUSED void *ptr, UNUSED size_t size, UNUSED const char *name) { #ifdef LABEL_MEMORY prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, name); diff --git a/memory.h b/memory.h index fe51546..bd28f1c 100644 --- a/memory.h +++ b/memory.h @@ -19,6 +19,7 @@ int memory_protect_rw_metadata(void *ptr, size_t size); int memory_remap(void *old, size_t old_size, size_t new_size); int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size); #endif +int memory_purge(void *ptr, size_t size); void memory_set_name(void *ptr, size_t size, const char *name); #endif