diff --git a/src/maat_compile.c b/src/maat_compile.c index d1a2432..afc022b 100644 --- a/src/maat_compile.c +++ b/src/maat_compile.c @@ -139,8 +139,7 @@ struct maat_compile { int not_clause_cnt; long long compile_id; char table_name[MAX_NAME_STR_LEN]; - void *user_data; - void (*user_data_free)(void *); + void *user_data; // compile_rule struct maat_clause_state clause_states[MAX_ITEMS_PER_BOOL_EXPR]; }; @@ -185,27 +184,57 @@ static struct maat_compile *maat_compile_new(long long compile_id) } static int maat_compile_set(struct maat_compile *compile, const char *table_name, - int declared_clause_num, void *user_data, - void (*user_data_free)(void *)) + int declared_clause_num, void *user_data) { - if (user_data != NULL && NULL == user_data_free) { - return -1; - } + if (NULL == compile) { + return -1; + } memset(compile->table_name, 0, sizeof(compile->table_name)); memcpy(compile->table_name, table_name, sizeof(compile->table_name)); compile->declared_clause_num = declared_clause_num; compile->user_data = user_data; - compile->user_data_free = user_data_free; return 0; } +static void rule_ex_data_free(int table_id, void **ex_data, + const struct ex_data_schema *ex_schema) +{ + ex_schema->free_func(table_id, ex_data, ex_schema->argl, ex_schema->argp); +} + +#define COMPILE_RULE_MAGIC 0x1a2b3c4d +static void compile_rule_free(struct compile_rule *compile_rule) +{ + struct compile_schema *schema = compile_rule->ref_schema; + assert(compile_rule->magic_num == COMPILE_RULE_MAGIC); + + if (1 == schema->set_flag) { + rule_ex_data_free(schema->table_id, compile_rule->ex_data, + &(schema->ex_schema)); + *compile_rule->ex_data = NULL; + } + + if (compile_rule->ex_data != NULL) { + FREE(compile_rule->ex_data); + } + + compile_rule->declared_clause_num = -1; + + if (compile_rule->table_line != NULL) { + FREE(compile_rule->table_line); + } + + FREE(compile_rule); +} + static void maat_compile_free(struct maat_compile *compile) { struct maat_clause_state *clause_state = NULL; - if (compile->user_data && compile->user_data_free) { - compile->user_data_free(compile->user_data); + + if (compile->user_data != NULL) { + compile_rule_free(compile->user_data); compile->user_data = NULL; } @@ -277,12 +306,6 @@ static void *rule_ex_data_new(const char *table_name, int table_id, return ex_data; } -static void rule_ex_data_free(int table_id, void **ex_data, - const struct ex_data_schema *ex_schema) -{ - ex_schema->free_func(table_id, ex_data, ex_schema->argl, ex_schema->argp); -} - static void rule_ex_data_new_cb(void *user_data, void *param, const char *table_name, int table_id) { @@ -1170,7 +1193,6 @@ static size_t maat_compile_bool_matcher_match(struct compile_runtime *compile_rt return ud_result_cnt; } -#define COMPILE_RULE_MAGIC 0x1a2b3c4d static struct compile_rule * compile_rule_new(struct compile_item *compile_item, struct compile_schema *schema, const char *table_name, const char *table_line) @@ -1220,30 +1242,6 @@ static struct compile_rule *compile_rule_clone(struct compile_rule *rule) return new_rule; } -static void compile_rule_free(struct compile_rule *compile_rule) -{ - struct compile_schema *schema = compile_rule->ref_schema; - assert(compile_rule->magic_num == COMPILE_RULE_MAGIC); - - if (1 == schema->set_flag) { - rule_ex_data_free(schema->table_id, compile_rule->ex_data, - &(schema->ex_schema)); - *compile_rule->ex_data = NULL; - } - - if (compile_rule->ex_data != NULL) { - FREE(compile_rule->ex_data); - } - - compile_rule->declared_clause_num = -1; - - if (compile_rule->table_line != NULL) { - FREE(compile_rule->table_line); - } - - FREE(compile_rule); -} - static struct maat_compile * maat_compile_clone(struct maat_compile *compile, int deep_copy) { @@ -1255,7 +1253,6 @@ maat_compile_clone(struct maat_compile *compile, int deep_copy) new_compile->declared_clause_num = compile->declared_clause_num; memcpy(new_compile->table_name, compile->table_name, sizeof(new_compile->table_name)); new_compile->not_clause_cnt = compile->not_clause_cnt; - new_compile->user_data_free = compile->user_data_free; if (1 == deep_copy && compile->user_data != NULL) { new_compile->user_data = compile_rule_clone((struct compile_rule *)compile->user_data); } @@ -1742,14 +1739,12 @@ static int compile_runtime_add_compile(struct compile_runtime *compile_rt, ******************************************************************/ /* compile has group2compile_table info, so set compile_table info */ - maat_compile_set(compile, table_name, compile_rule->declared_clause_num, - compile_rule, (void (*)(void *))compile_rule_free); + maat_compile_set(compile, table_name, compile_rule->declared_clause_num, compile_rule); } else { // compile neither in effective hash nor in updating hash compile = maat_compile_new(compile_rule->compile_id); assert(compile != NULL); - maat_compile_set(compile, table_name, compile_rule->declared_clause_num, - compile_rule, (void (*)(void *))compile_rule_free); + maat_compile_set(compile, table_name, compile_rule->declared_clause_num, compile_rule); rcu_hash_add(compile_rt->cfg_hash, (char *)&compile_id, sizeof(long long), compile); } } else { @@ -1771,15 +1766,13 @@ static int compile_runtime_add_compile(struct compile_runtime *compile_rt, rcu_hash_del(compile_rt->cfg_hash, (char *)&compile_id, sizeof(long long)); /* copy_compile has group2compile_table info, so set compile_table info */ - maat_compile_set(copy_compile, table_name, compile_rule->declared_clause_num, - compile_rule, (void (*)(void *))compile_rule_free); + maat_compile_set(copy_compile, table_name, compile_rule->declared_clause_num, compile_rule); /* add copy_compile to rcu hash */ rcu_hash_add(compile_rt->cfg_hash, (char *)&compile_id, sizeof(long long), copy_compile); } else { compile = maat_compile_new(compile_rule->compile_id); assert(compile != NULL); - maat_compile_set(compile, table_name, compile_rule->declared_clause_num, - compile_rule, (void (*)(void *))compile_rule_free); + maat_compile_set(compile, table_name, compile_rule->declared_clause_num, compile_rule); rcu_hash_add(compile_rt->cfg_hash, (char *)&compile_id, sizeof(long long), compile); } } @@ -1787,6 +1780,16 @@ static int compile_runtime_add_compile(struct compile_runtime *compile_rt, return 0; } +void garbage_compile_rule_free(void *data, void *arg) +{ + struct compile_rule *compile_rule = (struct compile_rule *)data; + if (NULL == compile_rule) { + return; + } + + compile_rule_free(compile_rule); +} + static void compile_runtime_del_compile(struct compile_runtime *compile_rt, long long compile_id) { @@ -1805,8 +1808,9 @@ static void compile_runtime_del_compile(struct compile_runtime *compile_rt, 2. rcu_hash_commit(htable) ==> undo because it's in updating hash, we can modify it directly ******************************************************************/ - if (compile->user_data_free && compile->user_data) { - compile->user_data_free(compile->user_data); + if (compile->user_data != NULL) { + maat_garbage_bagging(compile_rt->ref_garbage_bin, compile->user_data, NULL, + garbage_compile_rule_free); compile->user_data = NULL; } diff --git a/src/maat_expr.c b/src/maat_expr.c index e5a7c8f..3b2b488 100644 --- a/src/maat_expr.c +++ b/src/maat_expr.c @@ -64,7 +64,6 @@ struct expr_item { int is_hexbin; int is_case_sensitive; void *user_data; - void (*user_data_free)(void *data); int district_id; }; @@ -453,10 +452,8 @@ static void expr_item_free(struct expr_item *item) return; } - if (item->user_data != NULL && - item->user_data_free != NULL) { - item->user_data_free(item->user_data); - item->user_data = NULL; + if (item->user_data != NULL) { + FREE(item->user_data); } FREE(item); @@ -805,7 +802,6 @@ int expr_runtime_update(void *expr_runtime, void *expr_schema, *item_district_id = expr_item->district_id; expr_item->user_data = item_district_id; - expr_item->user_data_free = free; } int ret = expr_runtime_update_row(expr_rt, (char *)&item_id, sizeof(long long), diff --git a/src/maat_flag.c b/src/maat_flag.c index e645d0a..6056355 100644 --- a/src/maat_flag.c +++ b/src/maat_flag.c @@ -40,7 +40,6 @@ struct flag_item { long long flag; long long flag_mask; void *user_data; - void (*user_data_free)(void *data); int district_id; }; @@ -163,9 +162,8 @@ static void flag_item_free(struct flag_item *item) return; } - if (item->user_data != NULL && item->user_data_free != NULL) { - item->user_data_free(item->user_data); - item->user_data = NULL; + if (item->user_data != NULL) { + FREE(item->user_data); } FREE(item); @@ -432,7 +430,6 @@ int flag_runtime_update(void *flag_runtime, void *flag_schema, *item_district_id = flag_item->district_id; flag_item->user_data = item_district_id; - flag_item->user_data_free = free; } int ret = flag_runtime_update_row(flag_rt, (char *)&item_id, sizeof(long long), diff --git a/src/maat_interval.c b/src/maat_interval.c index c738ef4..8ac4157 100644 --- a/src/maat_interval.c +++ b/src/maat_interval.c @@ -37,7 +37,6 @@ struct interval_item { int low_bound; int up_bound; void *user_data; - void (*user_data_free)(void *data); int district_id; }; @@ -160,9 +159,8 @@ static void interval_item_free(struct interval_item *item) return; } - if (item->user_data != NULL && item->user_data_free != NULL) { - item->user_data_free(item->user_data); - item->user_data = NULL; + if (item->user_data != NULL) { + FREE(item->user_data); } FREE(item); @@ -433,7 +431,6 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema, *item_district_id = interval_item->district_id; interval_item->user_data = item_district_id; - interval_item->user_data_free = free; } int ret = interval_runtime_update_row(interval_rt, (char *)&item_id, sizeof(long long),