-
Notifications
You must be signed in to change notification settings - Fork 39
load ctl defaults from env variable/file #1457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,16 +24,21 @@ | |
#include "utils_assert.h" | ||
#include "utils_concurrency.h" | ||
#include "utils_log.h" | ||
#include "utlist.h" | ||
|
||
#define UMF_DEFAULT_SIZE 100 | ||
#define UMF_DEFAULT_LEN 100 | ||
typedef struct ctl_default_entry_t { | ||
char *name; | ||
void *value; | ||
size_t value_size; | ||
umf_ctl_query_source_t source; | ||
struct ctl_default_entry_t *next; | ||
} ctl_default_entry_t; | ||
|
||
static ctl_default_entry_t *ctl_default_list = NULL; | ||
|
||
utils_mutex_t ctl_mtx; | ||
static UTIL_ONCE_FLAG mem_pool_ctl_initialized = UTIL_ONCE_FLAG_INIT; | ||
|
||
char CTL_DEFAULT_ENTRIES[UMF_DEFAULT_SIZE][UMF_DEFAULT_LEN] = {0}; | ||
char CTL_DEFAULT_VALUES[UMF_DEFAULT_SIZE][UMF_DEFAULT_LEN] = {0}; | ||
|
||
static struct ctl umf_pool_ctl_root; | ||
|
||
static void pool_ctl_init(void); | ||
|
@@ -79,36 +84,76 @@ static umf_result_t CTL_SUBTREE_HANDLER(default)( | |
|
||
utils_mutex_lock(&ctl_mtx); | ||
|
||
ctl_default_entry_t *entry = NULL; | ||
LL_FOREACH(ctl_default_list, entry) { | ||
if (strcmp(entry->name, extra_name) == 0) { | ||
break; | ||
} | ||
} | ||
|
||
if (queryType == CTL_QUERY_WRITE) { | ||
int i = 0; | ||
for (; i < UMF_DEFAULT_SIZE; i++) { | ||
if (CTL_DEFAULT_ENTRIES[i][0] == '\0' || | ||
strcmp(CTL_DEFAULT_ENTRIES[i], extra_name) == 0) { | ||
strncpy(CTL_DEFAULT_ENTRIES[i], extra_name, UMF_DEFAULT_LEN); | ||
CTL_DEFAULT_ENTRIES[i][UMF_DEFAULT_LEN - 1] = '\0'; | ||
strncpy(CTL_DEFAULT_VALUES[i], arg, UMF_DEFAULT_LEN); | ||
CTL_DEFAULT_VALUES[i][UMF_DEFAULT_LEN - 1] = '\0'; | ||
break; | ||
bool is_new_entry = false; | ||
if (entry == NULL) { | ||
entry = umf_ba_global_alloc(sizeof(*entry)); | ||
if (entry == NULL) { | ||
utils_mutex_unlock(&ctl_mtx); | ||
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; | ||
} | ||
|
||
entry->name = NULL; | ||
entry->value = NULL; | ||
entry->next = NULL; | ||
is_new_entry = true; | ||
} | ||
if (UMF_DEFAULT_SIZE == i) { | ||
LOG_ERR("Default entries array is full"); | ||
|
||
size_t name_len = strlen(extra_name) + 1; | ||
char *new_name = umf_ba_global_alloc(name_len); | ||
if (new_name == NULL) { | ||
utils_mutex_unlock(&ctl_mtx); | ||
return UMF_RESULT_ERROR_OUT_OF_RESOURCES; | ||
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; | ||
} | ||
} else if (queryType == CTL_QUERY_READ) { | ||
int i = 0; | ||
for (; i < UMF_DEFAULT_SIZE; i++) { | ||
if (strcmp(CTL_DEFAULT_ENTRIES[i], extra_name) == 0) { | ||
strncpy(arg, CTL_DEFAULT_VALUES[i], size); | ||
break; | ||
|
||
memcpy(new_name, extra_name, name_len); | ||
if (entry->name) { | ||
umf_ba_global_free(entry->name); | ||
} | ||
entry->name = new_name; | ||
|
||
void *new_value = NULL; | ||
if (size > 0) { | ||
new_value = umf_ba_global_alloc(size); | ||
if (new_value == NULL) { | ||
utils_mutex_unlock(&ctl_mtx); | ||
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; | ||
} | ||
memcpy(new_value, arg, size); | ||
} | ||
if (UMF_DEFAULT_SIZE == i) { | ||
|
||
if (entry->value) { | ||
umf_ba_global_free(entry->value); | ||
} | ||
|
||
entry->value = new_value; | ||
entry->value_size = size; | ||
entry->source = source; | ||
|
||
if (is_new_entry) { | ||
LL_APPEND(ctl_default_list, entry); | ||
} | ||
} else if (queryType == CTL_QUERY_READ) { | ||
if (entry == NULL) { | ||
LOG_WARN("Wrong path name: %s", extra_name); | ||
utils_mutex_unlock(&ctl_mtx); | ||
return UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
if (entry->value_size > size) { | ||
LOG_ERR("Provided buffer size %zu is smaller than field size %zu", | ||
size, entry->value_size); | ||
utils_mutex_unlock(&ctl_mtx); | ||
return UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
} | ||
memcpy(arg, entry->value, entry->value_size); | ||
} | ||
|
||
utils_mutex_unlock(&ctl_mtx); | ||
|
@@ -174,12 +219,11 @@ static const umf_pool_create_flags_t UMF_POOL_CREATE_FLAG_ALL = | |
// windows do not allow to use uninitialized va_list so this function help us to initialize it. | ||
static umf_result_t default_ctl_helper(const umf_memory_pool_ops_t *ops, | ||
void *ctl, const char *name, void *arg, | ||
...) { | ||
size_t size, ...) { | ||
va_list empty_args; | ||
va_start(empty_args, arg); | ||
umf_result_t ret = | ||
ops->ext_ctl(ctl, CTL_QUERY_PROGRAMMATIC, name, arg, UMF_DEFAULT_LEN, | ||
CTL_QUERY_WRITE, empty_args); | ||
va_start(empty_args, size); | ||
umf_result_t ret = ops->ext_ctl(ctl, CTL_QUERY_PROGRAMMATIC, name, arg, | ||
size, CTL_QUERY_WRITE, empty_args); | ||
va_end(empty_args); | ||
return ret; | ||
} | ||
|
@@ -246,18 +290,23 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops, | |
} | ||
|
||
// Set default property "name" to pool if exists | ||
for (int i = 0; i < UMF_DEFAULT_SIZE; i++) { | ||
const char *pname = NULL; | ||
ret = ops->get_name(NULL, &pname); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
LOG_ERR("Failed to get pool name"); | ||
goto err_pool_init; | ||
} | ||
if (CTL_DEFAULT_ENTRIES[i][0] != '\0' && pname && | ||
strstr(CTL_DEFAULT_ENTRIES[i], pname)) { | ||
|
||
default_ctl_helper(ops, pool->pool_priv, CTL_DEFAULT_ENTRIES[i], | ||
CTL_DEFAULT_VALUES[i]); | ||
const char *pname = NULL; | ||
ret = ops->get_name(NULL, &pname); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
LOG_ERR("Failed to get pool name"); | ||
goto err_pool_init; | ||
} | ||
assert(pname != NULL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Pointers can be directly handled: assert(pname); |
||
|
||
size_t pname_len = strlen(pname); | ||
ctl_default_entry_t *it = NULL; | ||
LL_FOREACH(ctl_default_list, it) { | ||
if (strlen(it->name) > pname_len + 1 && | ||
strncmp(it->name, pname, pname_len) == 0 && | ||
it->name[pname_len] == '.') { | ||
const char *ctl_name = it->name + pname_len + 1; | ||
default_ctl_helper(ops, pool->pool_priv, ctl_name, it->value, | ||
it->value_size); | ||
} | ||
} | ||
|
||
|
@@ -454,3 +503,23 @@ umf_result_t umfPoolGetTag(umf_memory_pool_handle_t hPool, void **tag) { | |
utils_mutex_unlock(&hPool->lock); | ||
return UMF_RESULT_SUCCESS; | ||
} | ||
|
||
void umfPoolCtlDefaultsDestroy(void) { | ||
utils_init_once(&mem_pool_ctl_initialized, pool_ctl_init); | ||
ldorau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
utils_mutex_lock(&ctl_mtx); | ||
|
||
ctl_default_entry_t *entry = NULL, *tmp = NULL; | ||
LL_FOREACH_SAFE(ctl_default_list, entry, tmp) { | ||
LL_DELETE(ctl_default_list, entry); | ||
if (entry->name) { | ||
umf_ba_global_free(entry->name); | ||
} | ||
if (entry->value) { | ||
umf_ba_global_free(entry->value); | ||
} | ||
umf_ba_global_free(entry); | ||
} | ||
|
||
utils_mutex_unlock(&ctl_mtx); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.