@@ -101,7 +101,7 @@ void utils_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
101
101
* of a UMF pool to allocate memory needed by an application. It should be freed
102
102
* by an application.
103
103
*/
104
-
104
+ #ifndef _WIN32
105
105
typedef void * (* system_aligned_alloc_t )(size_t alignment , size_t size );
106
106
typedef void * (* system_calloc_t )(size_t nmemb , size_t size );
107
107
typedef void (* system_free_t )(void * ptr );
@@ -110,7 +110,6 @@ typedef size_t (*system_malloc_usable_size_t)(void *ptr);
110
110
typedef void * (* system_realloc_t )(void * ptr , size_t size );
111
111
112
112
// pointers to the default system allocator's API
113
- static void * System_library_handle ;
114
113
static system_aligned_alloc_t System_aligned_alloc ;
115
114
static system_calloc_t System_calloc ;
116
115
static system_free_t System_free ;
@@ -119,6 +118,7 @@ static system_malloc_usable_size_t System_malloc_usable_size;
119
118
static system_realloc_t System_realloc ;
120
119
121
120
static size_t Size_threshold_value = 0 ;
121
+ #endif /* _WIN32 */
122
122
123
123
static UTIL_ONCE_FLAG Base_alloc_leak_initialized = UTIL_ONCE_FLAG_INIT ;
124
124
static umf_ba_linear_pool_t * Base_alloc_leak = NULL ;
@@ -132,24 +132,7 @@ static __TLS int was_called_from_umfPool = 0;
132
132
/*** The constructor and destructor of the proxy library *********************/
133
133
/*****************************************************************************/
134
134
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
153
136
static size_t get_size_threshold (void ) {
154
137
char * str_threshold = utils_env_var_get_str ("UMF_PROXY" , "size.threshold=" );
155
138
if (!str_threshold ) {
@@ -165,32 +148,25 @@ static size_t get_size_threshold(void) {
165
148
* end = '\0' ;
166
149
}
167
150
168
- size_t int_threshold = (size_t )custom_atoi (str_threshold );
151
+ size_t int_threshold = (size_t )atoi (str_threshold );
169
152
LOG_DEBUG ("Size_threshold_value = (char *) %s, (int) %zu" , str_threshold ,
170
153
int_threshold );
171
154
172
155
return int_threshold ;
173
156
}
174
157
175
158
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
-
182
159
* ((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 );
184
161
* ((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 );
188
164
* ((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 );
192
168
* ((void * * )(& System_realloc )) =
193
- utils_get_symbol_addr (System_library_handle , "realloc" , NULL );
169
+ utils_get_symbol_addr (RTLD_NEXT , "realloc" , NULL );
194
170
195
171
if (System_aligned_alloc && System_calloc && System_free && System_malloc &&
196
172
System_malloc_usable_size && System_realloc ) {
@@ -206,13 +182,15 @@ static int get_system_allocator_symbols(void) {
206
182
207
183
return -1 ;
208
184
}
185
+ #endif /* _WIN32 */
209
186
210
187
void proxy_lib_create_common (void ) {
211
188
utils_log_init ();
212
189
umf_os_memory_provider_params_t os_params =
213
190
umfOsMemoryProviderParamsDefault ();
214
191
umf_result_t umf_result ;
215
192
193
+ #ifndef _WIN32
216
194
size_t _threshold = get_size_threshold ();
217
195
if (_threshold > 0 ) {
218
196
if (get_system_allocator_symbols ()) {
@@ -225,7 +203,6 @@ void proxy_lib_create_common(void) {
225
203
Size_threshold_value );
226
204
}
227
205
228
- #ifndef _WIN32
229
206
if (utils_env_var_has_str ("UMF_PROXY" , "page.disposition=shared-fd" )) {
230
207
LOG_INFO ("proxy_lib: using the MAP_SHARED visibility mode with the "
231
208
"file descriptor duplication" );
@@ -281,14 +258,6 @@ void proxy_lib_destroy_common(void) {
281
258
umf_memory_provider_handle_t provider = OS_memory_provider ;
282
259
OS_memory_provider = NULL ;
283
260
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
-
292
261
LOG_DEBUG ("proxy library destroyed" );
293
262
294
263
fini_proxy_lib_destroy_common :
@@ -371,9 +340,11 @@ static inline size_t ba_leak_pool_contains_pointer(void *ptr) {
371
340
/*****************************************************************************/
372
341
373
342
void * malloc (size_t size ) {
343
+ #ifndef _WIN32
374
344
if (size < Size_threshold_value ) {
375
345
return System_malloc (size );
376
346
}
347
+ #endif /* _WIN32 */
377
348
378
349
if (!was_called_from_umfPool && Proxy_pool ) {
379
350
was_called_from_umfPool = 1 ;
@@ -386,9 +357,11 @@ void *malloc(size_t size) {
386
357
}
387
358
388
359
void * calloc (size_t nmemb , size_t size ) {
360
+ #ifndef _WIN32
389
361
if ((nmemb * size ) < Size_threshold_value ) {
390
362
return System_calloc (nmemb , size );
391
363
}
364
+ #endif /* _WIN32 */
392
365
393
366
if (!was_called_from_umfPool && Proxy_pool ) {
394
367
was_called_from_umfPool = 1 ;
@@ -416,10 +389,12 @@ void free(void *ptr) {
416
389
return ;
417
390
}
418
391
392
+ #ifndef _WIN32
419
393
if (Size_threshold_value ) {
420
394
System_free (ptr );
421
395
return ;
422
396
}
397
+ #endif /* _WIN32 */
423
398
424
399
LOG_ERR ("free() failed: %p" , ptr );
425
400
@@ -448,19 +423,23 @@ void *realloc(void *ptr, size_t size) {
448
423
return new_ptr ;
449
424
}
450
425
426
+ #ifndef _WIN32
451
427
if (Size_threshold_value ) {
452
428
return System_realloc (ptr , size );
453
429
}
430
+ #endif /* _WIN32 */
454
431
455
432
LOG_ERR ("realloc() failed: %p" , ptr );
456
433
457
434
return NULL ;
458
435
}
459
436
460
437
void * aligned_alloc (size_t alignment , size_t size ) {
438
+ #ifndef _WIN32
461
439
if (size < Size_threshold_value ) {
462
440
return System_aligned_alloc (alignment , size );
463
441
}
442
+ #endif /* _WIN32 */
464
443
465
444
if (!was_called_from_umfPool && Proxy_pool ) {
466
445
was_called_from_umfPool = 1 ;
@@ -493,9 +472,11 @@ size_t malloc_usable_size(void *ptr) {
493
472
return size ;
494
473
}
495
474
475
+ #ifndef _WIN32
496
476
if (Size_threshold_value ) {
497
477
return System_malloc_usable_size (ptr );
498
478
}
479
+ #endif /* _WIN32 */
499
480
500
481
LOG_ERR ("malloc_usable_size() failed: %p" , ptr );
501
482
0 commit comments