Skip to content

Commit 0485390

Browse files
committed
Add proxy library
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 310dbe1 commit 0485390

File tree

9 files changed

+387
-0
lines changed

9 files changed

+387
-0
lines changed

.github/workflows/codeql.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ jobs:
5353
-DUMF_FORMAT_CODE_STYLE=ON
5454
-DUMF_DEVELOPER_MODE=ON
5555
-DUMF_ENABLE_POOL_TRACKING=ON
56+
-DUMF_BUILD_LIBUMF_POOL_JEMALLOC=ON
5657
-DUMF_BUILD_LIBUMF_POOL_SCALABLE=${{matrix.pool_scalable}}
5758
5859
- name: Build

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,4 @@ install(TARGETS umf
103103
)
104104

105105
add_subdirectory(pool)
106+
add_subdirectory(proxy_lib)

src/proxy_lib/CMakeLists.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (C) 2023-2024 Intel Corporation
2+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
6+
7+
set(PROXY_LIBS umf jemalloc_pool)
8+
9+
set(PROXY_SOURCES
10+
proxy_lib.c
11+
)
12+
13+
set(PROXY_SOURCES_LINUX
14+
proxy_lib_linux.c
15+
)
16+
17+
set(PROXY_SOURCES_WINDOWS
18+
proxy_lib_windows.c
19+
)
20+
21+
set(PROXY_SOURCES_MACOSX
22+
proxy_lib_linux.c
23+
)
24+
25+
if(LINUX)
26+
set(PROXY_SOURCES ${PROXY_SOURCES} ${PROXY_SOURCES_LINUX})
27+
elseif(WINDOWS)
28+
set(PROXY_SOURCES ${PROXY_SOURCES} ${PROXY_SOURCES_WINDOWS})
29+
elseif(MACOSX)
30+
set(PROXY_SOURCES ${PROXY_SOURCES} ${PROXY_SOURCES_MACOSX})
31+
endif()
32+
33+
add_umf_library(NAME umf_proxy
34+
TYPE SHARED
35+
SRCS ${PROXY_SOURCES}
36+
LIBS ${PROXY_LIBS}
37+
LINUX_MAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/proxy_lib.map
38+
WINDOWS_DEF_FILE ${CMAKE_CURRENT_SOURCE_DIR}/proxy_lib.def)
39+
40+
add_library(${PROJECT_NAME}::proxy ALIAS umf_proxy)
41+
42+
target_include_directories(umf_proxy PUBLIC
43+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
44+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
45+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
46+
)
47+
48+
install(TARGETS umf_proxy
49+
EXPORT ${PROJECT_NAME}-targets)

