Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed most of matching and all visible memory leaks
  • Loading branch information
gian committed Aug 2, 2011
1 parent 2999c31 commit d3abc26
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
@@ -1,6 +1,6 @@
AUTOMAKE_OPTIONS = subdir-objects
ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
SUBDIRS = src doc
SUBDIRS = src . tests doc

confdir = @prefix@/conf

Expand Down
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -14,6 +14,7 @@ AC_PREFIX_DEFAULT(/usr/local/bigmc)
AC_CONFIG_FILES([Makefile
src/Makefile
src/predicates/Makefile
tests/Makefile
doc/Makefile
])

Expand Down
3 changes: 3 additions & 0 deletions include/mc.h
Expand Up @@ -28,6 +28,7 @@ class mc {
unsigned long steps;
static map<string,query *> properties;
map<unsigned long,bool> checked;
static set<match *> match_discard;
public:
mc(bigraph *b);
~mc();
Expand All @@ -37,6 +38,8 @@ class mc {
static void add_property(string s, query *q);
bool check_properties(node *n);
static void *thread_wrapper( void *i );
static void match_mark_delete( match *m );
static void match_gc();
};

#endif
2 changes: 1 addition & 1 deletion include/parsenode.h
Expand Up @@ -55,7 +55,7 @@ class parsenode {
public:
int type;
parsenode();
~parsenode();
virtual ~parsenode();
virtual string to_string();
virtual bool is_valid();
virtual vector<parsenode *> get_children();
Expand Down
1 change: 1 addition & 0 deletions include/query.h
Expand Up @@ -102,6 +102,7 @@ class query_predicate : public query {
bool check(node *n);
int eval(node *n);
static void register_predicate(string name, predicate *p);
static void cleanup();
};

class query_scope : public query {
Expand Down
13 changes: 10 additions & 3 deletions src/Makefile.am
Expand Up @@ -3,17 +3,20 @@ AM_YFLAGS = -y -d
BUILT_SOURCES = bgparser.h

bin_PROGRAMS = bigmc
bigmc_SOURCES = bgparser.ypp \

noinst_LIBRARIES = libbigmc.a

libbigmc_a_SOURCES = bgparser.ypp \
bgscanner.ll \
parser.cpp \
report.cpp \
bigraph.cpp \
driver.cpp \
graph.cpp \
main.cpp \
match.cpp \
mc.cpp \
node.cpp \
globals.cpp \
parsenode.cpp \
predicate.cpp \
query.cpp \
Expand Down Expand Up @@ -44,6 +47,7 @@ bigmc_SOURCES = bgparser.ypp \
../include/matcher.h \
../include/subtree.h


noinst_HEADERS = ../include/bigmc.h \
../include/bigraph.h \
../include/driver.h \
Expand All @@ -61,8 +65,11 @@ noinst_HEADERS = ../include/bigmc.h \
../include/matcher.h \
../include/subtree.h

libbigmc_a_CPPFLAGS = -I../include
bigmc_CPPFLAGS = -I../include
bigmc_SOURCES = main.cpp
bigmc_LDADD = libbigmc.a

includes = ../include
SUBDIRS = predicates


3 changes: 3 additions & 0 deletions src/bgscanner.ll
Expand Up @@ -95,6 +95,9 @@ IDENTR [a-zA-Z0-9_]
"%outer" {
return OUTER;
}
"%name" {
return OUTER;
}
"%active" {
return ACTIVE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/bigraph.cpp
Expand Up @@ -193,7 +193,7 @@ bigraph *bigraph::apply_match(match *m) {
b->rules = rules;

// destroy the match -- we're done with it
delete m;
mc::match_mark_delete(m);
return b;
} else {
if(m->get_rule()->reactum->type != TREGION) {
Expand Down Expand Up @@ -236,7 +236,7 @@ bigraph *bigraph::apply_match(match *m) {
delete nr;
}

delete m;
mc::match_mark_delete(m);
return b;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/driver.cpp
Expand Up @@ -39,5 +39,7 @@ void driver::check(bigraph *b) {

delete m;

query_predicate::cleanup();

exit(0);
}
12 changes: 12 additions & 0 deletions src/globals.cpp
@@ -0,0 +1,12 @@
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <set>

#include <bigmc.h>

global_config global_cfg;


5 changes: 3 additions & 2 deletions src/main.cpp
Expand Up @@ -7,8 +7,6 @@ using namespace std;

#include <bigmc.h>

global_config global_cfg;

void print_usage(char **argv) {
fprintf(stderr,
"Usage: %s [options] <modelfile>\n"
Expand Down Expand Up @@ -128,6 +126,8 @@ void config_read() {

}
}

free(configfile);
}

int main(int argc, char**argv) {
Expand Down Expand Up @@ -218,6 +218,7 @@ int main(int argc, char**argv) {
}

parser::cleanup();
query_predicate::cleanup();

return 0;
}
15 changes: 6 additions & 9 deletions src/match.cpp
Expand Up @@ -24,13 +24,7 @@ using namespace std;
#include <set>
#include <iostream>

#include <config.h>
#include <globals.h>
#include <report.h>
#include <match.h>
#include <term.h>
#include <reactionrule.h>
#include <bigraph.h>
#include <bigmc.h>

#include <assert.h>

Expand All @@ -45,6 +39,7 @@ match::match(reactionrule *rl) {
rule = rl;
has_failed = false;
has_succeeded = false;
mc::match_mark_delete(this);
}

match::~match() {
Expand Down Expand Up @@ -110,7 +105,7 @@ name match::get_name(name n) {
set<match *> match::failure() {
has_failed = true;
has_succeeded = false;
delete this;
mc::match_mark_delete(this);
return set<match *>();
}

Expand Down Expand Up @@ -180,6 +175,8 @@ string match::to_string() {
s += " Failed";
}

s += "\nParameters:\n";

for(map<int,term*>::iterator i = parameters.begin(); i!=parameters.end(); i++) {
std::stringstream p;
p << i->first;
Expand Down Expand Up @@ -242,7 +239,7 @@ wide_match::~wide_match() {
list<match *>::iterator i = submatches.begin();

while(i != submatches.end()) {
delete *i;
mc::match_mark_delete(*i);
i++;
}

Expand Down
94 changes: 62 additions & 32 deletions src/matcher.cpp
Expand Up @@ -44,6 +44,8 @@ set<match *> matcher::try_match(prefix *t, prefix *r, match *m) {
" against redex: " << r->to_string() << endl;
}

assert(m != NULL);

if(t->get_control() == r->get_control()) {

if(m->root == NULL && t->active_context()) {
Expand Down Expand Up @@ -122,6 +124,7 @@ set<match *> matcher::try_match(parallel *t, prefix *r, match *m) {
}

set<match *> matcher::try_match(parallel *t, parallel *r, match *m) {
// FIXME: This approach can be optimised considerably!
if(DEBUG) {
rinfo("matcher::try_match") << "matching: " << t->to_string() <<
" against redex: " << r->to_string() << endl;
Expand All @@ -131,6 +134,18 @@ set<match *> matcher::try_match(parallel *t, parallel *r, match *m) {
set<term *> rch = r->get_children();
map<term *, vector<set<match *> > > matches;

hole *has_hole = NULL; // Is there a top level hole? e.g. A | B | $0
// Something like A.$0 | B | C does not count.
// This will be the hole term itself, or NULL.

for(set<term *>::iterator i = rch.begin(); i != rch.end(); i++) {
if((*i)->type == THOLE) {
has_hole = (hole *)*i;
rch.erase(i);
break;
}
}

if(rch.size()-1 > tch.size())
return m->failure();

Expand All @@ -142,7 +157,6 @@ set<match *> matcher::try_match(parallel *t, parallel *r, match *m) {

m->root = t;
}

m->add_match(r,t);

int sum = rch.size() * tch.size();
Expand All @@ -162,58 +176,74 @@ set<match *> matcher::try_match(parallel *t, parallel *r, match *m) {
set<match *> res;

do {
cout << "PERM: ";
for(int i = 0; i<xdim; i++) {
cout << " " << cand[i];
}
cout << endl;

int k = 0;
int succ = 0;
int holecnt = 0;

match *nn = m->clone();

for(set<term *>::iterator i = rch.begin(); i != rch.end(); i++) {
if((*i)->type == THOLE) {
if(xdim > ydim && holecnt == 0) {
// There are "extra" things lying around,
// push these all into the hole.
set<term*> nch;

nch.insert(cand[xdim-1-k++]);

for(int l = 0; l<xdim-ydim; l++) {
nch.insert(cand[l]);
}

parallel *np = new parallel(nch);
nn->add_param(((hole*)(*i))->index, np);
succ++;
holecnt++;
continue;
} else if (xdim == ydim || holecnt > 0) {
nn->add_param(((hole*)(*i))->index, cand[xdim-1-k++]);
succ++;
continue;
} else {
nn->failure();
break;
}
rerror("matcher::try_match") << "A hole remains at the top-level in the redex" <<
". This is a bug. Please report it." << endl;
exit(1);
}

set<match*> mm = try_match(cand[xdim-1-k++],*i,nn);

if(nn->has_failed) {
delete nn;
mm.clear();
if(mm.size() == 0) {
//delete nn;
//mm.clear();
nn->failure();
break;
}

nn->has_succeeded = false;

res = match::merge(res,mm);
//res = match::merge(res,mm);

succ++;
}

if(succ < ydim) {
cout << "fail\n";
continue;
}

cout << "success\n";

if(has_hole != NULL) {
if(xdim - ydim > 1) {
// There are "extra" things lying around,
// push these all into the hole.
// The previous loop has matched cand[size - 1] ... cand[size - 1 - k]
// elements and has incremented k once more.
// Therefore we have cand[0] ... cand[size - k] elements to put in the hole.
set<term*> nch;

for(int l = 0; l<xdim-k; l++) {
nch.insert(cand[l]);
succ++;
}

parallel *np = new parallel(nch);
nn->add_param(has_hole->index, np);
} else if (xdim - ydim == 1) {
nn->add_param(has_hole->index, cand[0]);
succ++;
} else {
nn->add_param(has_hole->index, new nil());
}
}


if(succ >= ydim) {
res.insert(nn);
}
}
} while(next_permutation(cand, cand+xdim));

if(res.size() == 0) return m->failure();
Expand Down

0 comments on commit d3abc26

Please sign in to comment.