Skip to content

Commit d2f2c80

Browse files
committed
Make size threshold Windows only
Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent 1abc22e commit d2f2c80

File tree

1 file changed

+25
-44
lines changed

1 file changed

+25
-44
lines changed

src/proxy_lib/proxy_lib.c

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void utils_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
101101
* of a UMF pool to allocate memory needed by an application. It should be freed
102102
* by an application.
103103
*/
104-
104+
#ifndef _WIN32
105105
typedef void *(*system_aligned_alloc_t)(size_t alignment, size_t size);
106106
typedef void *(*system_calloc_t)(size_t nmemb, size_t size);
107107
typedef void (*system_free_t)(void *ptr);
@@ -110,7 +110,6 @@ typedef size_t (*system_malloc_usable_size_t)(void *ptr);
110110
typedef void *(*system_realloc_t)(void *ptr, size_t size);
111111

112112
// pointers to the default system allocator's API
113-
static void *System_library_handle;
114113
static system_aligned_alloc_t System_aligned_alloc;
115114
static system_calloc_t System_calloc;
116115
static system_free_t System_free;
@@ -119,6 +118,7 @@ static system_malloc_usable_size_t System_malloc_usable_size;
119118
static system_realloc_t System_realloc;
120119

121120
static size_t Size_threshold_value = 0;
121+
#endif /* _WIN32 */
122122

123123
static UTIL_ONCE_FLAG Base_alloc_leak_initialized = UTIL_ONCE_FLAG_INIT;
124124
static umf_ba_linear_pool_t *Base_alloc_leak = NULL;
@@ -132,24 +132,7 @@ static __TLS int was_called_from_umfPool = 0;
132132
/*** The constructor and destructor of the proxy library *********************/
133133
/*****************************************************************************/
134134

