From 15c79e437d6066973b2e85171a3de50ce2725492 Mon Sep 17 00:00:00 2001 From: Lipeng Zhu Date: Thu, 13 Jun 2024 02:55:07 -0400 Subject: [PATCH] Use a static inline function instead of a macro. Signed-off-by: Lipeng Zhu --- src/zmalloc.c | 24 +++++++++++------------- src/zmalloc.h | 1 - 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/zmalloc.c b/src/zmalloc.c index e740a41677..cd1ec97516 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -88,18 +88,6 @@ void zlibc_free(void *ptr) { #define dallocx(ptr, flags) je_dallocx(ptr, flags) #endif -#define update_zmalloc_stat_alloc(__n) \ - do { \ - if (unlikely(thread_index == -1)) zmalloc_register_thread_index(); \ - used_memory_thread[thread_index] += (__n); \ - } while (0) - -#define update_zmalloc_stat_free(__n) \ - do { \ - if (unlikely(thread_index == -1)) zmalloc_register_thread_index(); \ - used_memory_thread[thread_index] -= (__n); \ - } while (0) - /* A thread-local storage which keep the current thread's index in the used_memory_thread array. */ static __thread int thread_index = -1; /* MAX_THREADS_NUM = IO_THREADS_MAX_NUM(128) + BIO threads(3) + main thread(1). */ @@ -109,11 +97,21 @@ static unsigned long long used_memory_thread[MAX_THREADS_NUM]; static atomic_int total_active_threads; /* Register the thread index in start_routine. */ -void zmalloc_register_thread_index(void) { +static inline void zmalloc_register_thread_index(void) { thread_index = atomic_fetch_add_explicit(&total_active_threads, 1, memory_order_relaxed); assert(total_active_threads < MAX_THREADS_NUM); } +static inline void update_zmalloc_stat_alloc(size_t size) { + if (unlikely(thread_index == -1)) zmalloc_register_thread_index(); + used_memory_thread[thread_index] += size; +} + +static inline void update_zmalloc_stat_free(size_t size) { + if (unlikely(thread_index == -1)) zmalloc_register_thread_index(); + used_memory_thread[thread_index] -= size; +} + static void zmalloc_default_oom(size_t size) { fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n", size); fflush(stderr); diff --git a/src/zmalloc.h b/src/zmalloc.h index a068c671c3..a909366c13 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -142,7 +142,6 @@ size_t zmalloc_get_memory_size(void); void zlibc_free(void *ptr); void zlibc_trim(void); void zmadvise_dontneed(void *ptr); -void zmalloc_register_thread_index(void); #ifdef HAVE_DEFRAG void zfree_no_tcache(void *ptr);