fix most space leaks in the LRTableMaker

This commit is contained in:
Krasimir Angelov
2023-12-23 15:31:28 +01:00
parent ba19ff1f63
commit 31b52adfa7

View File

@@ -32,6 +32,9 @@ struct PgfLRTableMaker::CCat {
ref<PgfLRReduceArg> persist(); ref<PgfLRReduceArg> persist();
void suspend_item(Item *item);
void register_item(Item *item);
~CCat(); ~CCat();
}; };
@@ -64,9 +67,14 @@ struct PgfLRTableMaker::Production {
// If there is no constructor, GCC will zero the object, // If there is no constructor, GCC will zero the object,
// while it has already been initialized in the new operator. // while it has already been initialized in the new operator.
} }
void operator delete(void *p) {
free(p);
}
}; };
struct PgfLRTableMaker::Item { struct PgfLRTableMaker::Item {
size_t ref_cnt; // how many CCat:s contain the item?
CCat* ccat; CCat* ccat;
object lin_obj; object lin_obj;
ref<PgfSequence> seq; ref<PgfSequence> seq;
@@ -106,6 +114,10 @@ struct PgfLRTableMaker::Item {
// If there is no constructor, GCC will zero the object, // If there is no constructor, GCC will zero the object,
// while it has already been initialized in the new operator. // while it has already been initialized in the new operator.
} }
void operator delete(void *p) {
free(p);
}
}; };
struct PgfLRTableMaker::CompareItem : std::less<Item*> { struct PgfLRTableMaker::CompareItem : std::less<Item*> {
@@ -168,12 +180,26 @@ ref<PgfLRReduceArg> PgfLRTableMaker::CCat::persist() {
return persistant; 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() { PgfLRTableMaker::CCat::~CCat() {
for (Item *item : items) { for (Item *item : items) {
delete item; item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
} }
for (Item *item : suspended) { for (Item *item : suspended) {
delete item; item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
} }
for (Production *prod : prods) { for (Production *prod : prods) {
delete prod; delete prod;
@@ -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; size_t ex_size = sizeof(Arg)*n_args+sizeof(size_t)*n_vars;
Item *item = (Item *) malloc(size+ex_size); Item *item = (Item *) malloc(size+ex_size);
item->ref_cnt = 0;
item->ccat = ccat; item->ccat = ccat;
item->lin_obj = lin.tagged(); item->lin_obj = lin.tagged();
item->seq = *vector_elem(lin->seqs,seq_idx); item->seq = *vector_elem(lin->seqs,seq_idx);
@@ -233,6 +260,7 @@ void *PgfLRTableMaker::Item::operator new(size_t size, ref<PgfConcrLincat> linca
lincat->n_lindefs*lincat->fields->len + index; lincat->n_lindefs*lincat->fields->len + index;
Item *item = (Item *) malloc(size+ex_size); Item *item = (Item *) malloc(size+ex_size);
item->ref_cnt = 0;
item->ccat = NULL; item->ccat = NULL;
item->lin_obj = lincat.tagged(); item->lin_obj = lincat.tagged();
item->seq = *vector_elem(lincat->seqs,seq_idx); item->seq = *vector_elem(lincat->seqs,seq_idx);
@@ -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; size_t ex_size = sizeof(Arg)*prod->args.count+sizeof(size_t)*prod->vals.count;
Item *item = (Item *) malloc(size+ex_size); Item *item = (Item *) malloc(size+ex_size);
item->ref_cnt = 0;
item->ccat = ccat; item->ccat = ccat;
item->lin_obj = prod->lin.tagged(); item->lin_obj = prod->lin.tagged();
item->seq_idx = prod->index*n_fields+lin_idx; item->seq_idx = prod->index*n_fields+lin_idx;
@@ -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; size_t ex_size = sizeof(Arg)*item->args.count+sizeof(size_t)*item->vals.count;
Item *new_item = (Item *) malloc(size+ex_size); Item *new_item = (Item *) malloc(size+ex_size);
new_item->ref_cnt = 0;
new_item->ccat = item->ccat; new_item->ccat = item->ccat;
new_item->lin_obj = item->lin_obj; new_item->lin_obj = item->lin_obj;
new_item->seq = item->seq; new_item->seq = item->seq;
@@ -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; size_t ex_size = sizeof(Arg)*item->args.count+sizeof(size_t)*item->vals.count;
Item *new_item = (Item *) malloc(size+ex_size); Item *new_item = (Item *) malloc(size+ex_size);
new_item->ref_cnt = 0;
new_item->ccat = item->ccat; new_item->ccat = item->ccat;
new_item->lin_obj = item->lin_obj; new_item->lin_obj = item->lin_obj;
new_item->seq = item->seq; new_item->seq = item->seq;
@@ -404,12 +435,20 @@ struct PgfLRTableMaker::State {
~State() { ~State() {
for (Item *item : items) { 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) { void push_item(Item *item) {
items.push_back(item); items.push_back(item); item->ref_cnt++;
push_heap(items.begin(), items.end(), compare_item); push_heap(items.begin(), items.end(), compare_item);
} }
}; };
@@ -468,7 +507,6 @@ PgfLRTableMaker::PgfLRTableMaker(ref<PgfAbstr> abstr, ref<PgfConcr> concr)
PgfLRTableMaker::~PgfLRTableMaker() PgfLRTableMaker::~PgfLRTableMaker()
{ {
/*
for (auto p : states) { for (auto p : states) {
delete p.second; delete p.second;
} }
@@ -479,7 +517,7 @@ PgfLRTableMaker::~PgfLRTableMaker()
for (auto p : ccats2) { for (auto p : ccats2) {
delete p.second; delete p.second;
}*/ }
} }
#if defined(DEBUG_STATE_CREATION) || defined(DEBUG_AUTOMATON) #if defined(DEBUG_STATE_CREATION) || defined(DEBUG_AUTOMATON)
@@ -673,7 +711,13 @@ void PgfLRTableMaker::symbol(State *state, Fold fold, Item *item, PgfSymbol sym)
if (fold == PROBE) { if (fold == PROBE) {
item->ccat->productive = true; item->ccat->productive = true;
} }
if (item->ref_cnt == 0)
delete item;
break;
} }
default:
if (item->ref_cnt == 0)
delete item;
} }
} }
@@ -720,12 +764,12 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, T cat, bool e
i--; i--;
values[i].value++; values[i].value++;
if (values[i].value < values[i].range) { if (values[i].value < values[i].range) {
index += r->terms[i].factor; index += values[i].factor;
i++; i++;
break; break;
} }
index -= (values[i].value-1) * r->terms[i].factor; index -= (values[i].value-1) * values[i].factor;
values[i].value = 0; values[i].value = 0;
} }
@@ -743,7 +787,7 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, ref<PgfText>
ccat = new CCat(++ccat_id, NULL, lin_idx); ccat = new CCat(++ccat_id, NULL, lin_idx);
} }
if (fold == PROBE) { if (fold == PROBE) {
ccat->suspended.push_back(item); ccat->suspend_item(item);
} }
if (tmp == NULL) { if (tmp == NULL) {
std::function<bool(ref<PgfAbsFun>)> f = std::function<bool(ref<PgfAbsFun>)> f =
@@ -760,7 +804,7 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, ref<PgfText>
if (fold == PROBE) { if (fold == PROBE) {
if (item->ccat != NULL && ccat->productive) { if (item->ccat != NULL && ccat->productive) {
item->ccat->productive = true; item->ccat->productive = true;
item->ccat->items.push_back(item); item->ccat->register_item(item);
} }
} else { } else {
if (ccat->productive) { if (ccat->productive) {
@@ -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); new_ccat = new CCat(++ccat_id, ccat, lin_idx);
} }
if (fold == PROBE) { if (fold == PROBE) {
new_ccat->suspended.push_back(item); new_ccat->suspend_item(item);
} }
if (tmp == NULL) { if (tmp == NULL) {
size_t n_prods = ccat->prods.size(); size_t n_prods = ccat->prods.size();
@@ -807,7 +851,7 @@ void PgfLRTableMaker::predict(State *state, Fold fold, Item *item, CCat *ccat, b
if (fold == PROBE) { if (fold == PROBE) {
if (item->ccat != NULL && new_ccat->productive) { if (item->ccat != NULL && new_ccat->productive) {
item->ccat->productive = true; item->ccat->productive = true;
item->ccat->items.push_back(item); item->ccat->register_item(item);
} }
} else { } else {
if (new_ccat->productive) { if (new_ccat->productive) {
@@ -873,8 +917,11 @@ void PgfLRTableMaker::complete(State *state, Fold fold, Item *item)
} }
} }
} }
if (item->ref_cnt == 0)
delete item;
} else { } else {
state->completed.push_back(item); state->completed.push_back(item); item->ref_cnt++;
#if defined(DEBUG_AUTOMATON) #if defined(DEBUG_AUTOMATON)
fprintf(stderr, "reduce "); fprintf(stderr, "reduce ");
@@ -904,7 +951,9 @@ ref<PgfLRTable> PgfLRTableMaker::make()
#endif #endif
process(state, INIT, item); process(state, INIT, item);
// delete item; item->ref_cnt--;
if (item->ref_cnt == 0)
delete item;
} }
for (auto &i : state->ccats1) { for (auto &i : state->ccats1) {