Skip to content

Commit 527d19e

Browse files
committed
add defaults to providers
1 parent cfc99b7 commit 527d19e

File tree

11 files changed

+257
-142
lines changed

11 files changed

+257
-142
lines changed

include/umf/memory_provider_ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ typedef struct umf_memory_provider_ops_t {
126126
/// \details
127127
/// * Implementations *must* return a literal null-terminated string.
128128
///
129-
/// * Implementations *must* return default pool name when NULL is provided,
129+
/// * Implementations *must* return default provider name when NULL is provided,
130130
/// otherwise the pool's name is returned.
131131
///
132132
/// * The returned name should not exceed 64 characters and may contain

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(UMF_LIBS umf_utils umf_ba umf_coarse $<BUILD_INTERFACE:${UMF_HWLOC_NAME}>)
3838

3939
set(UMF_SOURCES
4040
ctl/ctl.c
41+
ctl/ctl_defaults.c
4142
libumf.c
4243
ipc.c
4344
ipc_cache.c

src/ctl/ctl_defaults.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
*
3+
* Copyright (C) 2025 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*/
8+
9+
#include "ctl_defaults.h"
10+
11+
#include <string.h>
12+
13+
#include "base_alloc_global.h"
14+
#include "utils_concurrency.h"
15+
#include "utils_log.h"
16+
#include "utlist.h"
17+
18+
static umf_result_t default_ctl_helper(ctl_ext_ctl_fn fn, void *ctl,
19+
const char *name, void *arg, size_t size,
20+
...) {
21+
va_list empty_args;
22+
va_start(empty_args, size);
23+
umf_result_t ret = fn(ctl, CTL_QUERY_PROGRAMMATIC, name, arg, size,
24+
CTL_QUERY_WRITE, empty_args);
25+
va_end(empty_args);
26+
return ret;
27+
}
28+
29+
umf_result_t ctl_default_subtree(ctl_default_entry_t **list, utils_mutex_t *mtx,
30+
umf_ctl_query_source_t source, void *arg,
31+
size_t size, const char *extra_name,
32+
umf_ctl_query_type_t queryType) {
33+
(void)source;
34+
if (strstr(extra_name, "{}") != NULL) {
35+
LOG_ERR("%s, default setting do not support wildcard parameters {}",
36+
extra_name);
37+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
38+
}
39+
40+
utils_mutex_lock(mtx);
41+
42+
ctl_default_entry_t *entry = NULL;
43+
LL_FOREACH(*list, entry) {
44+
if (strcmp(entry->name, extra_name) == 0) {
45+
break;
46+
}
47+
}
48+
49+
if (queryType == CTL_QUERY_WRITE) {
50+
bool is_new_entry = false;
51+
if (!entry) {
52+
entry = umf_ba_global_alloc(sizeof(*entry));
53+
if (!entry) {
54+
utils_mutex_unlock(mtx);
55+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
56+
}
57+
entry->name = NULL;
58+
entry->value = NULL;
59+
entry->next = NULL;
60+
is_new_entry = true;
61+
}
62+
63+
char *new_name = umf_ba_global_strdup(extra_name);
64+
if (!new_name) {
65+
utils_mutex_unlock(mtx);
66+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
67+
}
68+
69+
if (entry->name) {
70+
umf_ba_global_free(entry->name);
71+
}
72+
entry->name = new_name;
73+
74+
void *new_value = NULL;
75+
if (size > 0) {
76+
new_value = umf_ba_global_alloc(size);
77+
if (!new_value) {
78+
utils_mutex_unlock(mtx);
79+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
80+
}
81+
memcpy(new_value, arg, size);
82+
}
83+
if (entry->value) {
84+
umf_ba_global_free(entry->value);
85+
}
86+
entry->value = new_value;
87+
entry->value_size = size;
88+
entry->source = source;
89+
90+
if (is_new_entry) {
91+
LL_APPEND(*list, entry);
92+
}
93+
} else if (queryType == CTL_QUERY_READ) {
94+
if (!entry) {
95+
LOG_WARN("Wrong path name: %s", extra_name);
96+
utils_mutex_unlock(mtx);
97+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
98+
}
99+
100+
if (entry->value_size > size) {
101+
LOG_ERR("Provided buffer size %zu is smaller than field size %zu",
102+
size, entry->value_size);
103+
utils_mutex_unlock(mtx);
104+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
105+
}
106+
memcpy(arg, entry->value, entry->value_size);
107+
}
108+
109+
utils_mutex_unlock(mtx);
110+
return UMF_RESULT_SUCCESS;
111+
}
112+
113+
void ctl_default_apply(ctl_default_entry_t *list, const char *pname,
114+
ctl_ext_ctl_fn ext_ctl, void *priv) {
115+
if (!pname || !ext_ctl) {
116+
return;
117+
}
118+
119+
size_t pname_len = strlen(pname);
120+
ctl_default_entry_t *it = NULL;
121+
LL_FOREACH(list, it) {
122+
if (strlen(it->name) > pname_len + 1 &&
123+
strncmp(it->name, pname, pname_len) == 0 &&
124+
it->name[pname_len] == '.') {
125+
const char *ctl_name = it->name + pname_len + 1;
126+
default_ctl_helper(ext_ctl, priv, ctl_name, it->value,
127+
it->value_size);
128+
}
129+
}
130+
}
131+
132+
void ctl_default_destroy(ctl_default_entry_t **list, utils_mutex_t *mtx) {
133+
utils_mutex_lock(mtx);
134+
ctl_default_entry_t *entry = NULL, *tmp = NULL;
135+
LL_FOREACH_SAFE(*list, entry, tmp) {
136+
LL_DELETE(*list, entry);
137+
if (entry->name) {
138+
umf_ba_global_free(entry->name);
139+
}
140+
if (entry->value) {
141+
umf_ba_global_free(entry->value);
142+
}
143+
umf_ba_global_free(entry);
144+
}
145+
utils_mutex_unlock(mtx);
146+
}

src/ctl/ctl_defaults.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
*
3+
* Copyright (C) 2025 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*/
8+
9+
#ifndef UMF_CTL_DEFAULTS_H
10+
#define UMF_CTL_DEFAULTS_H 1
11+
12+
#include <stddef.h>
13+
#include <stdarg.h>
14+
15+
#include <umf/base.h>
16+
17+
#include "ctl_internal.h"
18+
#include "utils_concurrency.h"
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
typedef struct ctl_default_entry_t {
25+
char *name;
26+
void *value;
27+
size_t value_size;
28+
umf_ctl_query_source_t source;
29+
struct ctl_default_entry_t *next;
30+
} ctl_default_entry_t;
31+
32+
umf_result_t ctl_default_subtree(ctl_default_entry_t **list, utils_mutex_t *mtx,
33+
umf_ctl_query_source_t source, void *arg,
34+
size_t size, const char *extra_name,
35+
umf_ctl_query_type_t queryType);
36+
37+
typedef umf_result_t (*ctl_ext_ctl_fn)(void *obj, umf_ctl_query_source_t source,
38+
const char *name, void *arg, size_t size,
39+
umf_ctl_query_type_t queryType,
40+
va_list args);
41+
42+
void ctl_default_apply(ctl_default_entry_t *list, const char *pname,
43+
ctl_ext_ctl_fn ext_ctl, void *priv);
44+
45+
void ctl_default_destroy(ctl_default_entry_t **list, utils_mutex_t *mtx);
46+
47+
#ifdef __cplusplus
48+
}
49+
#endif
50+
51+
#endif /* UMF_CTL_DEFAULTS_H */

src/libumf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ umf_result_t umfTearDown(void) {
139139
umfMemoryTrackerDestroy(t);
140140
LOG_DEBUG("UMF tracker destroyed");
141141

142+
umfProviderCtlDefaultsDestroy();
142143
umfPoolCtlDefaultsDestroy();
143144

144145
umf_ba_destroy_global();

0 commit comments

Comments
 (0)