Skip to content

Commit 119a6fc

Browse files
author
Felipe Zimmerle
committed
test-only: Placing a mutex while evaluating the pm operator
Performing an earlier optimization of the tree (before threads creation)
1 parent a2427df commit 119a6fc

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/operators/pm.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Pm::~Pm() {
4040

4141
free(m_p);
4242
m_p = NULL;
43+
pthread_mutex_destroy(&m_lock);
4344
}
4445

4546

@@ -86,8 +87,9 @@ bool Pm::evaluate(Transaction *transaction, Rule *rule,
8687
pt.parser = m_p;
8788
pt.ptr = NULL;
8889
const char *match = NULL;
89-
90+
pthread_mutex_lock(&m_lock);
9091
rc = acmp_process_quick(&pt, &match, input.c_str(), input.length());
92+
pthread_mutex_unlock(&m_lock);
9193
bool capture = rule && rule->getActionsByName("capture").size() > 0;
9294

9395
if (rc > 0 && transaction) {
@@ -114,6 +116,8 @@ bool Pm::init(const std::string &file, std::string *error) {
114116
std::istringstream *iss;
115117
const char *err = NULL;
116118

119+
pthread_mutex_init(&m_lock, NULL);
120+
117121
char *content = parse_pm_content(m_param.c_str(), m_param.length(), &err);
118122
if (content == NULL) {
119123
iss = new std::istringstream(m_param);
@@ -129,7 +133,9 @@ bool Pm::init(const std::string &file, std::string *error) {
129133
acmp_add_pattern(m_p, a.c_str(), NULL, NULL, a.length());
130134
}
131135

132-
acmp_prepare(m_p);
136+
while (m_p->is_failtree_done == 0) {
137+
acmp_prepare(m_p);
138+
}
133139

134140
if (content) {
135141
free(content);

src/operators/pm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class Pm : public Operator {
5555

5656
protected:
5757
ACMP *m_p;
58+
59+
private:
60+
pthread_mutex_t m_lock;
5861
};
5962

6063

src/utils/acmp.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ static void acmp_add_btree_leaves(acmp_btree_node_t *node, acmp_node_t *nodes[],
297297
if ((pos - lb) > 1) {
298298
left = lb + (pos - lb) / 2;
299299
node->left = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
300+
node->left->node = NULL;
301+
node->left->right = NULL;
302+
node->left->left = NULL;
303+
node->left->letter = 0;
300304
/* ENH: Check alloc succeded */
301305
node->left->node = nodes[left];
302306
node->left->letter = nodes[left]->letter;
@@ -307,6 +311,10 @@ static void acmp_add_btree_leaves(acmp_btree_node_t *node, acmp_node_t *nodes[],
307311
if ((rb - pos) > 1) {
308312
right = pos + (rb - pos) / 2;
309313
node->right = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
314+
node->right->node = NULL;
315+
node->right->right = NULL;
316+
node->right->left = NULL;
317+
node->right->letter = 0;
310318
/* ENH: Check alloc succeded */
311319
node->right->node = nodes[right];
312320
node->right->letter = nodes[right]->letter;
@@ -355,8 +363,12 @@ static void acmp_build_binary_tree(ACMP *parser, acmp_node_t *node) {
355363
nodes[i] = nodes[j];
356364
nodes[j] = tmp;
357365
}
358-
if (node->btree) { free (node->btree); node->btree = NULL; }
366+
if (node->btree != NULL) {
367+
free(node->btree);
368+
node->btree = NULL;
369+
}
359370
node->btree = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
371+
360372
/* ENH: Check alloc succeded */
361373
pos = count / 2;
362374
node->btree->node = nodes[pos];
@@ -365,7 +377,9 @@ static void acmp_build_binary_tree(ACMP *parser, acmp_node_t *node) {
365377
for (i = 0; i < count; i++) {
366378
if (nodes[i]->child != NULL) acmp_build_binary_tree(parser, nodes[i]);
367379
}
368-
free(nodes);
380+
if (nodes != NULL) {
381+
free(nodes);
382+
}
369383
}
370384

371385
/**
@@ -532,9 +546,11 @@ int acmp_process_quick(ACMPT *acmpt, const char **match, const char *data, size_
532546
const char *end;
533547
int offset = 0;
534548

549+
/*
535550
if (acmpt->parser->is_failtree_done == 0) {
536551
acmp_prepare(acmpt->parser);
537552
};
553+
*/
538554

539555
parser = acmpt->parser;
540556
if (acmpt->ptr == NULL) acmpt->ptr = parser->root_node;

0 commit comments

Comments
 (0)