Skip to content

Commit 9a61dda

Browse files
committed
control logger thru CTL
1 parent fe3aea6 commit 9a61dda

File tree

11 files changed

+412
-70
lines changed

11 files changed

+412
-70
lines changed

src/ctl/ctl.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
#include "base_alloc/base_alloc_global.h"
2929
#include "ctl_internal.h"
30+
#include "uthash/utlist.h"
3031
#include "utils/utils_common.h"
3132
#include "utils_log.h"
32-
#include "utlist.h"
3333

3434
#ifdef _WIN32
3535
#define strtok_r strtok_s
@@ -49,13 +49,25 @@
4949
static int ctl_global_first_free = 0;
5050
static umf_ctl_node_t CTL_NODE(global)[CTL_MAX_ENTRIES];
5151

52+
static void *(*ctl_malloc_fn)(size_t) = NULL;
53+
static void (*ctl_free_fn)(void *) = NULL;
54+
55+
void ctl_init(void *(*Malloc)(size_t), void (*Free)(void *)) {
56+
if (Malloc) {
57+
ctl_malloc_fn = Malloc;
58+
}
59+
if (Free) {
60+
ctl_free_fn = Free;
61+
}
62+
}
63+
5264
typedef struct optional_umf_result_t {
5365
bool is_valid;
5466
umf_result_t value;
5567
} optional_umf_result_t;
5668

5769
void *Zalloc(size_t sz) {
58-
void *ptr = umf_ba_global_alloc(sz);
70+
void *ptr = ctl_malloc_fn(sz);
5971
if (ptr) {
6072
memset(ptr, 0, sz);
6173
}
@@ -64,7 +76,7 @@ void *Zalloc(size_t sz) {
6476

6577
char *Strdup(const char *s) {
6678
size_t len = strlen(s) + 1;
67-
char *p = umf_ba_global_alloc(len);
79+
char *p = ctl_malloc_fn(len);
6880
if (p) {
6981
memcpy(p, s, len);
7082
}
@@ -121,9 +133,9 @@ static void ctl_delete_indexes(umf_ctl_index_utlist_t *indexes) {
121133
LL_DELETE(indexes, elem);
122134
if (elem) {
123135
if (elem->arg) {
124-
umf_ba_global_free(elem->arg);
136+
ctl_free_fn(elem->arg);
125137
}
126-
umf_ba_global_free(elem);
138+
ctl_free_fn(elem);
127139
}
128140
}
129141
}
@@ -139,7 +151,7 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
139151

140152
switch (source) {
141153
case CTL_QUERY_CONFIG_INPUT:
142-
umf_ba_global_free(real_arg);
154+
ctl_free_fn(real_arg);
143155
break;
144156
case CTL_QUERY_PROGRAMMATIC:
145157
break;
@@ -153,7 +165,7 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
153165
* structure
154166
*/
155167
static void *ctl_parse_args(const struct ctl_argument *arg_proto, char *arg) {
156-
char *dest_arg = umf_ba_global_alloc(arg_proto->dest_size);
168+
char *dest_arg = ctl_malloc_fn(arg_proto->dest_size);
157169
if (dest_arg == NULL) {
158170
return NULL;
159171
}
@@ -176,7 +188,7 @@ static void *ctl_parse_args(const struct ctl_argument *arg_proto, char *arg) {
176188
return dest_arg;
177189

178190
error_parsing:
179-
umf_ba_global_free(dest_arg);
191+
ctl_free_fn(dest_arg);
180192
return NULL;
181193
}
182194

@@ -372,7 +384,7 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
372384
goto error;
373385
}
374386
// argument is a wildcard so we need to allocate it from va_list
375-
node_arg = umf_ba_global_alloc(n->arg->dest_size);
387+
node_arg = ctl_malloc_fn(n->arg->dest_size);
376388
if (node_arg == NULL) {
377389
goto error;
378390
}
@@ -386,9 +398,9 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
386398
}
387399

388400
umf_ctl_index_utlist_t *entry = NULL;
389-
entry = umf_ba_global_alloc(sizeof(*entry));
401+
entry = ctl_malloc_fn(sizeof(*entry));
390402
if (entry == NULL) {
391-
umf_ba_global_free(arg);
403+
ctl_free_fn(arg);
392404
goto error;
393405
}
394406

@@ -462,13 +474,13 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
462474
}
463475
}
464476
out:
465-
umf_ba_global_free(parse_str);
477+
ctl_free_fn(parse_str);
466478
ctl_delete_indexes(indexes);
467479
return ret;
468480

