27
27
28
28
#include "base_alloc/base_alloc_global.h"
29
29
#include "ctl_internal.h"
30
+ #include "uthash/utlist.h"
30
31
#include "utils/utils_common.h"
31
32
#include "utils_log.h"
32
- #include "utlist.h"
33
33
34
34
#ifdef _WIN32
35
35
#define strtok_r strtok_s
49
49
static int ctl_global_first_free = 0 ;
50
50
static umf_ctl_node_t CTL_NODE (global )[CTL_MAX_ENTRIES ];
51
51
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
+
52
64
typedef struct optional_umf_result_t {
53
65
bool is_valid ;
54
66
umf_result_t value ;
55
67
} optional_umf_result_t ;
56
68
57
69
void * Zalloc (size_t sz ) {
58
- void * ptr = umf_ba_global_alloc (sz );
70
+ void * ptr = ctl_malloc_fn (sz );
59
71
if (ptr ) {
60
72
memset (ptr , 0 , sz );
61
73
}
@@ -64,7 +76,7 @@ void *Zalloc(size_t sz) {
64
76
65
77
char * Strdup (const char * s ) {
66
78
size_t len = strlen (s ) + 1 ;
67
- char * p = umf_ba_global_alloc (len );
79
+ char * p = ctl_malloc_fn (len );
68
80
if (p ) {
69
81
memcpy (p , s , len );
70
82
}
@@ -121,9 +133,9 @@ static void ctl_delete_indexes(umf_ctl_index_utlist_t *indexes) {
121
133
LL_DELETE (indexes , elem );
122
134
if (elem ) {
123
135
if (elem -> arg ) {
124
- umf_ba_global_free (elem -> arg );
136
+ ctl_free_fn (elem -> arg );
125
137
}
126
- umf_ba_global_free (elem );
138
+ ctl_free_fn (elem );
127
139
}
128
140
}
129
141
}
@@ -139,7 +151,7 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
139
151
140
152
switch (source ) {
141
153
case CTL_QUERY_CONFIG_INPUT :
142
- umf_ba_global_free (real_arg );
154
+ ctl_free_fn (real_arg );
143
155
break ;
144
156
case CTL_QUERY_PROGRAMMATIC :
145
157
break ;
@@ -153,7 +165,7 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
153
165
* structure
154
166
*/
155
167
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 );
157
169
if (dest_arg == NULL ) {
158
170
return NULL ;
159
171
}
@@ -176,7 +188,7 @@ static void *ctl_parse_args(const struct ctl_argument *arg_proto, char *arg) {
176
188
return dest_arg ;
177
189
178
190
error_parsing :
179
- umf_ba_global_free (dest_arg );
191
+ ctl_free_fn (dest_arg );
180
192
return NULL ;
181
193
}
182
194
@@ -372,7 +384,7 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
372
384
goto error ;
373
385
}
374
386
// 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 );
376
388
if (node_arg == NULL ) {
377
389
goto error ;
378
390
}
@@ -386,9 +398,9 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
386
398
}
387
399
388
400
umf_ctl_index_utlist_t * entry = NULL ;
389
- entry = umf_ba_global_alloc (sizeof (* entry ));
401
+ entry = ctl_malloc_fn (sizeof (* entry ));
390
402
if (entry == NULL ) {
391
- umf_ba_global_free (arg );
403
+ ctl_free_fn (arg );
392
404
goto error ;
393
405
}
394
406
@@ -462,13 +474,13 @@ ctl_find_and_execute_node(const umf_ctl_node_t *nodes, void *ctx,
462
474
}
463
475
}
464
476
out :
465
- umf_ba_global_free (parse_str );
477
+ ctl_free_fn (parse_str );
466
478
ctl_delete_indexes (indexes );
467
479
return ret ;
468
480
469
481
error :
470
482
ctl_delete_indexes (indexes );
471
- umf_ba_global_free (parse_str );
483
+ ctl_free_fn (parse_str );
472
484
ret .is_valid = false;
473
485
return ret ;
474
486
}
@@ -599,7 +611,7 @@ umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,
599
611
600
612
umf_result_t ret = ctl_load_config (ctl , ctx , buf );
601
613
602
- umf_ba_global_free (buf );
614
+ ctl_free_fn (buf );
603
615
return ret ;
604
616
}
605
617
@@ -661,7 +673,7 @@ umf_result_t ctl_load_config_from_file(struct ctl *ctl, void *ctx,
661
673
662
674
ret = ctl_load_config (ctl , ctx , buf );
663
675
664
- umf_ba_global_free (buf );
676
+ ctl_free_fn (buf );
665
677
666
678
error_file_parse :
667
679
(void )fclose (fp );
0 commit comments