Skip to content

control logger thru CTL #1446

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 1 commit into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/ctl/ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

#include "base_alloc/base_alloc_global.h"
#include "ctl_internal.h"
#include "uthash/utlist.h"
#include "utils/utils_common.h"
#include "utils_log.h"
#include "utlist.h"

#ifdef _WIN32
#define strtok_r strtok_s
Expand All @@ -49,13 +49,25 @@
static int ctl_global_first_free = 0;
static umf_ctl_node_t CTL_NODE(global)[CTL_MAX_ENTRIES];

static void *(*ctl_malloc_fn)(size_t) = NULL;
static void (*ctl_free_fn)(void *) = NULL;

void ctl_init(void *(*Malloc)(size_t), void (*Free)(void *)) {
if (Malloc) {
ctl_malloc_fn = Malloc;
}
if (Free) {
ctl_free_fn = Free;
}
}

typedef struct optional_umf_result_t {
bool is_valid;
umf_result_t value;
} optional_umf_result_t;

void *Zalloc(size_t sz) {
void *ptr = umf_ba_global_alloc(sz);
void *ptr = ctl_malloc_fn(sz);
if (ptr) {
memset(ptr, 0, sz);
}
Expand All @@ -64,7 +76,7 @@ void *Zalloc(size_t sz) {

char *Strdup(const char *s) {
size_t len = strlen(s) + 1;
char *p = umf_ba_global_alloc(len);
char *p = ctl_malloc_fn(len);
if (p) {
memcpy(p, s, len);
}
Expand Down Expand Up @@ -121,9 +133,9 @@ static void ctl_delete_indexes(umf_ctl_index_utlist_t *indexes) {
LL_DELETE(indexes, elem);
if (elem) {
if (elem->arg) {
umf_ba_global_free(elem->arg);
ctl_free_fn(elem->arg);
}
umf_ba_global_free(elem);
ctl_free_fn(elem);
}
}
}
Expand All @@ -139,7 +151,7 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,

switch (source) {
case CTL_QUERY_CONFIG_INPUT:
umf_ba_global_free(real_arg);
ctl_free_fn(real_arg);
break;
case CTL_QUERY_PROGRAMMATIC:
break;
Expand All @@ -153,7 +165,7 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
* structure
*/
static void *ctl_parse_args(const struct ctl_argument *arg_proto, char *arg) {
char *dest_arg = umf_ba_global_alloc(arg_proto->dest_size);
char *dest_arg = ctl_malloc_fn(arg_proto->dest_size);
if (dest_arg == NULL) {
return NULL;
}
Expand All @@ -176,7 +188,7 @@ static void *ctl_parse_args(const struct ctl_argument *arg_proto, char *arg) {
return dest_arg;

error_parsing:
umf_ba_global_free(dest_arg);
ctl_free_fn(dest_arg);
return NULL;
}

Expand Down Expand Up @@ -372,7 +384,7 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
goto error;
}
// argument is a wildcard so we need to allocate it from va_list
node_arg = umf_ba_global_alloc(n->arg->dest_size);
node_arg = ctl_malloc_fn(n->arg->dest_size);
if (node_arg == NULL) {
goto error;
}
Expand All @@ -386,9 +398,9 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
}

umf_ctl_index_utlist_t *entry = NULL;
entry = umf_ba_global_alloc(sizeof(*entry));
entry = ctl_malloc_fn(sizeof(*entry));
if (entry == NULL) {
umf_ba_global_free(arg);
ctl_free_fn(arg);
goto error;
}

Expand Down Expand Up @@ -462,13 +474,13 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
}
}
out:
umf_ba_global_free(parse_str);
ctl_free_fn(parse_str);
ctl_delete_indexes(indexes);
return ret;

error:
ctl_delete_indexes(indexes);
umf_ba_global_free(parse_str);
ctl_free_fn(parse_str);
ret.is_valid = false;
return ret;
}
Expand Down Expand Up @@ -599,7 +611,7 @@ umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,

umf_result_t ret = ctl_load_config(ctl, ctx, buf);

umf_ba_global_free(buf);
ctl_free_fn(buf);
return ret;
}

