Skip to content

Commit

Permalink
fix most space leaks in the LRTableMaker
Browse files Browse the repository at this point in the history
  • Loading branch information
krangelov committed Dec 23, 2023
1 parent ba19ff1 commit 31b52ad
Showing 1 changed file with 63 additions and 14 deletions.
77 changes: 63 additions & 14 deletions src/runtime/c/pgf/parser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct PgfLRTableMaker::CCat {

ref<PgfLRReduceArg> persist();

void suspend_item(Item *item);
void register_item(Item *item);

~CCat();
};

Expand Down Expand Up @@ -64,9 +67,14 @@ struct PgfLRTableMaker::Production {
// If there is no constructor, GCC will zero the object,
// while it has already been initialized in the new operator.
}

void operator delete(void *p) {
free(p);
}
};

struct PgfLRTableMaker::Item {
size_t ref_cnt; // how many CCat:s contain the item?
CCat* ccat;
object lin_obj;
ref<PgfSequence> seq;
Expand Down Expand Up @@ -106,6 +114,10 @@ struct PgfLRTableMaker::Item {
// If there is no constructor, GCC will zero the object,
// while it has already been initialized in the new operator.
}

void operator delete(void *p) {
free(p);
}
};

struct PgfLRTableMaker::CompareItem : std::less<Item*> {
Expand Down Expand Up @@ -168,12 +180,26 @@ ref<PgfLRReduceArg> PgfLRTableMaker::CCat::persist() {
return persistant;
}

void PgfLRTableMaker::CCat::suspend_item(Item *item) {
suspended.push_back(item);
if (item != NULL)
item->ref_cnt++;
}

void PgfLRTableMaker::CCat::register_item(Item *item) {
items.push_back(item); item->ref_cnt++;
}

PgfLRTableMaker::CCat::~CCat() {
for (Item *item : items) {
delete item;
item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
}
for (Item *item : suspended) {
delete item;
item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
}
for (Production *prod : prods) {
delete prod;
Expand Down Expand Up @@ -210,6 +236,7 @@ void *PgfLRTableMaker::Item::operator new(size_t size, CCat* ccat, ref<PgfConcrL
size_t ex_size = sizeof(Arg)*n_args+sizeof(size_t)*n_vars;

Item *item = (Item *) malloc(size+ex_size);
item->ref_cnt = 0;
item->ccat = ccat;
item->lin_obj = lin.tagged();
item->seq = *vector_elem(lin->seqs,seq_idx);
Expand All @@ -233,6 +260,7 @@ void *PgfLRTableMaker::Item::operator new(size_t size, ref<PgfConcrLincat> linca
lincat->n_lindefs*lincat->fields->len + index;

Item *item = (Item *) malloc(size+ex_size);
item->ref_cnt = 0;
item->ccat = NULL;
item->lin_obj = lincat.tagged();
item->seq = *vector_elem(lincat->seqs,seq_idx);
Expand All @@ -252,6 +280,7 @@ void *PgfLRTableMaker::Item::operator new(size_t size, CCat* ccat, Production *p
size_t ex_size = sizeof(Arg)*prod->args.count+sizeof(size_t)*prod->vals.count;

Item *item = (Item *) malloc(size+ex_size);
item->ref_cnt = 0;
item->ccat = ccat;
item->lin_obj = prod->lin.tagged();
item->seq_idx = prod->index*n_fields+lin_idx;
Expand All @@ -276,6 +305,7 @@ void *PgfLRTableMaker::Item::operator new(size_t size, Item *item, CCat *ccat, b
size_t ex_size = sizeof(Arg)*item->args.count+sizeof(size_t)*item->vals.count;

Item *new_item = (Item *) malloc(size+ex_size);
new_item->ref_cnt = 0;
new_item->ccat = item->ccat;
new_item->lin_obj = item->lin_obj;
new_item->seq = item->seq;
Expand Down Expand Up @@ -339,6 +369,7 @@ void *PgfLRTableMaker::Item::operator new(size_t size, Item *item, size_t lin_id
size_t ex_size = sizeof(Arg)*item->args.count+sizeof(size_t)*item->vals.count;

Item *new_item = (Item *) malloc(size+ex_size);
new_item->ref_cnt = 0;
new_item->ccat = item->ccat;
new_item->lin_obj = item->lin_obj;
new_item->seq = item->seq;
Expand Down Expand Up @@ -404,12 +435,20 @@ struct PgfLRTableMaker::State {

~State() {
for (Item *item : items) {
delete item;
item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
}

for (Item *item : completed) {
item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
}
}

void push_item(Item *item) {
items.push_back(item);
items.push_back(item); item->ref_cnt++;
push_heap(items.begin(), items.end(), compare_item);
}
};
Expand Down Expand Up @@ -468,7 +507,6 @@ PgfLRTableMaker::PgfLRTableMaker(ref<PgfAbstr> abstr, ref<PgfConcr> concr)

PgfLRTableMaker::~PgfLRTableMaker()
{
/*
for (auto p : states) {
delete p.second;
}
Expand All @@ -479,7 +517,7 @@ PgfLRTableMaker::~PgfLRTableMaker()

for (auto p : ccats2) {
delete p.second;
}*/
}
}

#if defined(DEBUG_STATE_CREATION) || defined(DEBUG_AUTOMATON)
Expand Down Expand Up @@ -673,7 +711,13 @@ void PgfLRTableMaker::symbol(State *state, Fold fold, Item *item, PgfSymbol sym)
if (fold == PROBE) {
item->ccat->productive = true;
}
if (item->ref_cnt == 0)
delete item;
break;
}
default:
if (item->ref_cnt == 0)
delete item;
}
}

Expand Down Expand Up @@ -720,12 +764,12 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, T cat, bool e
i--;
values[i].value++;
if (values[i].value < values[i].range) {
index += r->terms[i].factor;
index += values[i].factor;
i++;
break;
}

index -= (values[i].value-1) * r->terms[i].factor;
index -= (values[i].value-1) * values[i].factor;
values[i].value = 0;
}

Expand All @@ -743,7 +787,7 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, ref<PgfText>
ccat = new CCat(++ccat_id, NULL, lin_idx);
}
if (fold == PROBE) {
ccat->suspended.push_back(item);
ccat->suspend_item(item);
}
if (tmp == NULL) {
std::function<bool(ref<PgfAbsFun>)> f =
Expand All @@ -760,7 +804,7 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, ref<PgfText>
if (fold == PROBE) {
if (item->ccat != NULL && ccat->productive) {
item->ccat->productive = true;
item->ccat->items.push_back(item);
item->ccat->register_item(item);
}
} else {
if (ccat->productive) {
Expand Down Expand Up @@ -793,7 +837,7 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, CCat *ccat, b
new_ccat = new CCat(++ccat_id, ccat, lin_idx);
}
if (fold == PROBE) {
new_ccat->suspended.push_back(item);
new_ccat->suspend_item(item);
}
if (tmp == NULL) {
size_t n_prods = ccat->prods.size();
Expand All @@ -807,7 +851,7 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, CCat *ccat, b
if (fold == PROBE) {
if (item->ccat != NULL && new_ccat->productive) {
item->ccat->productive = true;
item->ccat->items.push_back(item);
item->ccat->register_item(item);
}
} else {
if (new_ccat->productive) {
Expand Down Expand Up @@ -873,8 +917,11 @@ void PgfLRTableMaker::complete(State *state, Fold fold, Item *item)
}
}
}

if (item->ref_cnt == 0)
delete item;
} else {
state->completed.push_back(item);
state->completed.push_back(item); item->ref_cnt++;

#if defined(DEBUG_AUTOMATON)
fprintf(stderr, "reduce ");
Expand Down Expand Up @@ -904,7 +951,9 @@ ref<PgfLRTable> PgfLRTableMaker::make()
#endif

process(state, INIT, item);
// delete item;
item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
}

for (auto &i : state->ccats1) {
Expand Down

0 comments on commit 31b52ad

Please sign in to comment.