Skip to content

Commit 5751bf1

Browse files
authored
Merge pull request #438 from ldorau/Make_os_translate_mem_protection_flags_return_umf_result_t
Make os_translate_mem_protection_flags return umf_result_t
2 parents 13d7b52 + 5e4a1e6 commit 5751bf1

File tree

4 files changed

+62
-40
lines changed

4 files changed

+62
-40
lines changed

src/provider/provider_os_memory.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,29 @@ static umf_result_t nodemask_to_hwloc_nodeset(const unsigned long *nodemask,
108108
return UMF_RESULT_SUCCESS;
109109
}
110110

111-
int os_translate_flags(unsigned in_flags, unsigned max,
112-
int (*translate_flag)(unsigned)) {
113-
unsigned out_flags = 0;
111+
umf_result_t os_translate_flags(unsigned in_flags, unsigned max,
112+
umf_result_t (*translate_flag)(unsigned,
113+
unsigned *),
114+
unsigned *out_flags) {
115+
unsigned out_f = 0;
114116
for (unsigned n = 1; n < max; n <<= 1) {
115117
if (in_flags & n) {
116-
int f = translate_flag(n);
117-
if (f < 0) {
118-
return -1;
118+
unsigned flag;
119+
umf_result_t result = translate_flag(n, &flag);
120+
if (result != UMF_RESULT_SUCCESS) {
121+
return result;
119122
}
120-
out_flags |= (unsigned)f;
123+
out_f |= flag;
121124
in_flags &= ~n; // clear this bit
122125
}
123126
}
124127

125128
if (in_flags != 0) {
126-
return -1;
129+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
127130
}
128131

129-
return out_flags;
132+
*out_flags = out_f;
133+
return UMF_RESULT_SUCCESS;
130134
}
131135

132136
static hwloc_membind_policy_t translate_numa_mode(umf_numa_mode_t mode,
@@ -174,14 +178,15 @@ static int getHwlocMembindFlags(umf_numa_mode_t mode) {
174178

175179
static umf_result_t translate_params(umf_os_memory_provider_params_t *in_params,
176180
os_memory_provider_t *provider) {
181+
umf_result_t result;
177182
int ret;
178183

179-
ret = os_translate_mem_protection_flags(in_params->protection);
180-
if (ret < 0) {
184+
result = os_translate_mem_protection_flags(in_params->protection,
185+
&provider->protection);
186+
if (result != UMF_RESULT_SUCCESS) {
181187
LOG_ERR("incorrect memory protection flags: %u", in_params->protection);
182-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
188+
return result;
183189
}
184-
provider->protection = ret;
185190

186191
// NUMA config
187192
int emptyNodeset = (!in_params->maxnode || !in_params->nodemask);

src/provider/provider_os_memory_internal.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ typedef enum umf_purge_advise_t {
2020
UMF_PURGE_FORCE,
2121
} umf_purge_advise_t;
2222

23-
int os_translate_flags(unsigned in_flags, unsigned max,
24-
int (*translate_flag)(unsigned));
23+
umf_result_t os_translate_flags(unsigned in_flags, unsigned max,
24+
umf_result_t (*translate_flag)(unsigned,
25+
unsigned *),
26+
unsigned *out_flags);
2527

26-
int os_translate_mem_protection_flags(unsigned protection);
28+
umf_result_t os_translate_mem_protection_flags(unsigned in_protection,
29+
unsigned *out_protection);
2730

2831
void *os_mmap(void *hint_addr, size_t length, int prot);
2932

src/provider/provider_os_memory_linux.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,31 @@
1313
#include "provider_os_memory_internal.h"
1414
#include <umf/providers/provider_os_memory.h>
1515

16-
static int os_translate_mem_protection_one_flag(unsigned protection) {
17-
switch (protection) {
16+
umf_result_t os_translate_mem_protection_one_flag(unsigned in_protection,
17+
unsigned *out_protection) {
18+
switch (in_protection) {
1819
case UMF_PROTECTION_NONE:
19-
return PROT_NONE;
20+
*out_protection = PROT_NONE;
21+
return UMF_RESULT_SUCCESS;
2022
case UMF_PROTECTION_READ:
21-
return PROT_READ;
23+
*out_protection = PROT_READ;
24+
return UMF_RESULT_SUCCESS;
2225
case UMF_PROTECTION_WRITE:
23-
return PROT_WRITE;
26+
*out_protection = PROT_WRITE;
27+
return UMF_RESULT_SUCCESS;
2428
case UMF_PROTECTION_EXEC:
25-
return PROT_EXEC;
29+
*out_protection = PROT_EXEC;
30+
return UMF_RESULT_SUCCESS;
2631
}
27-
assert(0);
28-
return -1;
32+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
2933
}
3034

31-
int os_translate_mem_protection_flags(unsigned protection_flags) {
35+
umf_result_t os_translate_mem_protection_flags(unsigned in_protection,
36+
unsigned *out_protection) {
3237
// translate protection - combination of 'umf_mem_protection_flags_t' flags
33-
return os_translate_flags(protection_flags, UMF_PROTECTION_MAX,
34-
os_translate_mem_protection_one_flag);
38+
return os_translate_flags(in_protection, UMF_PROTECTION_MAX,
39+
os_translate_mem_protection_one_flag,
40+
out_protection);
3541
}
3642

3743
static int os_translate_purge_advise(umf_purge_advise_t advise) {

src/provider/provider_os_memory_windows.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,38 @@
2020
static UTIL_ONCE_FLAG Page_size_is_initialized = UTIL_ONCE_FLAG_INIT;
2121
static size_t Page_size;
2222

23-
int os_translate_mem_protection_flags(unsigned protection) {
24-
switch (protection) {
23+
umf_result_t os_translate_mem_protection_flags(unsigned in_protection,
24+
unsigned *out_protection) {
25+
switch (in_protection) {
2526
case UMF_PROTECTION_NONE:
26-
return PAGE_NOACCESS;
27+
*out_protection = PAGE_NOACCESS;
28+
return UMF_RESULT_SUCCESS;
2729
case UMF_PROTECTION_EXEC:
28-
return PAGE_EXECUTE;
30+
*out_protection = PAGE_EXECUTE;
31+
return UMF_RESULT_SUCCESS;
2932
case (UMF_PROTECTION_EXEC | UMF_PROTECTION_READ):
30-
return PAGE_EXECUTE_READ;
33+
*out_protection = PAGE_EXECUTE_READ;
34+
return UMF_RESULT_SUCCESS;
3135
case (UMF_PROTECTION_EXEC | UMF_PROTECTION_READ | UMF_PROTECTION_WRITE):
32-
return PAGE_EXECUTE_READWRITE;
36+
*out_protection = PAGE_EXECUTE_READWRITE;
37+
return UMF_RESULT_SUCCESS;
3338
case (UMF_PROTECTION_EXEC | UMF_PROTECTION_WRITE):
34-
return PAGE_EXECUTE_WRITECOPY;
39+
*out_protection = PAGE_EXECUTE_WRITECOPY;
40+
return UMF_RESULT_SUCCESS;
3541
case UMF_PROTECTION_READ:
36-
return PAGE_READONLY;
42+
*out_protection = PAGE_READONLY;
43+
return UMF_RESULT_SUCCESS;
3744
case (UMF_PROTECTION_READ | UMF_PROTECTION_WRITE):
38-
return PAGE_READWRITE;
45+
*out_protection = PAGE_READWRITE;
46+
return UMF_RESULT_SUCCESS;
3947
case UMF_PROTECTION_WRITE:
40-
return PAGE_WRITECOPY;
48+
*out_protection = PAGE_WRITECOPY;
49+
return UMF_RESULT_SUCCESS;
4150
}
4251
LOG_ERR("os_translate_mem_protection_flags(): unsupported protection flag: "
4352
"%u",
44-
protection);
45-
assert(0);
46-
return -1;
53+
in_protection);
54+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
4755
}
4856

4957
void *os_mmap(void *hint_addr, size_t length, int prot) {

0 commit comments

Comments
 (0)