Expand Down Expand Up @@ -661,7 +673,7 @@ umf_result_t ctl_load_config_from_file(struct ctl *ctl, void *ctx,

ret = ctl_load_config(ctl, ctx, buf);

umf_ba_global_free(buf);
ctl_free_fn(buf);

error_file_parse:
(void)fclose(fp);
Expand Down
1 change: 1 addition & 0 deletions src/ctl/ctl_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct ctl {
};

void initialize_global_ctl(void);
void ctl_init(void *(*Malloc)(size_t), void (*Free)(void *));

umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,
const char *cfg_string);
Expand Down
4 changes: 3 additions & 1 deletion src/libumf.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>

#include "base_alloc_global.h"
#include "ctl/ctl_internal.h"
#include "ipc_cache.h"
#include "memory_pool_internal.h"
#include "memory_provider_internal.h"
Expand All @@ -36,7 +37,7 @@ static UTIL_ONCE_FLAG initMutexOnce = UTIL_ONCE_FLAG_INIT;
static void initialize_init_mutex(void) { utils_mutex_init(&initMutex); }

static umf_ctl_node_t CTL_NODE(umf)[] = {CTL_CHILD(provider), CTL_CHILD(pool),
CTL_NODE_END};
CTL_CHILD(logger), CTL_NODE_END};

void initialize_global_ctl(void) { CTL_REGISTER_MODULE(NULL, umf); }

Expand Down Expand Up @@ -65,6 +66,7 @@ umf_result_t umfInit(void) {
}

LOG_DEBUG("UMF IPC cache initialized");
ctl_init(umf_ba_global_alloc, umf_ba_global_free);
initialize_global_ctl();
}

Expand Down
8 changes: 4 additions & 4 deletions src/memory_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ char CTL_DEFAULT_VALUES[UMF_DEFAULT_SIZE][UMF_DEFAULT_LEN] = {0};

static struct ctl umf_pool_ctl_root;

static void ctl_init(void);
static void pool_ctl_init(void);

static umf_result_t CTL_SUBTREE_HANDLER(CTL_NONAME, by_handle)(
void *ctx, umf_ctl_query_source_t source, void *arg, size_t size,
Expand Down Expand Up @@ -66,7 +66,7 @@ static umf_result_t CTL_SUBTREE_HANDLER(default)(
umf_ctl_index_utlist_t *indexes, const char *extra_name,
umf_ctl_query_type_t queryType, va_list args) {
(void)indexes, (void)source, (void)ctx, (void)args;
utils_init_once(&mem_pool_ctl_initialized, ctl_init);
utils_init_once(&mem_pool_ctl_initialized, pool_ctl_init);

if (strstr(extra_name, "{}") != NULL) {
// We might implement it in future - it requires store copy of va_list
Expand Down Expand Up @@ -148,7 +148,7 @@ static const struct ctl_argument CTL_ARG(by_handle) = CTL_ARG_PTR;
umf_ctl_node_t CTL_NODE(pool)[] = {CTL_CHILD_WITH_ARG(by_handle),
CTL_LEAF_SUBTREE(default), CTL_NODE_END};

static void ctl_init(void) {
static void pool_ctl_init(void) {
utils_mutex_init(&ctl_mtx);
CTL_REGISTER_MODULE(&umf_pool_ctl_root, stats);
}
Expand Down Expand Up @@ -223,7 +223,7 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
pool->provider = provider;
}

utils_init_once(&mem_pool_ctl_initialized, ctl_init);
utils_init_once(&mem_pool_ctl_initialized, pool_ctl_init);

pool->flags = flags;
pool->ops = *ops;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
include(FindThreads)

set(UMF_UTILS_SOURCES_COMMON utils_common.c utils_log.c utils_load_library.c)
set(UMF_UTILS_SOURCES_COMMON utils_common.c utils_log.c utils_load_library.c
../ctl/ctl.c)
set(UMF_UTILS_SOURCES_POSIX utils_posix_common.c utils_posix_concurrency.c)
set(UMF_UTILS_SOURCES_LINUX utils_linux_common.c)
set(UMF_UTILS_SOURCES_MACOSX utils_macosx_common.c)
Expand Down
Loading