Skip to content

Commit 1b687d4

Browse files
committed
control logger thru CTL
1 parent fe3aea6 commit 1b687d4

File tree

11 files changed

+450
-110
lines changed

11 files changed

+450
-110
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: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ struct ctl_argument {
7979

8080
#define sizeof_member(t, m) sizeof(((t *)0)->m)
8181

82-
#define CTL_ARG_PARSER(t, p) \
83-
{ 0, sizeof(t), p }
82+
#define CTL_ARG_PARSER(t, p) {0, sizeof(t), p}
8483

85-
#define CTL_ARG_PARSER_STRUCT(t, m, p) \
86-
{ offsetof(t, m), sizeof_member(t, m), p }
84+
#define CTL_ARG_PARSER_STRUCT(t, m, p) {offsetof(t, m), sizeof_member(t, m), p}
8785

88-
#define CTL_ARG_PARSER_END \
89-
{ 0, 0, NULL }
86+
#define CTL_ARG_PARSER_END {0, 0, NULL}
9087

9188
/*
9289
* CTL Tree node structure, do not use directly. All the necessary functionality
@@ -122,6 +119,7 @@ struct ctl {
122119
};
123120

124121
void initialize_global_ctl(void);
122+
void ctl_init(void *(*Malloc)(size_t), void (*Free)(void *));
125123

126124
umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,
127125
const char *cfg_string);
@@ -175,7 +173,7 @@ int ctl_arg_string(const void *arg, void *dest, size_t dest_size);
175173
#define CTL_NONAME CTL_NAMELESS_NODE_
176174

177175
#define CTL_NODE_END \
178-
{ NULL, CTL_NODE_UNKNOWN, NULL, NULL, NULL, NULL, NULL, NULL }
176+
{NULL, CTL_NODE_UNKNOWN, NULL, NULL, NULL, NULL, NULL, NULL}
179177

180178
#define CTL_NODE(name, ...) ctl_node_##__VA_ARGS__##_##name
181179

@@ -186,28 +184,28 @@ umf_result_t ctl_query(struct ctl *ctl, void *ctx,
186184

187185
/* Declaration of a new child node */
188186
#define CTL_CHILD(name, ...) \
189-
{ \
190-
CTL_STR(name), CTL_NODE_NAMED, NULL, NULL, NULL, NULL, NULL, \
191-
(struct ctl_node *)CTL_NODE(name, __VA_ARGS__) \
192-
}
187+
{CTL_STR(name), CTL_NODE_NAMED, \
188+
NULL, NULL, \
189+
NULL, NULL, \
190+
NULL, (struct ctl_node *)CTL_NODE(name, __VA_ARGS__)}
193191

194192
/*
195193
* Declaration of a new child node with an argument
196194
* This is used to declare that the following node is an argument node, which
197195
* should be parsed and provided to the handler function in argument list.
198196
*/
199197
#define CTL_CHILD_WITH_ARG(name, ...) \
200-
{ \
201-
CTL_STR(name), CTL_NODE_NAMED, NULL, NULL, NULL, NULL, &CTL_ARG(name), \
202-
(struct ctl_node *)CTL_NODE(name, __VA_ARGS__) \
203-
}
198+
{CTL_STR(name), CTL_NODE_NAMED, \
199+
NULL, NULL, \
200+
NULL, NULL, \
201+
&CTL_ARG(name), (struct ctl_node *)CTL_NODE(name, __VA_ARGS__)}
204202

205203
/* Declaration of a new indexed node */
206204
#define CTL_INDEXED(name, ...) \
207-
{ \
208-
CTL_STR(name), CTL_NODE_INDEXED, NULL, NULL, NULL, NULL, NULL, \
209-
(struct ctl_node *)CTL_NODE(name, __VA_ARGS__) \
210-
}
205+
{CTL_STR(name), CTL_NODE_INDEXED, \
206+
NULL, NULL, \
207+
NULL, NULL, \
208+
NULL, (struct ctl_node *)CTL_NODE(name, __VA_ARGS__)}
211209

212210
#define CTL_HANDLER_NAME(name, action, ...) \
213211
ctl_##__VA_ARGS__##_##name##_##action
@@ -228,47 +226,48 @@ umf_result_t ctl_query(struct ctl *ctl, void *ctx,
228226
* must be declared by CTL_READ_HANDLER macro.
229227
*/
230228
#define CTL_LEAF_RO(name, ...) \
231-
{ \
232-
CTL_STR(name), CTL_NODE_LEAF, CTL_READ_HANDLER(name, __VA_ARGS__), \
233-
NULL, NULL, NULL, NULL, NULL \
234-
}
229+
{CTL_STR(name), CTL_NODE_LEAF, CTL_READ_HANDLER(name, __VA_ARGS__), \
230+
NULL, NULL, NULL, \
231+
NULL, NULL}
235232

236233
/*
237234
* Declaration of a new write-only leaf. If used the corresponding write
238235
* function must be declared by CTL_WRITE_HANDLER macro.
239236
*/
240237
#define CTL_LEAF_WO(name, ...) \
241-
{ \
242-
CTL_STR(name), CTL_NODE_LEAF, NULL, \
243-
CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL, NULL, &CTL_ARG(name), \
244-
NULL \
245-
}
238+
{CTL_STR(name), CTL_NODE_LEAF, \
239+
NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), \
240+
NULL, NULL, \
241+
&CTL_ARG(name), NULL}
246242

247243
/*
248244
* Declaration of a new runnable leaf. If used the corresponding run
249245
* function must be declared by CTL_RUNNABLE_HANDLER macro.
250246
*/
251247
#define CTL_LEAF_RUNNABLE(name, ...) \
252-
{ \
253-
CTL_STR(name), CTL_NODE_LEAF, NULL, NULL, \
254-
CTL_RUNNABLE_HANDLER(name, __VA_ARGS__), NULL, NULL, NULL \
255-
}
248+
{CTL_STR(name), \
249+
CTL_NODE_LEAF, \
250+
NULL, \
251+
NULL, \
252+
CTL_RUNNABLE_HANDLER(name, __VA_ARGS__), \
253+
NULL, \
254+
NULL, \
255+
NULL}
256256

257257
#define CTL_LEAF_SUBTREE(name, ...) \
258-
{ \
259-
CTL_STR(name), CTL_NODE_SUBTREE, NULL, NULL, NULL, \
260-
CTL_SUBTREE_HANDLER(name, __VA_ARGS__), NULL, NULL \
261-
}
258+
{CTL_STR(name), CTL_NODE_SUBTREE, \
259+
NULL, NULL, \
260+
NULL, CTL_SUBTREE_HANDLER(name, __VA_ARGS__), \
261+
NULL, NULL}
262262

263263
/*
264264
* Declaration of a new read-write leaf. If used both read and write function
265265
* must be declared by CTL_READ_HANDLER and CTL_WRITE_HANDLER macros.
266266
*/
267267
#define CTL_LEAF_RW(name) \
268-
{ \
269-
CTL_STR(name), CTL_NODE_LEAF, CTL_READ_HANDLER(name), \
270-
CTL_WRITE_HANDLER(name), NULL, NULL, &CTL_ARG(name), NULL \
271-
}
268+
{CTL_STR(name), CTL_NODE_LEAF, CTL_READ_HANDLER(name), \
269+
CTL_WRITE_HANDLER(name), NULL, NULL, \
270+
&CTL_ARG(name), NULL}
272271

273272
#define CTL_REGISTER_MODULE(_ctl, name) \
274273
ctl_register_module_node((_ctl), CTL_STR(name), \

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)