Skip to content

Commit 07e7b66

Browse files
committed
[CMake] Expand support for preinstalled jemalloc
Signed-off-by: Sarnie, Nick <[email protected]>
1 parent c765222 commit 07e7b66

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

CMakeLists.txt

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -155,36 +155,37 @@ if(NOT UMF_BUILD_LIBUMF_POOL_JEMALLOC)
155155
set(UMF_POOL_JEMALLOC_ENABLED FALSE)
156156
set(JEMALLOC_FOUND FALSE)
157157
set(JEMALLOC_LIBRARIES FALSE)
158-
elseif(WINDOWS)
159-
pkg_check_modules(JEMALLOC jemalloc)
158+
else()
159+
set(JEMALLOC_VER 5.3.0)
160+
pkg_check_modules(JEMALLOC QUIET jemalloc)
160161
if(NOT JEMALLOC_FOUND)
161-
find_package(JEMALLOC REQUIRED jemalloc)
162+
find_package(JEMALLOC ${JEMALLOC_VER} QUIET)
162163
endif()
163-
else()
164-
if(NOT DEFINED UMF_JEMALLOC_REPO)
164+
if(NOT JEMALLOC_FOUND)
165+
if(NOT DEFINED UMF_JEMALLOC_REPO)
165166
set(UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git")
166-
endif()
167+
endif()
167168

168-
if(NOT DEFINED UMF_JEMALLOC_TAG)
169-
set(UMF_JEMALLOC_TAG 5.3.0)
170-
endif()
169+
if(NOT DEFINED UMF_JEMALLOC_TAG)
170+
set(UMF_JEMALLOC_TAG ${JEMALLOC_VER})
171+
endif()
171172

172-
message(
173+
message(
173174
STATUS
174-
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
175-
)
175+
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
176+
)
176177

177-
FetchContent_Declare(
178+
FetchContent_Declare(
178179
jemalloc_targ
179180
GIT_REPOSITORY ${UMF_JEMALLOC_REPO}
180181
GIT_TAG ${UMF_JEMALLOC_TAG})
181-
FetchContent_MakeAvailable(jemalloc_targ)
182+
FetchContent_MakeAvailable(jemalloc_targ)
182183

183-
add_custom_command(
184+
add_custom_command(
184185
COMMAND ./autogen.sh
185186
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
186187
OUTPUT ${jemalloc_targ_SOURCE_DIR}/configure)
187-
add_custom_command(
188+
add_custom_command(
188189
# Custom jemalloc build. Non-default options used:
189190
# --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs
190191
# --disable-cxx - Disable C++ integration. This will cause new and
@@ -195,39 +196,40 @@ else()
195196
# loaded after program startup (e.g. using dlopen). --disable-doc -
196197
# Disable building and installing the documentation.
197198
COMMAND
198-
./configure --prefix=${jemalloc_targ_BINARY_DIR}
199-
--with-jemalloc-prefix=je_ --disable-cxx --disable-initial-exec-tls
200-
--disable-doc CFLAGS=-fPIC
199+
./configure --prefix=${jemalloc_targ_BINARY_DIR}
200+
--with-jemalloc-prefix=je_ --disable-cxx --disable-initial-exec-tls
201+
--disable-doc CFLAGS=-fPIC
201202
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
202203
OUTPUT ${jemalloc_targ_SOURCE_DIR}/Makefile
203204
DEPENDS ${jemalloc_targ_SOURCE_DIR}/configure)
204205

205-
if(NOT UMF_QEMU_BUILD)
206+
if(NOT UMF_QEMU_BUILD)
206207
set(MAKE_ARGUMENTS "-j$(nproc)")
207-
endif()
208+
endif()
208209

209-
add_custom_command(
210+
add_custom_command(
210211
COMMAND make ${MAKE_ARGUMENTS}
211212
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
212213
OUTPUT ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a
213214
DEPENDS ${jemalloc_targ_SOURCE_DIR}/Makefile)
214-
add_custom_command(
215+
add_custom_command(
215216
COMMAND make install
216217
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
217218
OUTPUT ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a
218219
DEPENDS ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a)
219220

220-
add_custom_target(jemalloc_prod
221-
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
222-
add_library(jemalloc INTERFACE)
223-
target_link_libraries(
221+
add_custom_target(jemalloc_prod
222+
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
223+
add_library(jemalloc INTERFACE)
224+
target_link_libraries(
224225
jemalloc INTERFACE ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
225-
add_dependencies(jemalloc jemalloc_prod)
226+
add_dependencies(jemalloc jemalloc_prod)
226227

227-
set(JEMALLOC_FOUND TRUE)
228-
set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
229-
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
230-
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
228+
set(JEMALLOC_FOUND TRUE)
229+
set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
230+
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
231+
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
232+
endif()
231233
endif()
232234

233235
if(JEMALLOC_FOUND)

src/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ if(UMF_POOL_JEMALLOC_ENABLED)
7575
set(UMF_PRIVATE_INCLUDE_DIRS ${UMF_PRIVATE_INCLUDE_DIRS}
7676
${JEMALLOC_INCLUDE_DIRS})
7777
set(UMF_COMMON_COMPILE_DEFINITIONS ${UMF_COMMON_COMPILE_DEFINITIONS}
78-
"UMF_POOL_JEMALLOC_ENABLED=1")
78+
"UMF_POOL_JEMALLOC_ENABLED=1"
79+
"USING_PREBUILT_JEMALLOC=${JEMALLOC_FOUND}")
7980
endif()
8081

8182
set(UMF_PRIVATE_LIBRARY_DIRS ${UMF_PRIVATE_LIBRARY_DIRS}
@@ -166,7 +167,7 @@ if(UMF_LINK_HWLOC_STATICALLY)
166167
endif()
167168
endif()
168169

169-
if(NOT WINDOWS AND UMF_POOL_JEMALLOC_ENABLED)
170+
if(UMF_POOL_JEMALLOC_ENABLED AND TARGET jemalloc)
170171
add_dependencies(umf jemalloc)
171172
endif()
172173

src/pool/pool_jemalloc.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ umfJemallocPoolParamsSetName(umf_jemalloc_pool_params_handle_t hParams,
5959
#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)
6060
#define DEFAULT_NAME "jemalloc"
6161

62+
#ifdef USING_PREBUILT_JEMALLOC
63+
64+
// When jemalloc is built in-tree, it is configured such that
65+
// all symbols are prepended with the je_ prefix. This is not
66+
// the case for system-installed jemalloc.
67+
68+
#define je_mallctl(...) mallctl(__VA_ARGS__)
69+
70+
#define je_mallocx(...) mallocx(__VA_ARGS__)
71+
72+
#define je_dallocx(...) dallocx(__VA_ARGS__)
73+
74+
#define je_rallocx(...) rallocx(__VA_ARGS__)
75+
76+
#define je_malloc_usable_size(...) malloc_usable_size(__VA_ARGS__)
77+
78+
#endif
79+
6280
typedef struct umf_jemalloc_pool_params_t {
6381
size_t n_arenas;
6482
char name[64];
@@ -678,4 +696,13 @@ umfJemallocPoolParamsSetName(umf_jemalloc_pool_params_handle_t hParams,
678696
return UMF_RESULT_SUCCESS;
679697
}
680698

699+
700+
#ifdef USING_PREBUILT_JEMALLOC
701+
#undef je_mallctl
702+
#undef je_mallocx
703+
#undef je_dallocx
704+
#undef je_rallocx
705+
#undef je_malloc_usable_size
706+
#endif
707+
681708
#endif /* UMF_POOL_JEMALLOC_ENABLED */

0 commit comments

Comments
 (0)