mirror of
https://dev.iopsys.eu/bbf/bbfdm.git
synced 2025-12-10 07:44:39 +01:00
reduce unused memory allocation
This commit is contained in:
parent
2977240eb0
commit
c88074d9c2
4 changed files with 50 additions and 51 deletions
|
|
@ -659,7 +659,8 @@ char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_secti
|
|||
if (instance && *instance == '\0') {
|
||||
int max_inst = find_max_instance(dmctx, parent_node);
|
||||
snprintf(buf, sizeof(buf), "%d", max_inst);
|
||||
instance = dmuci_set_value_by_section(s, inst_opt, buf);
|
||||
dmuci_set_value_by_section(s, inst_opt, buf);
|
||||
instance = dmstrdup(buf);
|
||||
}
|
||||
|
||||
int inst_mode = get_instance_mode(dmctx, parent_node);
|
||||
|
|
@ -670,7 +671,8 @@ char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_secti
|
|||
dmuci_get_value_by_section_string(s, alias_opt, &alias);
|
||||
if (alias && alias[0] == '\0') {
|
||||
snprintf(buf, sizeof(buf), "cpe-%s", instance);
|
||||
alias = dmuci_set_value_by_section(s, alias_opt, buf);
|
||||
dmuci_set_value_by_section(s, alias_opt, buf);
|
||||
alias = dmstrdup(buf);
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "[%s]", alias);
|
||||
instance = dmstrdup(buf);
|
||||
|
|
@ -790,8 +792,8 @@ char *update_instance_alias(int action, char **last_inst, char **max_inst, void
|
|||
dmuci_get_value_by_section_string(s, inst_opt, &instance);
|
||||
if (instance[0] == '\0') {
|
||||
snprintf(buf, sizeof(buf), "%d", max_instance + 1);
|
||||
instance = dmuci_set_value_by_section(s, inst_opt, buf);
|
||||
*max_inst = instance;
|
||||
dmuci_set_value_by_section(s, inst_opt, buf);
|
||||
*max_inst = dmstrdup(instance);
|
||||
} else {
|
||||
dmasprintf(max_inst, "%d", max_instance);
|
||||
}
|
||||
|
|
@ -801,7 +803,8 @@ char *update_instance_alias(int action, char **last_inst, char **max_inst, void
|
|||
dmuci_get_value_by_section_string(s, alias_opt, &alias);
|
||||
if (alias[0] == '\0') {
|
||||
snprintf(buf, sizeof(buf), "cpe-%s", instance);
|
||||
alias = dmuci_set_value_by_section(s, alias_opt, buf);
|
||||
dmuci_set_value_by_section(s, alias_opt, buf);
|
||||
alias = dmstrdup(buf);
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "[%s]", alias);
|
||||
instance = dmstrdup(buf);
|
||||
|
|
|
|||
|
|
@ -474,20 +474,17 @@ int dmuci_change_packages(struct list_head *clist)
|
|||
}
|
||||
|
||||
/**** UCI SET *****/
|
||||
char *dmuci_set_value(char *package, char *section, char *option, char *value)
|
||||
int dmuci_set_value(char *package, char *section, char *option, char *value)
|
||||
{
|
||||
struct uci_ptr ptr = {0};
|
||||
|
||||
if (dmuci_lookup_ptr(uci_ctx, &ptr, package, section, option, value))
|
||||
return "";
|
||||
return -1;
|
||||
|
||||
if (uci_set(uci_ctx, &ptr) != UCI_OK)
|
||||
return "";
|
||||
return -1;
|
||||
|
||||
if (ptr.o)
|
||||
return dmstrdup(ptr.o->v.string); // MEM WILL BE FREED IN DMMEMCLEAN
|
||||
|
||||
return "";
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**** UCI ADD LIST *****/
|
||||
|
|
@ -519,10 +516,10 @@ int dmuci_del_list_value(char *package, char *section, char *option, char *value
|
|||
}
|
||||
|
||||
/****** UCI ADD *******/
|
||||
char *dmuci_add_section(char *package, char *stype, struct uci_section **s)
|
||||
int dmuci_add_section(char *package, char *stype, struct uci_section **s)
|
||||
{
|
||||
struct uci_ptr ptr = {0};
|
||||
char fname[128], *val = "";
|
||||
char fname[128];
|
||||
|
||||
*s = NULL;
|
||||
|
||||
|
|
@ -532,14 +529,16 @@ char *dmuci_add_section(char *package, char *stype, struct uci_section **s)
|
|||
if (fptr)
|
||||
fclose(fptr);
|
||||
else
|
||||
return val;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dmuci_lookup_ptr(uci_ctx, &ptr, package, NULL, NULL, NULL) == 0
|
||||
&& uci_add_section(uci_ctx, ptr.p, stype, s) == UCI_OK)
|
||||
val = dmstrdup((*s)->e.name);
|
||||
if (dmuci_lookup_ptr(uci_ctx, &ptr, package, NULL, NULL, NULL))
|
||||
return -1;
|
||||
|
||||
return val;
|
||||
if (uci_add_section(uci_ctx, ptr.p, stype, s) != UCI_OK)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**** UCI DELETE *****/
|
||||
|
|
@ -682,20 +681,17 @@ int dmuci_get_value_by_section_list(struct uci_section *s, char *option, struct
|
|||
}
|
||||
|
||||
/**** UCI SET by section pointer ****/
|
||||
char *dmuci_set_value_by_section(struct uci_section *s, char *option, char *value)
|
||||
int dmuci_set_value_by_section(struct uci_section *s, char *option, char *value)
|
||||
{
|
||||
struct uci_ptr up = {0};
|
||||
|
||||
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1)
|
||||
return "";
|
||||
return -1;
|
||||
|
||||
if (uci_set(uci_ctx, &up) != UCI_OK)
|
||||
return "";
|
||||
return -1;
|
||||
|
||||
if (up.o)
|
||||
return dmstrdup(up.o->v.string); // MEM WILL BE FREED IN DMMEMCLEAN
|
||||
|
||||
return "";
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**** UCI DELETE by section pointer *****/
|
||||
|
|
|
|||
|
|
@ -198,12 +198,12 @@ int dmuci_get_option_value_list_##UCI_PATH(char *package, char *section, char *o
|
|||
uci_ctx = save_uci_ctx; \
|
||||
return res; \
|
||||
}\
|
||||
char *dmuci_set_value_##UCI_PATH(char *package, char *section, char *option, char *value) \
|
||||
int dmuci_set_value_##UCI_PATH(char *package, char *section, char *option, char *value) \
|
||||
{\
|
||||
struct uci_context *save_uci_ctx; \
|
||||
save_uci_ctx = uci_ctx; \
|
||||
uci_ctx = uci_ctx_##UCI_PATH; \
|
||||
char *res = dmuci_set_value(package, section, option, value); \
|
||||
int res = dmuci_set_value(package, section, option, value); \
|
||||
uci_ctx = save_uci_ctx; \
|
||||
return res; \
|
||||
}\
|
||||
|
|
@ -225,14 +225,14 @@ int dmuci_del_list_value_##UCI_PATH(char *package, char *section, char *option,
|
|||
uci_ctx = save_uci_ctx; \
|
||||
return res; \
|
||||
}\
|
||||
char *dmuci_add_section_##UCI_PATH(char *package, char *stype, struct uci_section **s)\
|
||||
int dmuci_add_section_##UCI_PATH(char *package, char *stype, struct uci_section **s)\
|
||||
{\
|
||||
struct uci_context *save_uci_ctx; \
|
||||
save_uci_ctx = uci_ctx; \
|
||||
uci_ctx = uci_ctx_##UCI_PATH; \
|
||||
char *name = dmuci_add_section(package, stype, s); \
|
||||
int res = dmuci_add_section(package, stype, s); \
|
||||
uci_ctx = save_uci_ctx; \
|
||||
return name; \
|
||||
return res; \
|
||||
}\
|
||||
int dmuci_delete_##UCI_PATH(char *package, char *section, char *option, char *value) \
|
||||
{\
|
||||
|
|
@ -243,12 +243,12 @@ int dmuci_delete_##UCI_PATH(char *package, char *section, char *option, char *va
|
|||
uci_ctx = save_uci_ctx; \
|
||||
return res; \
|
||||
}\
|
||||
char *dmuci_set_value_by_section_##UCI_PATH(struct uci_section *s, char *option, char *value)\
|
||||
int dmuci_set_value_by_section_##UCI_PATH(struct uci_section *s, char *option, char *value)\
|
||||
{\
|
||||
struct uci_context *save_uci_ctx; \
|
||||
save_uci_ctx = uci_ctx; \
|
||||
uci_ctx = uci_ctx_##UCI_PATH; \
|
||||
char *res = dmuci_set_value_by_section(s, option, value); \
|
||||
int res = dmuci_set_value_by_section(s, option, value); \
|
||||
uci_ctx = save_uci_ctx; \
|
||||
return res; \
|
||||
}\
|
||||
|
|
@ -338,15 +338,15 @@ int dmuci_get_section_type(char *package, char *section, char **value);
|
|||
int dmuci_get_option_value_string(char *package, char *section, char *option, char **value);
|
||||
char *dmuci_get_option_value_fallback_def(char *package, char *section, char *option, char *default_value);
|
||||
int dmuci_get_option_value_list(char *package, char *section, char *option, struct uci_list **value);
|
||||
char *dmuci_set_value(char *package, char *section, char *option, char *value);
|
||||
int dmuci_set_value(char *package, char *section, char *option, char *value);
|
||||
int dmuci_add_list_value(char *package, char *section, char *option, char *value);
|
||||
int dmuci_del_list_value(char *package, char *section, char *option, char *value);
|
||||
char *dmuci_add_section(char *package, char *stype, struct uci_section **s);
|
||||
int dmuci_add_section(char *package, char *stype, struct uci_section **s);
|
||||
int dmuci_delete(char *package, char *section, char *option, char *value);
|
||||
int dmuci_get_value_by_section_string(struct uci_section *s, char *option, char **value);
|
||||
char *dmuci_get_value_by_section_fallback_def(struct uci_section *s, char *option, char *default_value);
|
||||
int dmuci_get_value_by_section_list(struct uci_section *s, char *option, struct uci_list **value);
|
||||
char *dmuci_set_value_by_section(struct uci_section *s, char *option, char *value);
|
||||
int dmuci_set_value_by_section(struct uci_section *s, char *option, char *value);
|
||||
int dmuci_delete_by_section(struct uci_section *s, char *option, char *value);
|
||||
int dmuci_delete_by_section_unnamed(struct uci_section *s, char *option, char *value);
|
||||
int dmuci_add_list_value_by_section(struct uci_section *s, char *option, char *value);
|
||||
|
|
@ -355,9 +355,9 @@ int dmuci_rename_section_by_section(struct uci_section *s, char *value);
|
|||
struct uci_section *dmuci_walk_section(char *package, char *stype, void *arg1, void *arg2, int cmp , int (*filter)(struct uci_section *s, void *value), struct uci_section *prev_section, int walk);
|
||||
|
||||
int dmuci_get_option_value_string_bbfdm(char *package, char *section, char *option, char **value);
|
||||
char *dmuci_set_value_bbfdm(char *package, char *section, char *option, char *value);
|
||||
char *dmuci_set_value_by_section_bbfdm(struct uci_section *s, char *option, char *value);
|
||||
char *dmuci_add_section_bbfdm(char *package, char *stype, struct uci_section **s);
|
||||
int dmuci_set_value_bbfdm(char *package, char *section, char *option, char *value);
|
||||
int dmuci_set_value_by_section_bbfdm(struct uci_section *s, char *option, char *value);
|
||||
int dmuci_add_section_bbfdm(char *package, char *stype, struct uci_section **s);
|
||||
int dmuci_delete_bbfdm(char *package, char *section, char *option, char *value);
|
||||
int dmuci_delete_by_section_unnamed_bbfdm(struct uci_section *s, char *option, char *value);
|
||||
int dmuci_delete_by_section_bbfdm(struct uci_section *s, char *option, char *value);
|
||||
|
|
|
|||
|
|
@ -217,16 +217,16 @@ static void test_bbf_api_uci(void **state)
|
|||
*/
|
||||
|
||||
// dmuci_set_value: test with correct section/option and wrong config name
|
||||
value = dmuci_set_value("netwo", "wan", "vendorid", "dg400prime");
|
||||
assert_string_equal(value, "");
|
||||
uci_res = dmuci_set_value("netwo", "wan", "vendorid", "dg400prime");
|
||||
assert_int_equal(uci_res, -1);
|
||||
|
||||
// dmuci_set_value: test with correct config/option and wrong section name
|
||||
value = dmuci_set_value("network", "wann", "vendorid", "dg400prime");
|
||||
assert_string_equal(value, "");
|
||||
uci_res = dmuci_set_value("network", "wann", "vendorid", "dg400prime");
|
||||
assert_int_equal(uci_res, -1);
|
||||
|
||||
// dmuci_set_value: test correct config/section/option
|
||||
value = dmuci_set_value("network", "wan", "vendorid", "dg400prime");
|
||||
assert_string_equal(value, "dg400prime");
|
||||
uci_res = dmuci_set_value("network", "wan", "vendorid", "dg400prime");
|
||||
assert_int_equal(uci_res, 0);
|
||||
uci_res = dmuci_commit_package("network");
|
||||
assert_int_equal(uci_res, 0);
|
||||
uci_res = dmuci_get_option_value_string("network", "wan", "vendorid", &value);
|
||||
|
|
@ -234,8 +234,8 @@ static void test_bbf_api_uci(void **state)
|
|||
assert_string_equal(value, "dg400prime");
|
||||
|
||||
// dmuci_set_value: test correct config/section/option
|
||||
value = dmuci_set_value("dropbear", "@dropbear[0]", "Port", "7845");
|
||||
assert_string_equal(value, "7845");
|
||||
uci_res = dmuci_set_value("dropbear", "@dropbear[0]", "Port", "7845");
|
||||
assert_int_equal(uci_res, 0);
|
||||
uci_res = dmuci_commit_package("dropbear");
|
||||
assert_int_equal(uci_res, 0);
|
||||
uci_res = dmuci_get_option_value_string("dropbear", "@dropbear[0]", "Port", &value);
|
||||
|
|
@ -247,8 +247,8 @@ static void test_bbf_api_uci(void **state)
|
|||
*/
|
||||
|
||||
// dmuci_add_section: test with only config name
|
||||
value = dmuci_add_section("network", "section_test", &uci_s);
|
||||
assert_string_not_equal(value, "");
|
||||
uci_res = dmuci_add_section("network", "section_test", &uci_s);
|
||||
assert_int_equal(uci_res, 0);
|
||||
assert_non_null(uci_s);
|
||||
uci_res = dmuci_commit_package("network");
|
||||
assert_int_equal(uci_res, 0);
|
||||
|
|
@ -313,8 +313,8 @@ static void test_bbf_api_uci(void **state)
|
|||
*/
|
||||
|
||||
// dmuci_set_value_by_section: test with correct option name
|
||||
value = dmuci_set_value_by_section(uci_s, "reqopts", "44");
|
||||
assert_string_equal(value, "44");
|
||||
uci_res = dmuci_set_value_by_section(uci_s, "reqopts", "44");
|
||||
assert_int_equal(uci_res, 0);
|
||||
uci_res = dmuci_get_value_by_section_string(uci_s, "reqopts", &value);
|
||||
assert_int_equal(uci_res, 0);
|
||||
assert_string_equal(value, "44");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue