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
4949static int ctl_global_first_free = 0 ;
5050static 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+
5264typedef struct optional_umf_result_t {
5365 bool is_valid ;
5466 umf_result_t value ;
5567} optional_umf_result_t ;
5668
5769void * 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
6577char * 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 */
155167static 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
178190error_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 }
464476out :
465- umf_ba_global_free (parse_str );
477+ ctl_free_fn (parse_str );
466478 ctl_delete_indexes (indexes );
467479 return ret ;
468480
469481error :
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
666678error_file_parse :
667679 (void )fclose (fp );
0 commit comments