From: Chris Lamb <lamby@debian.org>
Date: Fri, 9 May 2025 14:53:42 -0700
Subject: Add support for USE_SYSTEM_JEMALLOC flag.

Forwarded: https://github.com/antirez/redis/pull/5279
---
 deps/Makefile |  2 ++
 src/Makefile  |  5 +++++
 src/zmalloc.c |  2 ++
 src/zmalloc.h | 13 +++++++++++++
 4 files changed, 22 insertions(+)

diff --git a/deps/Makefile b/deps/Makefile
index 5593e0c..e4c2ed4 100644
--- a/deps/Makefile
+++ b/deps/Makefile
@@ -39,7 +39,9 @@ distclean:
 	-(cd hiredis && $(MAKE) clean) > /dev/null || true
 	-(cd linenoise && $(MAKE) clean) > /dev/null || true
 	-(cd lua && $(MAKE) clean) > /dev/null || true
+ifneq ($(USE_SYSTEM_JEMALLOC),yes)
 	-(cd jemalloc && [ -f Makefile ] && $(MAKE) distclean) > /dev/null || true
+endif
 	-(cd hdr_histogram && $(MAKE) clean) > /dev/null || true
 	-(cd fpconv && $(MAKE) clean) > /dev/null || true
 	-(cd fast_float && $(MAKE) clean) > /dev/null || true
diff --git a/src/Makefile b/src/Makefile
index 179a9c4..b19b693 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -278,10 +278,15 @@ ifeq ($(MALLOC),tcmalloc_minimal)
 endif
 
 ifeq ($(MALLOC),jemalloc)
+ifeq ($(USE_SYSTEM_JEMALLOC),yes)
+	FINAL_CFLAGS+= -DUSE_JEMALLOC -DUSE_SYSTEM_JEMALLOC -I/usr/include/jemalloc/include
+	FINAL_LIBS := -ljemalloc $(FINAL_LIBS)
+else
 	DEPENDENCY_TARGETS+= jemalloc
 	FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include
 	FINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS)
 endif
+endif
 
 # LIBSSL & LIBCRYPTO
 LIBSSL_LIBS=
diff --git a/src/zmalloc.c b/src/zmalloc.c
index b2f5718..d3a623b 100644
--- a/src/zmalloc.c
+++ b/src/zmalloc.c
@@ -60,6 +60,7 @@ void zlibc_free(void *ptr) {
 #define free(ptr) tc_free(ptr)
 /* Explicitly override malloc/free etc when using jemalloc. */
 #elif defined(USE_JEMALLOC)
+#if !defined(USE_SYSTEM_JEMALLOC)
 #define malloc(size) je_malloc(size)
 #define calloc(count,size) je_calloc(count,size)
 #define realloc(ptr,size) je_realloc(ptr,size)
@@ -68,6 +69,7 @@ void zlibc_free(void *ptr) {
 #define rallocx(ptr,size,flags) je_rallocx(ptr,size,flags)
 #define dallocx(ptr,flags) je_dallocx(ptr,flags)
 #endif
+#endif
 
 #define MAX_THREADS 16 /* Keep it a power of 2 so we can use '&' instead of '%'. */
 #define THREAD_MASK (MAX_THREADS - 1)
diff --git a/src/zmalloc.h b/src/zmalloc.h
index bbb74a0..853ce3b 100644
--- a/src/zmalloc.h
+++ b/src/zmalloc.h
@@ -30,7 +30,20 @@
 #include <jemalloc/jemalloc.h>
 #if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2)
 #define HAVE_MALLOC_SIZE 1
+#if defined(USE_SYSTEM_JEMALLOC)
+#define zmalloc_size(p) malloc_usable_size(p)
+#define malloc(size) malloc(size)
+#define calloc(count,size) calloc(count,size)
+#define realloc(ptr,size) realloc(ptr,size)
+#define free(ptr) free(ptr)
+#define mallocx(size,flags) mallocx(size,flags)
+#define dallocx(ptr,flags) dallocx(ptr,flags)
+#define je_nallocx(ptr,flags) nallocx(ptr,flags)
+#define je_mallctl mallctl
+#define je_malloc_stats_print(a,b,c) malloc_stats_print(a,b,c)
+#else
 #define zmalloc_size(p) je_malloc_usable_size(p)
+#endif
 #else
 #error "Newer version of jemalloc required"
 #endif
