Skip to content

Commit

Permalink
Use a static inline function instead of a macro.
Browse files Browse the repository at this point in the history
Signed-off-by: Lipeng Zhu <[email protected]>
  • Loading branch information
lipzhu committed Jun 13, 2024
1 parent 8c18b84 commit 15c79e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
24 changes: 11 additions & 13 deletions src/zmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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). */
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/zmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 15c79e4

Please sign in to comment.