forked from oneapi-src/unified-memory-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_common.c
More file actions
140 lines (114 loc) · 3.73 KB
/
Copy pathutils_common.c
File metadata and controls
140 lines (114 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
#include <string.h>
#include "utils_assert.h"
#include "utils_common.h"
// align a pointer up and a size down
void utils_align_ptr_up_size_down(void **ptr, size_t *size, size_t alignment) {
uintptr_t p = (uintptr_t)*ptr;
size_t s = *size;
// align the pointer up to 'alignment' bytes and adjust the size down
size_t rest = p & (alignment - 1);
if (rest) {
p = ALIGN_UP(p, alignment);
s -= alignment - rest;
}
ASSERT(IS_ALIGNED(p, alignment));
ASSERT(IS_ALIGNED(s, alignment));
*ptr = (void *)p;
*size = s;
}
// align a pointer down and a size up (for mmap()/munmap())
void utils_align_ptr_down_size_up(void **ptr, size_t *size, size_t alignment) {
uintptr_t p = (uintptr_t)*ptr;
size_t s = *size;
// align the pointer down to 'alignment' bytes and adjust the size up
size_t rest = p & (alignment - 1);
if (rest) {
p = ALIGN_DOWN(p, alignment);
s += rest;
}
// align the size up to 'alignment' bytes
s = ALIGN_UP(s, alignment);
ASSERT(IS_ALIGNED(p, alignment));
ASSERT(IS_ALIGNED(s, alignment));
*ptr = (void *)p;
*size = s;
}
int utils_env_var_has_str(const char *envvar, const char *str) {
char *value = getenv(envvar);
if (value && strstr(value, str)) {
return 1;
}
return 0;
}
// check if we are running in the proxy library
int utils_is_running_in_proxy_lib(void) {
return utils_env_var_has_str("LD_PRELOAD", "libumf_proxy.so");
}
const char *utils_parse_var(const char *var, const char *option,
const char **extraArg) {
const char *found = strstr(var, option);
// ensure that found string is first on list or it's a separating semicolon
if (!found) {
return NULL;
}
// if found string is not at the beginning of var ensure it's preceded by ';'
if (found != var && *(found - 1) != ';') {
return NULL;
}
const char *endFound = found + strlen(option);
if (!extraArg) {
// if there is no argument, matched string should end with ';' or '\0'
if (*endFound != '\0' && *endFound != ';') {
return NULL;
}
return found;
}
// check if matched string ends with ','
if (*endFound != ',') {
return NULL;
}
*extraArg = endFound + 1;
return found;
}
int utils_copy_path(const char *in_path, char out_path[], size_t path_max) {
// (- 1) because there should be a room for the terminating null byte ('\0')
size_t max_len = path_max - 1;
if (strlen(in_path) > max_len) {
LOG_ERR("path of the %s file is longer than %zu bytes", in_path,
max_len);
return -1;
}
strncpy(out_path, in_path, max_len);
out_path[path_max - 1] = '\0'; // the terminating null byte
return 0;
}
umf_result_t utils_translate_flags(unsigned in_flags, unsigned max,
umf_result_t (*translate_flag)(unsigned,
unsigned *),
unsigned *out_flags) {
unsigned out_f = 0;
for (unsigned n = 1; n < max; n <<= 1) {
if (in_flags & n) {
unsigned flag;
umf_result_t result = translate_flag(n, &flag);
if (result != UMF_RESULT_SUCCESS) {
return result;
}
out_f |= flag;
in_flags &= ~n; // clear this bit
}
}
if (in_flags != 0) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
*out_flags = out_f;
return UMF_RESULT_SUCCESS;
}