fix leak memory

This commit is contained in:
liuwentan
2023-04-04 09:31:20 +08:00
parent a13af5c699
commit d3d19a4fe9
11 changed files with 46 additions and 32 deletions

View File

@@ -43,7 +43,7 @@ struct ex_data_runtime;
/* ex_data_runtime API */
struct ex_data_runtime *
ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn,
struct maat_garbage_bin *garbage_bin, struct log_handle *logger);
struct log_handle *logger);
void ex_data_runtime_free(struct ex_data_runtime *ex_data_rt);
void ex_data_runtime_commit(struct ex_data_runtime *ex_data_rt);

View File

@@ -11,6 +11,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <linux/limits.h>
#include "maat_utils.h"
@@ -438,7 +439,9 @@ void maat_free(struct maat *maat_instance)
}
void *ret = NULL;
while (0 == maat_instance->is_running) {
usleep(500 * 1000);
}
maat_instance->is_running = 0;
pthread_join(maat_instance->cfg_mon_thread, &ret);
}

View File

@@ -189,7 +189,7 @@ void *bool_plugin_runtime_new(void *bool_plugin_schema, int max_thread_num,
bool_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id,
ex_container_free,
garbage_bin, logger);
logger);
bool_plugin_rt->ref_garbage_bin = garbage_bin;
bool_plugin_rt->logger = logger;

View File

