集成支持expr_id的bool matcher

This commit is contained in:
zhengchao
2021-03-23 17:17:39 +08:00
parent 0eff6faae6
commit e3b3288dc1
4 changed files with 52 additions and 32 deletions

View File

@@ -895,6 +895,7 @@ static struct bool_matcher* Maat_hierarchy_build_bool_matcher(struct Maat_hierar
//some compile may have zero groups, e.g. default policy.
if(j==(size_t)compile->declared_clause_num&&j>0)
{
bool_expr_array[expr_cnt].expr_id=compile->compile_id;
bool_expr_array[expr_cnt].user_tag=compile;
bool_expr_array[expr_cnt].item_num=j;
expr_cnt++;
@@ -1338,6 +1339,7 @@ int Maat_hierarchy_region_compile(struct Maat_hierarchy_compile_mid* mid, int is
int bool_match_ret=0, i=0;
struct Maat_hierarchy* hier=mid->ref_hier;
struct Maat_hierarchy_compile* compile_array[ud_array_sz];
struct bool_expr_match expr_match[ud_array_sz];
size_t r_in_c_cnt=0, this_scan_region_hits=mid->this_scan_region_hit_cnt;
size_t ud_result_cnt=0;
@@ -1349,9 +1351,11 @@ int Maat_hierarchy_region_compile(struct Maat_hierarchy_compile_mid* mid, int is
pthread_rwlock_rdlock(&hier->rwlock);
bool_match_ret=bool_matcher_match(hier->bm, mid->thread_num,
(unsigned long long*)utarray_eltptr(mid->_all_hit_clause_array, 0), utarray_len(mid->_all_hit_clause_array),
(void**)compile_array, ud_array_sz);
expr_match, ud_array_sz);
for(i=0; i<bool_match_ret; i++)
{
compile_array[i]=(struct Maat_hierarchy_compile*)expr_match[i].user_tag;
assert((unsigned long long)compile_array[i]->compile_id==expr_match[i].expr_id);
r_in_c_cnt=Maat_hierarchy_compile_mid_update_by_compile(mid, compile_array[i]);
if(compile_array[i]->not_clause_cnt>0 && !is_last_compile)
{

View File

@@ -12,7 +12,7 @@ struct thread_local_data_t
{
unsigned int mapped_ids[MAX_ARRAY_SIZE];
unsigned int used_cells[MAX_ARRAY_SIZE];
void * cached_results[MAX_ARRAY_SIZE];
bool_expr_match cached_results[MAX_ARRAY_SIZE];
unsigned char * multiexpr_bitmap;
unsigned int * singlexpr_bitmap;
};
@@ -22,7 +22,7 @@ struct bool_matcher
unsigned int max_thread_num;
unsigned int bool_expr_num;
unsigned int multi_expr_num;
void ** bool_expr_ids;
bool_expr_match * bool_expr_ids;
unsigned char * multi_expr_size;
unsigned char * multi_expr_mask;
unsigned int bool_item_id_num;
@@ -78,8 +78,8 @@ struct bool_matcher * bool_matcher_new(struct bool_expr * exprs, size_t expr_num
matcher->bool_expr_num=(unsigned int)expr_num;
matcher->multi_expr_num=I;
matcher->bool_expr_ids=new void *[expr_num];
mem_bytes+=(unsigned int)expr_num*sizeof(void *);
matcher->bool_expr_ids=new bool_expr_match[expr_num];
mem_bytes+=(unsigned int)expr_num*sizeof(bool_expr_match);
matcher->multi_expr_size=new unsigned char[matcher->multi_expr_num+1];
mem_bytes+=(matcher->multi_expr_num+1)*sizeof(unsigned char);
@@ -105,7 +105,8 @@ struct bool_matcher * bool_matcher_new(struct bool_expr * exprs, size_t expr_num
unsigned int count=0;
for(unsigned int i=0; i<expr_num; i++)
{
matcher->bool_expr_ids[i]=exprs[i].user_tag;
matcher->bool_expr_ids[i].expr_id=exprs[i].expr_id;
matcher->bool_expr_ids[i].user_tag=exprs[i].user_tag;
if(i<matcher->multi_expr_num)
{
matcher->multi_expr_size[i]=(unsigned int)exprs[i].item_num;
@@ -160,7 +161,14 @@ struct bool_matcher * bool_matcher_new(struct bool_expr * exprs, size_t expr_num
return matcher;
}
int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, const unsigned long long * item_ids, size_t item_num, void ** result, size_t size)
int res_comp(const void * lhs, const void * rhs)
{
bool_expr_match * _lhs=(bool_expr_match *)lhs;
bool_expr_match * _rhs=(bool_expr_match *)rhs;
return (_lhs->expr_id<_rhs->expr_id) ? 1 : -1;
}
int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, const unsigned long long * item_ids, size_t item_num, struct bool_expr_match * results, size_t n_result)
{
if(matcher==NULL) return -1;
if(thread_id>=matcher->max_thread_num) return -1;
@@ -238,7 +246,7 @@ int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, co
}
unsigned int r=0;
void ** cached_results=matcher->thread_data[thread_id].cached_results;
bool_expr_match * cached_results=matcher->thread_data[thread_id].cached_results;
for(unsigned int i=0; i<used_num; i++)
{
@@ -260,15 +268,15 @@ int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, co
}
}
sort(cached_results, cached_results+r);
qsort(cached_results, r, sizeof(bool_expr_match), res_comp);
int I=0;
for(unsigned int J=0; J<r; ++J)
{
if(I==0 || cached_results[J]!=result[I-1])
if(I==0 || cached_results[J].expr_id!=results[I-1].expr_id)
{
if(I==(int)size) return I;
result[I++]=cached_results[J];
if(I==(int)n_result) return I;
results[I++]=cached_results[J];
}
}