src/proxy_lib/proxy_lib.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <assert.h>
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <string.h>
12+
13+
#include <umf/memory_pool.h>
14+
#include <umf/memory_provider.h>
15+
#include <umf/providers/provider_os_memory.h>
16+
17+
#include "../utils/utils_common.h"
18+
#include "proxy_lib.h"
19+
#include "umf/pools/pool_jemalloc.h"
20+
21+
static umf_memory_provider_handle_t OS_memory_provider = NULL;
22+
static umf_memory_pool_handle_t Proxy_pool = NULL;
23+
24+
// it protects us from recursion in umfPool*()
25+
static __TLS int was_called_from_umfPool = 0;
26+
27+
malloc_func_t System_malloc_func;
28+
calloc_func_t System_calloc_func;
29+
realloc_func_t System_realloc_func;
30+
free_func_t System_free_func;
31+
aligned_alloc_func_t System_aligned_alloc_func;
32+
33+
/******************************************************************************/
34+
35+
void proxy_lib_create_common(void) {
36+
umf_os_memory_provider_params_t os_params =
37+
umfOsMemoryProviderParamsDefault();
38+
enum umf_result_t umf_result;
39+
40+
umf_result = umfMemoryProviderCreate(umfOsMemoryProviderOps(), &os_params,
41+
&OS_memory_provider);
42+
if (umf_result != UMF_RESULT_SUCCESS) {
43+
fprintf(stderr, "error: creating OS memory provider failed\n");
44+
exit(-1);
45+
}
46+
47+
umf_result = umfPoolCreate(umfJemallocPoolOps(), OS_memory_provider, NULL,
48+
0, &Proxy_pool);
49+
if (umf_result != UMF_RESULT_SUCCESS) {
50+
fprintf(stderr, "error: creating jemalloc pool manager failed\n");
51+
exit(-1);
52+
}
53+
}
54+
55+
void proxy_lib_destroy_common(void) {
56+
umf_memory_pool_handle_t pool = Proxy_pool;
57+
Proxy_pool = NULL;
58+
umfPoolDestroy(pool);
59+
umfMemoryProviderDestroy(OS_memory_provider);
60+
}
61+
62+
/******************************************************************************/
63+
64+
static inline void *system_malloc(size_t size) {
65+
if (!System_malloc_func) {
66+
proxy_lib_system_alloc_init();
67+
}
68+
return System_malloc_func(size);
69+
}
70+
71+
static inline void *system_calloc(size_t nmemb, size_t size) {
72+
if (!System_calloc_func) {
73+
proxy_lib_system_alloc_init();
74+
}
75+
return System_calloc_func(nmemb, size);
76+
}
77+
78+
static inline void *system_realloc(void *ptr, size_t size) {
79+
if (!System_realloc_func) {
80+
proxy_lib_system_alloc_init();
81+
}
82+
return System_realloc_func(ptr, size);
83+
}
84+
85+
static inline void system_free(void *ptr) {
86+
if (!System_free_func) {
87+
proxy_lib_system_alloc_init();
88+
}
89+
System_free_func(ptr);
90+
}
91+
92+
static inline void *system_aligned_alloc(size_t alignment, size_t size) {
93+
if (!System_aligned_alloc_func) {
94+
proxy_lib_system_alloc_init();
95+
if (!System_aligned_alloc_func) {
96+
return NULL; // unsupported
97+
}
98+
}
99+
return System_aligned_alloc_func(alignment, size);
100+
}
101+
102+
/******************************************************************************/
103+
104+
WIN32_dllexport void *malloc(size_t size) {
105+
if (!was_called_from_umfPool && Proxy_pool) {
106+
was_called_from_umfPool = 1;
107+
void *ptr = umfPoolMalloc(Proxy_pool, size);
108+
was_called_from_umfPool = 0;
109+
return ptr;
110+
}
111+
112+
return system_malloc(size);
113+
}
114+
115+
WIN32_dllexport void *calloc(size_t nmemb, size_t size) {
116+
if (!was_called_from_umfPool && Proxy_pool) {
117+
was_called_from_umfPool = 1;
118+
void *ptr = umfPoolCalloc(Proxy_pool, nmemb, size);
119+
was_called_from_umfPool = 0;
120+
return ptr;
121+
}
122+
123+
return system_calloc(nmemb, size);
124+
}
125+
126+
WIN32_dllexport void *realloc(void *ptr, size_t size) {
127+
if (!was_called_from_umfPool && Proxy_pool) {
128+
was_called_from_umfPool = 1;
129+
void *new_ptr = umfPoolRealloc(Proxy_pool, ptr, size);
130+
was_called_from_umfPool = 0;
131+
return new_ptr;
132+
}
133+
134+
return system_realloc(ptr, size);
135+
}
136+
137+
WIN32_dllexport void free(void *ptr) {
138+
if (ptr == NULL) {
139+
return;
140+
}
141+
142+
umf_memory_pool_handle_t pool = umfPoolByPtr(ptr);
143+
if (pool == NULL) {
144+
system_free(ptr);
145+
return;
146+
}
147+
148+
enum umf_result_t umf_result = umfPoolFree(pool, ptr);
149+
if (umf_result != UMF_RESULT_SUCCESS) {
150+
fprintf(stderr, "error: umfPoolFree() failed\n");
151+
assert(0);
152+
}
153+
}
154+
155+
WIN32_dllexport void *aligned_alloc(size_t alignment, size_t size) {
156+
if (!was_called_from_umfPool && Proxy_pool) {
157+
was_called_from_umfPool = 1;
158+
void *ptr = umfPoolAlignedMalloc(Proxy_pool, size, alignment);
159+
was_called_from_umfPool = 0;
160+
return ptr;
161+
}
162+
163+
return system_aligned_alloc(alignment, size);
164+
}
165+
166+
WIN32_dllexport size_t malloc_usable_size(void *ptr) {
167+
if (!was_called_from_umfPool && Proxy_pool) {
168+
was_called_from_umfPool = 1;
169+
size_t size = umfPoolMallocUsableSize(Proxy_pool, ptr);
170+
was_called_from_umfPool = 0;
171+
return size;
172+
}
173+
174+
return 0; // unsupported in this case
175+
}