135-
// atoi() is defined in stdlib.h, but we cannot include it on Windows
136-
static size_t custom_atoi(const char *str) {
137-
size_t result = 0;
138-
139-
for (int i = 0; str[i]; i++) {
140-
if (str[i] < '0' || str[i] > '9') {
141-
LOG_ERR("proxy_lib_create_common: size threshold is not a valid "
142-
"number: %s",
143-
str);
144-
return 0;
145-
}
146-
147-
result = 10 * result + (size_t)(str[i] - '0');
148-
}
149-
150-
return result;
151-
}
152-
135+
#ifndef _WIN32
153136
static size_t get_size_threshold(void) {
154137
char *str_threshold = utils_env_var_get_str("UMF_PROXY", "size.threshold=");
155138
if (!str_threshold) {
@@ -165,32 +148,25 @@ static size_t get_size_threshold(void) {
165148
*end = '\0';
166149
}
167150

168-
size_t int_threshold = (size_t)custom_atoi(str_threshold);
151+
size_t int_threshold = (size_t)atoi(str_threshold);
169152
LOG_DEBUG("Size_threshold_value = (char *) %s, (int) %zu", str_threshold,
170153
int_threshold);
171154

172155
return int_threshold;
173156
}
174157

175158
static int get_system_allocator_symbols(void) {
176-
#ifdef _WIN32
177-
System_library_handle = utils_open_library("msvcrt.dll", 0);
178-
#else
179-
System_library_handle = RTLD_NEXT;
180-
#endif /* _WIN32 */
181-
182159
*((void **)(&System_aligned_alloc)) =
183-
utils_get_symbol_addr(System_library_handle, "aligned_alloc", NULL);
160+
utils_get_symbol_addr(RTLD_NEXT, "aligned_alloc", NULL);
184161
*((void **)(&System_calloc)) =
185-
utils_get_symbol_addr(System_library_handle, "calloc", NULL);
186-
*((void **)(&System_free)) =
187-
utils_get_symbol_addr(System_library_handle, "free", NULL);
162+
utils_get_symbol_addr(RTLD_NEXT, "calloc", NULL);
163+
*((void **)(&System_free)) = utils_get_symbol_addr(RTLD_NEXT, "free", NULL);
188164
*((void **)(&System_malloc)) =
189-
utils_get_symbol_addr(System_library_handle, "malloc", NULL);
190-
*((void **)(&System_malloc_usable_size)) = utils_get_symbol_addr(
191-
System_library_handle, "malloc_usable_size", NULL);
165+
utils_get_symbol_addr(RTLD_NEXT, "malloc", NULL);
166+
*((void **)(&System_malloc_usable_size)) =
167+
utils_get_symbol_addr(RTLD_NEXT, "malloc_usable_size", NULL);
192168
*((void **)(&System_realloc)) =
193-
utils_get_symbol_addr(System_library_handle, "realloc", NULL);
169+
utils_get_symbol_addr(RTLD_NEXT, "realloc", NULL);
194170

195171
if (System_aligned_alloc && System_calloc && System_free && System_malloc &&
196172
System_malloc_usable_size && System_realloc) {
@@ -206,13 +182,15 @@ static int get_system_allocator_symbols(void) {
206182

207183
return -1;
208184
}
185+
#endif /* _WIN32 */
209186

210187
void proxy_lib_create_common(void) {
211188
utils_log_init();
212189
umf_os_memory_provider_params_t os_params =
213190
umfOsMemoryProviderParamsDefault();
214191
umf_result_t umf_result;
215192

193+
#ifndef _WIN32
216194
size_t _threshold = get_size_threshold();
217195
if (_threshold > 0) {
218196
if (get_system_allocator_symbols()) {
@@ -225,7 +203,6 @@ void proxy_lib_create_common(void) {
225203
Size_threshold_value);
226204
}
227205

228-
#ifndef _WIN32
229206
if (utils_env_var_has_str("UMF_PROXY", "page.disposition=shared-fd")) {
230207
LOG_INFO("proxy_lib: using the MAP_SHARED visibility mode with the "
231208
"file descriptor duplication");
@@ -281,14 +258,6 @@ void proxy_lib_destroy_common(void) {
281258
umf_memory_provider_handle_t provider = OS_memory_provider;
282259
OS_memory_provider = NULL;
283260
umfMemoryProviderDestroy(provider);
284-
285-
#ifdef _WIN32
286-
if (System_library_handle) {
287-
utils_close_library(System_library_handle);
288-
System_library_handle = NULL;
289-
}
290-
#endif /* _WIN32 */
291-
292261
LOG_DEBUG("proxy library destroyed");
293262

294263
fini_proxy_lib_destroy_common:
@@ -371,9 +340,11 @@ static inline size_t ba_leak_pool_contains_pointer(void *ptr) {
371340
/*****************************************************************************/
372341

373342
void *malloc(size_t size) {
343+
#ifndef _WIN32
374344
if (size < Size_threshold_value) {
375345
return System_malloc(size);
376346
}
347+
#endif /* _WIN32 */
377348

378349
if (!was_called_from_umfPool && Proxy_pool) {
379350
was_called_from_umfPool = 1;
@@ -386,9 +357,11 @@ void *malloc(size_t size) {
386357
}
387358

388359
void *calloc(size_t nmemb, size_t size) {
360+
#ifndef _WIN32
389361
if ((nmemb * size) < Size_threshold_value) {
390362
return System_calloc(nmemb, size);
391363
}
364+
#endif /* _WIN32 */
392365

393366
if (!was_called_from_umfPool && Proxy_pool) {
394367
was_called_from_umfPool = 1;
@@ -416,10 +389,12 @@ void free(void *ptr) {
416389
return;
417390
}
418391

392+
#ifndef _WIN32
419393
if (Size_threshold_value) {
420394
System_free(ptr);
421395
return;
422396
}
397+
#endif /* _WIN32 */
423398

424399
LOG_ERR("free() failed: %p", ptr);
425400

@@ -448,19 +423,23 @@ void *realloc(void *ptr, size_t size) {
448423
return new_ptr;
449424
}
450425

426+
#ifndef _WIN32
451427
if (Size_threshold_value) {
452428
return System_realloc(ptr, size);
453429
}
430+
#endif /* _WIN32 */
454431

455432
LOG_ERR("realloc() failed: %p", ptr);
456433

457434
return NULL;
458435
}
459436

460437
void *aligned_alloc(size_t alignment, size_t size) {
438+
#ifndef _WIN32
461439
if (size < Size_threshold_value) {
462440
return System_aligned_alloc(alignment, size);
463441
}
442+
#endif /* _WIN32 */
464443

465444
if (!was_called_from_umfPool && Proxy_pool) {
466445
was_called_from_umfPool = 1;
@@ -493,9 +472,11 @@ size_t malloc_usable_size(void *ptr) {
493472
return size;
494473
}
495474

475+
#ifndef _WIN32
496476
if (Size_threshold_value) {
497477
return System_malloc_usable_size(ptr);
498478
}
479+
#endif /* _WIN32 */
499480

500481
LOG_ERR("malloc_usable_size() failed: %p", ptr);
501482

0 commit comments

Comments
 (0)