469481
error:
470482
ctl_delete_indexes(indexes);
471-
umf_ba_global_free(parse_str);
483+
ctl_free_fn(parse_str);
472484
ret.is_valid = false;
473485
return ret;
474486
}
@@ -599,7 +611,7 @@ umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,
599611

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

602-
umf_ba_global_free(buf);
614+
ctl_free_fn(buf);
603615
return ret;
604616
}
605617

@@ -661,7 +673,7 @@ umf_result_t ctl_load_config_from_file(struct ctl *ctl, void *ctx,
661673

662674
ret = ctl_load_config(ctl, ctx, buf);
663675

664-
umf_ba_global_free(buf);
676+
ctl_free_fn(buf);
665677

666678
error_file_parse:
667679
(void)fclose(fp);

src/ctl/ctl_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct ctl {
122122
};
123123

124124
void initialize_global_ctl(void);
125+
void ctl_init(void *(*Malloc)(size_t), void (*Free)(void *));
125126

126127
umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,
127128
const char *cfg_string);

src/libumf.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string.h>
1313

1414
#include "base_alloc_global.h"
15+
#include "ctl/ctl_internal.h"
1516
#include "ipc_cache.h"
1617
#include "memory_pool_internal.h"
1718
#include "memory_provider_internal.h"
@@ -36,7 +37,7 @@ static UTIL_ONCE_FLAG initMutexOnce = UTIL_ONCE_FLAG_INIT;
3637
static void initialize_init_mutex(void) { utils_mutex_init(&initMutex); }
3738

3839
static umf_ctl_node_t CTL_NODE(umf)[] = {CTL_CHILD(provider), CTL_CHILD(pool),
39-
CTL_NODE_END};
40+
CTL_CHILD(logger), CTL_NODE_END};
4041

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

@@ -65,6 +66,7 @@ umf_result_t umfInit(void) {
6566
}
6667

6768
LOG_DEBUG("UMF IPC cache initialized");
69+
ctl_init(umf_ba_global_alloc, umf_ba_global_free);
6870
initialize_global_ctl();
6971
}
7072

src/memory_pool.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ char CTL_DEFAULT_VALUES[UMF_DEFAULT_SIZE][UMF_DEFAULT_LEN] = {0};
3636

3737
static struct ctl umf_pool_ctl_root;
3838

39-
static void ctl_init(void);
39+
static void pool_ctl_init(void);
4040

4141
static umf_result_t CTL_SUBTREE_HANDLER(CTL_NONAME, by_handle)(
4242
void *ctx, umf_ctl_query_source_t source, void *arg, size_t size,
@@ -66,7 +66,7 @@ static umf_result_t CTL_SUBTREE_HANDLER(default)(
6666
umf_ctl_index_utlist_t *indexes, const char *extra_name,
6767
umf_ctl_query_type_t queryType, va_list args) {
6868
(void)indexes, (void)source, (void)ctx, (void)args;
69-
utils_init_once(&mem_pool_ctl_initialized, ctl_init);
69+
utils_init_once(&mem_pool_ctl_initialized, pool_ctl_init);
7070

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

151-
static void ctl_init(void) {
151+
static void pool_ctl_init(void) {
152152
utils_mutex_init(&ctl_mtx);
153153
CTL_REGISTER_MODULE(&umf_pool_ctl_root, stats);
154154
}
@@ -223,7 +223,7 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
223223
pool->provider = provider;
224224
}
225225

226-
utils_init_once(&mem_pool_ctl_initialized, ctl_init);
226+
utils_init_once(&mem_pool_ctl_initialized, pool_ctl_init);
227227

228228
pool->flags = flags;
229229
pool->ops = *ops;

src/utils/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
66
include(FindThreads)
77

8-
set(UMF_UTILS_SOURCES_COMMON utils_common.c utils_log.c utils_load_library.c)
8+
set(UMF_UTILS_SOURCES_COMMON utils_common.c utils_log.c utils_load_library.c
9+
../ctl/ctl.c)
910
set(UMF_UTILS_SOURCES_POSIX utils_posix_common.c utils_posix_concurrency.c)
1011
set(UMF_UTILS_SOURCES_LINUX utils_linux_common.c)
1112
set(UMF_UTILS_SOURCES_MACOSX utils_macosx_common.c)

0 commit comments

Comments
 (0)