src/proxy_lib/proxy_lib.def

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;;;; Begin Copyright Notice
2+
; Copyright (C) 2024 Intel Corporation
3+
; Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
4+
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
;;;; End Copyright Notice
6+
7+
LIBRARY umf_proxy
8+
EXPORTS
9+
aligned_alloc
10+
calloc
11+
free
12+
malloc
13+
malloc_usable_size
14+
realloc

src/proxy_lib/proxy_lib.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#ifndef UMF_PROXY_LIB_H
9+
#define UMF_PROXY_LIB_H 1
10+
11+
#include <stddef.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
#ifdef _WIN32
18+
#define WIN32_dllexport __declspec(dllexport)
19+
#else
20+
#define WIN32_dllexport
21+
#endif
22+
23+
typedef void *(*malloc_func_t)(size_t size);
24+
typedef void *(*calloc_func_t)(size_t nmemb, size_t size);
25+
typedef void *(*realloc_func_t)(void *ptr, size_t size);
26+
typedef void (*free_func_t)(void *ptr);
27+
typedef void *(*aligned_alloc_func_t)(size_t alignment, size_t size);
28+
29+
extern malloc_func_t System_malloc_func;
30+
extern calloc_func_t System_calloc_func;
31+
extern realloc_func_t System_realloc_func;
32+
extern free_func_t System_free_func;
33+
extern aligned_alloc_func_t System_aligned_alloc_func;
34+
35+
void proxy_lib_create_common(void);
36+
void proxy_lib_destroy_common(void);
37+
void proxy_lib_system_alloc_init(void);
38+
39+
#ifdef __cplusplus
40+
}
41+
#endif
42+
43+
#endif /* UMF_PROXY_LIB_H */

src/proxy_lib/proxy_lib.map

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
# linker VERSION script
6+
7+
{
8+
global:
9+
aligned_alloc;
10+
calloc;
11+
free;
12+
malloc;
13+
malloc_usable_size;
14+
realloc;
15+
local:
16+
*;
17+
};

src/proxy_lib/proxy_lib_linux.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#define _GNU_SOURCE
11+
#include <dlfcn.h>
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
16+
#include "proxy_lib.h"
17+
18+
void __attribute__((constructor(101))) proxy_lib_create(void) {
19+
proxy_lib_create_common();
20+
}
21+
22+
void __attribute__((destructor(101))) proxy_lib_destroy(void) {
23+
proxy_lib_destroy_common();
24+
}
25+
26+
void proxy_lib_system_alloc_init(void) {
27+
// ISO C forbids conversion of object pointer to function pointer type [-Werror=pedantic]
28+
*((void **)(&System_malloc_func)) = dlsym(RTLD_NEXT, "malloc");
29+
*((void **)(&System_calloc_func)) = dlsym(RTLD_NEXT, "calloc");
30+
*((void **)(&System_realloc_func)) = dlsym(RTLD_NEXT, "realloc");
31+
*((void **)(&System_free_func)) = dlsym(RTLD_NEXT, "free");
32+
*((void **)(&System_aligned_alloc_func)) =
33+
dlsym(RTLD_NEXT, "aligned_alloc");
34+
35+
// System_aligned_alloc_func can be NULL
36+
if (!System_malloc_func || !System_calloc_func || !System_realloc_func ||
37+
!System_free_func) {
38+
fprintf(stderr, "error: cannot find libc alloc functions\n");
39+
exit(-1);
40+
}
41+
}

0 commit comments

Comments
 (0)