@@ -858,7 +858,7 @@ int compare_literal_id(const void *pa, const void *pb)
struct maat_literal_id *la = (struct maat_literal_id *)pa;
struct maat_literal_id *lb = (struct maat_literal_id *)pb;
int ret = la->vtable_id - lb->vtable_id;
long long ret = la->vtable_id - lb->vtable_id;
if (0 == ret) {
ret = la->group_id - lb->group_id;
}
@@ -927,7 +927,7 @@ maat_clause_hash_fetch_clause(struct compile_runtime *compile_rt,
HASH_FIND(hh, compile_rt->clause_by_literals_hash, literal_ids,
n_literal_id * sizeof(struct maat_literal_id), clause);
if (!clause) {
if (NULL == clause) {
clause = ALLOC(struct maat_clause, 1);
clause->clause_id = maat_runtime_get_sequence(compile_rt->ref_maat_rt, "clause_id");
clause->n_literal_id = n_literal_id;

View File

@@ -25,7 +25,6 @@ struct ex_data_runtime {
size_t cache_size;
struct rcu_hash_table *htable;
struct maat_garbage_bin *ref_garbage_bin;
int table_id;
struct log_handle *logger;
@@ -40,10 +39,9 @@ UT_icd ut_cache_row_icd = {sizeof(char*), NULL, NULL, cache_row_free};
struct ex_data_runtime *
ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn,
struct maat_garbage_bin *garbage_bin, struct log_handle *logger)
struct log_handle *logger)
{
if (NULL == data_free_fn || NULL == garbage_bin ||
NULL == logger) {
if (NULL == data_free_fn || NULL == logger) {
return NULL;
}
@@ -52,7 +50,6 @@ ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn,
utarray_new(ex_data_rt->cache_rows, &ut_cache_row_icd);
ex_data_rt->htable = rcu_hash_new(data_free_fn);
ex_data_rt->table_id = table_id;
ex_data_rt->ref_garbage_bin = garbage_bin;
ex_data_rt->logger = logger;
return ex_data_rt;
@@ -203,23 +200,24 @@ struct ex_container *ex_container_new(void *ex_data, void *custom_data)
void ex_container_free(void *schema, void *data)
{
/* schema is NULL if not call ex_data_runtime_set_ex_container_schema */
if (NULL == schema || NULL == data) {
if (NULL == data) {
return;
}
struct ex_container *ex_container = (struct ex_container *)data;
if (ex_container->custom_data != NULL) {
FREE(ex_container->custom_data);
}
struct ex_container_schema *container_schema = (struct ex_container_schema *)schema;
if (container_schema != NULL && container_schema->ex_schema != NULL) {
long argl = container_schema->ex_schema->argl;
void *argp = container_schema->ex_schema->argp;
if (ex_container->ex_data != NULL
&& container_schema->ex_schema->free_func != NULL) {
if (ex_container->ex_data != NULL && container_schema->ex_schema->free_func != NULL) {
container_schema->ex_schema->free_func(container_schema->table_id,
&(ex_container->ex_data), argl, argp);
}
if (ex_container->custom_data != NULL) {
FREE(ex_container->custom_data);
}
FREE(ex_container);

View File

@@ -173,7 +173,7 @@ void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num,
struct fqdn_plugin_runtime *fqdn_plugin_rt = ALLOC(struct fqdn_plugin_runtime, 1);
fqdn_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free,
garbage_bin, logger);
logger);
fqdn_plugin_rt->ref_garbage_bin = garbage_bin;
fqdn_plugin_rt->logger = logger;

View File

@@ -403,6 +403,11 @@ void ip_runtime_free(void *ip_runtime)
ip_rt->ip_matcher = NULL;
}
if (ip_rt->intval_matcher != NULL) {
interval_matcher_free(ip_rt->intval_matcher);
ip_rt->intval_matcher = NULL;
}
if (ip_rt->htable != NULL) {
rcu_hash_free(ip_rt->htable);
ip_rt->htable = NULL;
@@ -614,6 +619,10 @@ int ip_runtime_commit(void *ip_runtime, const char *table_name)
FREE(rules);
}
if (intval_rules != NULL) {
FREE(intval_rules);
}
if (ex_data_array != NULL) {
FREE(ex_data_array);
}

View File

@@ -358,7 +358,7 @@ void *ip_plugin_runtime_new(void *ip_plugin_schema, int max_thread_num,
struct ip_plugin_runtime *ip_plugin_rt = ALLOC(struct ip_plugin_runtime, 1);
ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free,
garbage_bin, logger);
logger);
ip_plugin_rt->ref_garbage_bin = garbage_bin;
ip_plugin_rt->logger = logger;

View File

@@ -267,7 +267,7 @@ void *plugin_runtime_new(void *plugin_schema, int max_thread_num,
struct plugin_runtime *plugin_rt = ALLOC(struct plugin_runtime, 1);
plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id,
ex_container_free,
garbage_bin, logger);
logger);
plugin_rt->ref_garbage_bin = garbage_bin;
plugin_rt->logger = logger;

View File

@@ -29,7 +29,7 @@ void ex_data_new_cb(const char *table_name, int table_id, const char *key, const
int valid = 0, tag = 0;
int ret = sscanf(table_line, "%d\t%s\t%s%d\t%d", &(u->id), u->ip_addr, u->name, &valid, &tag);
EXPECT_EQ(ret, 5);
u->ref_cnt = 1;
u->ref_cnt = 0;
*ad = u;
(*counter)++;
}
@@ -38,8 +38,11 @@ void ex_data_free_cb(int table_id, void **ad, long argl, void *argp)
{
struct user_info *u = (struct user_info *)(*ad);
if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0)) {
printf("yes, free(u)\n");
free(u);
*ad = NULL;
} else {
printf("u->ref_cnt:%d\n", u->ref_cnt);
}
}
@@ -56,8 +59,7 @@ TEST(EXDataRuntime, Update) {
ASSERT_GT(table_id, 0);
int ex_data_counter = 0;
struct maat_garbage_bin *garbage_bin = maat_garbage_bin_new(10);
struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, garbage_bin, g_logger);
struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, g_logger);
struct ex_data_schema *ex_schema = ex_data_schema_new(ex_data_new_cb, ex_data_free_cb, ex_data_dup_cb,
0, &ex_data_counter);
@@ -87,15 +89,17 @@ TEST(EXDataRuntime, Update) {
struct user_info *info = (struct user_info *)res_data1;
EXPECT_EQ(0, strcmp(info->name, "mahuateng"));
EXPECT_EQ(info->id, 1);
ex_data_free_cb(table_id, (void **)&res_data1, 0, NULL);
ex_container = NULL;
void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.2", 11);
EXPECT_TRUE(res_data2 != NULL);
info = (struct user_info *)res_data2;
EXPECT_EQ(0, strcmp(info->name, "liyanhong"));
EXPECT_EQ(info->id, 2);
ex_data_free_cb(table_id, (void **)&res_data2, 0, NULL);
maat_garbage_bin_free(garbage_bin);
ex_data_runtime_free(ex_data_rt);
ex_data_schema_free(ex_schema);
}

View File

@@ -2186,7 +2186,7 @@ void fqdn_plugin_ex_new_cb(const char *table_name, int table_id, const char *key
EXPECT_EQ(ret, 0);
sscanf(table_line + column_offset, "catid=%d", &ud->catid);
ud->ref_cnt = 1;
ud->ref_cnt = 0;
*ad = ud;
(*counter)++;
}
@@ -2268,7 +2268,7 @@ void bool_plugin_ex_new_cb(const char *table_name, int table_id, const char *key
ud->name = (char *)malloc(column_len+1);
memcpy(ud->name, table_line+column_offset, column_len);
ud->ref_cnt = 1;
ud->ref_cnt = 0;
*ad = ud;
(*counter)++;
}
@@ -3946,7 +3946,7 @@ void plugin_ex_new_cb(const char *table_name, int table_id, const char *key,
int valid = 0, tag = 0;
int ret = sscanf(table_line, "%d\t%s\t%s%d\t%d", &(u->id), u->ip_addr, u->name, &valid, &tag);
EXPECT_EQ(ret, 5);
u->ref_cnt = 1;
u->ref_cnt = 0;
*ad = u;
(*counter)++;
}