Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Correct some memory bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mercatp committed Jul 20, 2016
1 parent 42b95c5 commit 7ae36cb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
50 changes: 39 additions & 11 deletions src/sage/combinat/words/automataC.c
Expand Up @@ -79,7 +79,10 @@ void printDict (Dict d)
void dictAdd (Dict *d, int e)
{
d->n++;
d->e = (int *)realloc(d->e, sizeof(int)*d->n);
if (d->n == 1)
d->e = (int *)malloc(sizeof(int));
else
d->e = (int *)realloc(d->e, sizeof(int)*d->n);
if (!d->e)
{
printf("Out of memory !");
Expand Down Expand Up @@ -171,6 +174,7 @@ NAutomaton NewNAutomaton (int n, int na)
a.na = na;
if (n == 0)
{
a.e = NULL;
return a;
}
a.e = (NEtat *)malloc(sizeof(NEtat)*n);
Expand All @@ -190,7 +194,10 @@ NAutomaton NewNAutomaton (int n, int na)

void ReallocNAutomaton (NAutomaton *a, int n)
{
a->e = (NEtat*)realloc(a->e, sizeof(NEtat)*n);
if (a->n)
a->e = (NEtat*)realloc(a->e, sizeof(NEtat)*n);
else
a->e = (NEtat*)malloc(sizeof(NEtat)*n);
if (a->n < n)
{
int i;
Expand Down Expand Up @@ -230,7 +237,10 @@ void ReallocAutomaton (Automaton *a, int n, bool init)
free(a->e[i].f);
}
}
a->e = (Etat*)realloc(a->e, sizeof(Etat)*n);
if (a->n)
a->e = (Etat*)realloc(a->e, sizeof(Etat)*n);
else
a->e = (Etat*)malloc(sizeof(Etat)*n);
if (a->n < n)
{
int i;
Expand Down Expand Up @@ -873,7 +883,10 @@ void AddEtat (Automaton *a, bool final)
{
/**/
a->n++;
a->e = (Etat *)realloc(a->e, sizeof(Etat)*a->n);
if (a->n == 1)
a->e = (Etat *)malloc(sizeof(Etat));
else
a->e = (Etat *)realloc(a->e, sizeof(Etat)*a->n);
if (!a->e)
{
printf("Out of memory !");
Expand Down Expand Up @@ -983,7 +996,10 @@ bool AddEl (ListEtats *l, Etats e, int* res)
}
//ajoute l'élément
l->n++;
l->e = (Etats*)realloc(l->e, sizeof(Etats)*l->n);
if (l->n == 1)
l->e = (Etats*)malloc(sizeof(Etats));
else
l->e = (Etats*)realloc(l->e, sizeof(Etats)*l->n);
if (!l->e)
{
printf("Out of memory !");
Expand All @@ -1000,7 +1016,10 @@ void AddEl2 (ListEtats *l, Etats e)
{
//ajoute l'élément
l->n++;
l->e = (Etats*)realloc(l->e, sizeof(Etats)*l->n);
if (l->n == 1)
l->e = (Etats*)malloc(sizeof(Etats));
else
l->e = (Etats*)realloc(l->e, sizeof(Etats)*l->n);
if (!l->e)
{
printf("Out of memory !");
Expand Down Expand Up @@ -1128,19 +1147,25 @@ ListEtats2 NewListEtats2(int n, int na)

void ReallocListEtats2(ListEtats2 *l, int n, bool marge)
{
l->n = n;
if (!marge)
{
if (l->na)
l->e = (Etats2*)realloc(l->e, sizeof(Etats2)*n);
else
l->e = (Etats2*)malloc(sizeof(Etats2)*n);
l->na = n;
l->e = (Etats2*)realloc(l->e, sizeof(Etats2)*n);
}else
{
if (n > l->na)
{
l->na = l->n*2;
l->e = (Etats2*)realloc(l->e, sizeof(Etats2)*l->na);
if (l->na)
l->e = (Etats2*)realloc(l->e, sizeof(Etats2)*n*2);
else
l->e = (Etats2*)malloc(sizeof(Etats2)*n*2);
l->na = n*2;
}
}
l->n = n;
}

void FreeListEtats2 (ListEtats2* l)
Expand Down Expand Up @@ -2047,7 +2072,10 @@ NAutomaton Transpose (Automaton a)
{
//ajoute une arête de f vers i étiquetée par j
r.e[f].n++;
r.e[f].a = (Arete *)realloc(r.e[f].a, sizeof(Arete)*r.e[f].n);
if (r.e[f].n == 1)
r.e[f].a = (Arete *)malloc(sizeof(Arete));
else
r.e[f].a = (Arete *)realloc(r.e[f].a, sizeof(Arete)*r.e[f].n);
r.e[f].a[r.e[f].n-1].l = j;
r.e[f].a[r.e[f].n-1].e = i;
}
Expand Down
6 changes: 3 additions & 3 deletions src/sage/monoids/beta_adic_monoid.pyx
Expand Up @@ -256,7 +256,7 @@ cdef extern from "draw.h":
void FreeColorList (ColorList l)
Color randColor (int a)
Automate NewAutomate (int n, int na)
void FreeAutomate(Automate a)
void FreeAutomate(Automate *a)
void FreeAutomates(Automate* a, int n)
BetaAdic NewBetaAdic (int n)
void FreeBetaAdic (BetaAdic b)
Expand Down Expand Up @@ -878,7 +878,7 @@ class BetaAdicMonoid(Monoid_class):
print "Free..."
FreeSurface(s)
if not isinstance(tss, FastAutomaton):
FreeAutomate(b.a)
FreeAutomate(&b.a)
FreeBetaAdic(b)
sig_off()

Expand Down Expand Up @@ -1012,7 +1012,7 @@ class BetaAdicMonoid(Monoid_class):
else:
for i,a in enumerate(la):
if not isinstance(a, FastAutomaton):
FreeAutomate(b.a[i])
FreeAutomate(&b.a[i])
FreeBetaAdic2(b)
FreeColorList(cl)
sig_off()
Expand Down
18 changes: 13 additions & 5 deletions src/sage/monoids/draw.c
Expand Up @@ -63,6 +63,12 @@ Automate NewAutomate (int n, int na)
Automate a;
a.n = n;
a.na = na;
a.i = -1;
if (!n)
{
a.e = NULL;
return;
}
a.e = (Etat *)malloc(sizeof(Etat)*n);
int i, j;
for (i=0;i<n;i++)
Expand All @@ -77,22 +83,24 @@ Automate NewAutomate (int n, int na)
return a;
}

void FreeAutomate (Automate a)
void FreeAutomate (Automate *a)
{
int i;
for (i=0;i<a.n;i++)
for (i=0;i<a->n;i++)
{
free(a.e[i].f);
free(a->e[i].f);
}
free(a.e);
if (a->n)
free(a->e);
a->e = NULL;
}

void FreeAutomates (Automate* a, int n)
{
int j;
for (j=0;j<n;j++)
{
FreeAutomate(a[j]);
FreeAutomate(&a[j]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/sage/monoids/draw.h
Expand Up @@ -41,7 +41,7 @@ Surface NewSurface (int sx, int sy);
void FreeSurface (Surface s);
void Fill (Surface s, Color c);
Automate NewAutomate (int n, int na);
void FreeAutomate (Automate a);
void FreeAutomate (Automate *a);
void FreeAutomates (Automate* a, int n);
BetaAdic NewBetaAdic (int n);
void FreeBetaAdic (BetaAdic b);
Expand Down
6 changes: 4 additions & 2 deletions src/sage/monoids/relations.c
Expand Up @@ -9,9 +9,9 @@ Automate NewAutomaton (int n, int na)
Automate a;
a.n = n;
a.na = na;
a.i = -1;
if (n == 0)
{
a.i = -1;
a.e = NULL;
return a;
}
Expand All @@ -34,6 +34,7 @@ Automate NewAutomaton (int n, int na)
for (j=0;j<na;j++)
{
a.e[i].f[j] = -1;
a.e[i].final = false;
}
}
return a;
Expand Down Expand Up @@ -681,7 +682,8 @@ Automate RelationsAutomatonT (InfoBetaAdic iba2, Element t, bool isvide, bool ex
r.i = indElement(t);
e = zeroElement();
ind = indElement(e);
r.e[ind].final = true;
if (ind != -1)
r.e[ind].final = true;
if (verb)
printf("free...\n");
//libère les éléments de la table de hachage
Expand Down

0 comments on commit 7ae36cb

Please sign in to comment.