Skip to content

Commit 3c04a4f

Browse files
committed
Add utils_get_size_threshold() with tests
Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent ec89615 commit 3c04a4f

File tree

5 files changed

+48
-25
lines changed

5 files changed

+48
-25
lines changed

src/proxy_lib/proxy_lib.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -142,31 +142,7 @@ static __TLS int was_called_from_malloc_usable_size = 0;
142142
#ifndef _WIN32
143143
static size_t get_size_threshold(void) {
144144
char *str_threshold = utils_env_var_get_str("UMF_PROXY", "size.threshold=");
145-
if (!str_threshold) {
146-
return 0;
147-
}
148-
149-
// move to the beginning of the number
150-
str_threshold += strlen("size.threshold=");
151-
// find ';' at the end
152-
char *end = strstr(str_threshold, ";");
153-
if (end) {
154-
// replace ';' with '\0' to mark end of the string
155-
*end = '\0';
156-
}
157-
158-
long long_threshold = atol(str_threshold);
159-
if (long_threshold < 0) {
160-
LOG_ERR("size threshold cannot be negative! = (char *) %s, (int) %li",
161-
str_threshold, long_threshold);
162-
return 0;
163-
}
164-
165-
size_t int_threshold = (size_t)long_threshold;
166-
LOG_DEBUG("Size_threshold_value = (char *) %s, (int) %zu", str_threshold,
167-
int_threshold);
168-
169-
return int_threshold;
145+
return utils_get_size_threshold(str_threshold);
170146
}
171147

172148
static int get_system_allocator_symbols(void) {

src/utils/utils_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ int utils_file_open_or_create(const char *path);
168168

169169
int utils_fallocate(int fd, long offset, long len);
170170

171+
size_t utils_get_size_threshold(char *str_threshold);
172+
171173
#ifdef __cplusplus
172174
}
173175
#endif

src/utils/utils_posix_common.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,27 @@ int utils_file_open_or_create(const char *path) {
266266

267267
return fd;
268268
}
269+
270+
// Expected input:
271+
// char *str_threshold = utils_env_var_get_str("UMF_PROXY", "size.threshold=");
272+
size_t utils_get_size_threshold(char *str_threshold) {
273+
if (!str_threshold) {
274+
return 0;
275+
}
276+
277+
// move to the beginning of the number
278+
str_threshold += strlen("size.threshold=");
279+
280+
long long_threshold = atol(str_threshold);
281+
if (long_threshold < 0) {
282+
LOG_ERR("size threshold cannot be negative! = (char *) %s, (int) %li",
283+
str_threshold, long_threshold);
284+
return 0;
285+
}
286+
287+
size_t int_threshold = (size_t)long_threshold;
288+
LOG_DEBUG("Size_threshold_value = (char *) %s, (int) %zu", str_threshold,
289+
int_threshold);
290+
291+
return int_threshold;
292+
}

src/utils/utils_windows_common.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,10 @@ int utils_fallocate(int fd, long offset, long len) {
215215

216216
return -1;
217217
}
218+
219+
// Expected input:
220+
// char *str_threshold = utils_env_var_get_str("UMF_PROXY", "size.threshold=");
221+
size_t utils_get_size_threshold(char *str_threshold) {
222+
(void)str_threshold; // unused
223+
return 0;
224+
}

test/utils/utils_linux.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ TEST_F(test, utils_shm_create_invalid_args) {
5656
ret = utils_shm_create("/abc", -1);
5757
EXPECT_EQ(ret, -1);
5858
}
59+
60+
TEST_F(test, utils_get_size_threshold) {
61+
// Expected input to utils_get_size_threshold():
62+
// char *str_threshold = utils_env_var_get_str("UMF_PROXY", "size.threshold=");
63+
// positive tests
64+
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=111"), 111);
65+
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=222;abcd"), 222);
66+
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=333;var=value"),
67+
333);
68+
// negative tests
69+
EXPECT_EQ(utils_get_size_threshold(NULL), 0);
70+
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=abc"), 0);
71+
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=-1"), 0);
72+
}

0 commit comments

Comments
 (0)