|
| 1 | +From: Chris Lamb <lamby@debian.org> |
| 2 | +Date: Mon, 28 Apr 2025 15:45:49 -0300 |
| 3 | +Subject: Add support for USE_SYSTEM_JEMALLOC flag. |
| 4 | + |
| 5 | +Last-Update: 14-04-2026 |
| 6 | +Updated for Valkey 9.1 (deps/Makefile:distclean dropped fast_float_c_interface and gained rm -f .make-*) |
| 7 | +--- |
| 8 | + deps/Makefile | 2 ++ |
| 9 | + src/Makefile | 5 +++++ |
| 10 | + src/debug.c | 4 ++++ |
| 11 | + src/object.c | 4 ++++ |
| 12 | + src/sds.c | 4 ++++ |
| 13 | + src/zmalloc.c | 11 +++++++++++ |
| 14 | + src/zmalloc.h | 4 ++++ |
| 15 | + 7 files changed, 34 insertions(+) |
| 16 | + |
| 17 | +diff --git a/deps/Makefile b/deps/Makefile |
| 18 | +--- a/deps/Makefile |
| 19 | ++++ b/deps/Makefile |
| 20 | +@@ -39,7 +39,9 @@ distclean: |
| 21 | + -(cd libvalkey && $(MAKE) clean) > /dev/null || true |
| 22 | + -(cd linenoise && $(MAKE) clean) > /dev/null || true |
| 23 | + -(cd lua && $(MAKE) clean) > /dev/null || true |
| 24 | ++ifneq ($(USE_SYSTEM_JEMALLOC),yes) |
| 25 | + -(cd jemalloc && [ -f Makefile ] && $(MAKE) distclean) > /dev/null || true |
| 26 | ++endif |
| 27 | + -(cd hdr_histogram && $(MAKE) clean) > /dev/null || true |
| 28 | + -(cd fpconv && $(MAKE) clean) > /dev/null || true |
| 29 | + -(rm -f .make-*) |
| 30 | +diff --git a/src/Makefile b/src/Makefile |
| 31 | +index ac88fc0..8aefc33 100644 |
| 32 | +--- a/src/Makefile |
| 33 | ++++ b/src/Makefile |
| 34 | +@@ -289,10 +289,15 @@ ifeq ($(MALLOC),tcmalloc_minimal) |
| 35 | + endif |
| 36 | + |
| 37 | + ifeq ($(MALLOC),jemalloc) |
| 38 | ++ifeq ($(USE_SYSTEM_JEMALLOC),yes) |
| 39 | ++ FINAL_CFLAGS+= -DUSE_JEMALLOC -DUSE_SYSTEM_JEMALLOC -I/usr/include/jemalloc |
| 40 | ++ FINAL_LIBS := -ljemalloc $(FINAL_LIBS) |
| 41 | ++else |
| 42 | + DEPENDENCY_TARGETS+= jemalloc |
| 43 | + FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include |
| 44 | + FINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS) |
| 45 | + endif |
| 46 | ++endif |
| 47 | + |
| 48 | + ifeq ($(USE_LTTNG),yes) |
| 49 | + FINAL_CFLAGS+=-DUSE_LTTNG |
| 50 | +diff --git a/src/debug.c b/src/debug.c |
| 51 | +index 4a57cd4..88b37af 100644 |
| 52 | +--- a/src/debug.c |
| 53 | ++++ b/src/debug.c |
| 54 | +@@ -82,6 +82,10 @@ void bugReportEnd(int killViaSignal, int sig); |
| 55 | + void logStackTrace(void *eip, int uplevel, int current_thread); |
| 56 | + void sigalrmSignalHandler(int sig, siginfo_t *info, void *secret); |
| 57 | + |
| 58 | ++#if defined(USE_JEMALLOC) && defined(USE_SYSTEM_JEMALLOC) |
| 59 | ++#define je_mallctl mallctl |
| 60 | ++#endif |
| 61 | ++ |
| 62 | + /* ================================= Debugging ============================== */ |
| 63 | + |
| 64 | + /* Compute the sha1 of string at 's' with 'len' bytes long. |
| 65 | +diff --git a/src/object.c b/src/object.c |
| 66 | +index 9ddd412..d60d867 100644 |
| 67 | +--- a/src/object.c |
| 68 | ++++ b/src/object.c |
| 69 | +@@ -45,6 +45,10 @@ |
| 70 | + /* For objects with large embedded keys, we reserve space for an expire field, |
| 71 | + * so if expire is set later, we don't need to reallocate the object. */ |
| 72 | + #define KEY_SIZE_TO_INCLUDE_EXPIRE_THRESHOLD 128 |
| 73 | ++#if defined(USE_JEMALLOC) && defined(USE_SYSTEM_JEMALLOC) |
| 74 | ++#define je_mallctl mallctl |
| 75 | ++#define je_malloc_stats_print malloc_stats_print |
| 76 | ++#endif |
| 77 | + |
| 78 | + /* ===================== Creation and parsing of objects ==================== */ |
| 79 | + |
| 80 | +diff --git a/src/sds.c b/src/sds.c |
| 81 | +index 7843363..ea1551d 100644 |
| 82 | +--- a/src/sds.c |
| 83 | ++++ b/src/sds.c |
| 84 | +@@ -39,6 +39,10 @@ |
| 85 | + #include "sdsalloc.h" |
| 86 | + #include "util.h" |
| 87 | + |
| 88 | ++#ifdef USE_SYSTEM_JEMALLOC |
| 89 | ++#define je_nallocx(size, flags) nallocx(size, flags) |
| 90 | ++#endif |
| 91 | ++ |
| 92 | + const char *SDS_NOINIT = "SDS_NOINIT"; |
| 93 | + |
| 94 | + int sdsHdrSize(char type) { |
| 95 | +diff --git a/src/zmalloc.c b/src/zmalloc.c |
| 96 | +index 24dd11a..5c8e17a 100644 |
| 97 | +--- a/src/zmalloc.c |
| 98 | ++++ b/src/zmalloc.c |
| 99 | +@@ -80,11 +80,22 @@ void zlibc_free(void *ptr) { |
| 100 | + #define free(ptr) tc_free(ptr) |
| 101 | + /* Explicitly override malloc/free etc when using jemalloc. */ |
| 102 | + #elif defined(USE_JEMALLOC) |
| 103 | ++#ifdef USE_SYSTEM_JEMALLOC |
| 104 | ++#define malloc(size) malloc(size) |
| 105 | ++#define calloc(count,size) calloc(count,size) |
| 106 | ++#define realloc(ptr,size) realloc(ptr,size) |
| 107 | ++#define free(ptr) free(ptr) |
| 108 | ++#define mallocx(size,flags) mallocx(size,flags) |
| 109 | ++#define dallocx(ptr,flags) dallocx(ptr,flags) |
| 110 | ++#define je_sdallocx(ptr,size,flags) sdallocx(ptr,size,flags) |
| 111 | ++#define je_mallctl mallctl |
| 112 | ++#else |
| 113 | + #define malloc(size) je_malloc(size) |
| 114 | + #define calloc(count, size) je_calloc(count, size) |
| 115 | + #define realloc(ptr, size) je_realloc(ptr, size) |
| 116 | + #define free(ptr) je_free(ptr) |
| 117 | + #endif |
| 118 | ++#endif |
| 119 | + |
| 120 | + #define thread_local _Thread_local |
| 121 | + |
| 122 | +diff --git a/src/zmalloc.h b/src/zmalloc.h |
| 123 | +index 68b4df6..6bfab3d 100644 |
| 124 | +--- a/src/zmalloc.h |
| 125 | ++++ b/src/zmalloc.h |
| 126 | +@@ -53,7 +53,11 @@ |
| 127 | + #include <jemalloc/jemalloc.h> |
| 128 | + #if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2) |
| 129 | + #define HAVE_MALLOC_SIZE 1 |
| 130 | ++#ifdef USE_SYSTEM_JEMALLOC |
| 131 | ++#define zmalloc_size(p) malloc_usable_size(p) |
| 132 | ++#else |
| 133 | + #define zmalloc_size(p) je_malloc_usable_size(p) |
| 134 | ++#endif |
| 135 | + #else |
| 136 | + #error "Newer version of jemalloc required" |
| 137 | + #endif |
| 138 | + |
0 commit comments