mirror of
https://github.com/lcn2/calc.git
synced 2025-08-19 01:13:27 +03:00
Fix more memory leaks, improve zfree() action
This commit is contained in:
12
zfunc.c
12
zfunc.c
@@ -1619,7 +1619,7 @@ zfacrem(ZVALUE z1, ZVALUE z2, ZVALUE *rem)
|
|||||||
long
|
long
|
||||||
zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
||||||
{
|
{
|
||||||
ZVALUE tmp1, tmp2;
|
ZVALUE tmp1, tmp2, tmp3, tmp4;
|
||||||
long count, onecount;
|
long count, onecount;
|
||||||
long sh;
|
long sh;
|
||||||
|
|
||||||
@@ -1663,15 +1663,17 @@ zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
|||||||
* the gcd becomes one.
|
* the gcd becomes one.
|
||||||
*/
|
*/
|
||||||
while (!zisunit(z2)) {
|
while (!zisunit(z2)) {
|
||||||
onecount = zfacrem(z1, z2, &tmp1);
|
onecount = zfacrem(z1, z2, &tmp3);
|
||||||
if (onecount) {
|
if (onecount) {
|
||||||
count += onecount;
|
count += onecount;
|
||||||
zfree(z1);
|
zfree(z1);
|
||||||
z1 = tmp1;
|
z1 = tmp3;
|
||||||
|
} else {
|
||||||
|
zfree(tmp3);
|
||||||
}
|
}
|
||||||
zgcd(z1, z2, &tmp1);
|
zgcd(z1, z2, &tmp4);
|
||||||
zfree(z2);
|
zfree(z2);
|
||||||
z2 = tmp1;
|
z2 = tmp4;
|
||||||
}
|
}
|
||||||
zfree(z2);
|
zfree(z2);
|
||||||
*res = z1;
|
*res = z1;
|
||||||
|
2
zmath.h
2
zmath.h
@@ -579,7 +579,7 @@ E_FUNC void zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res);
|
|||||||
#define zcopyval(z1,z2) memcpy((z2).v, (z1).v, (z1).len * sizeof(HALF))
|
#define zcopyval(z1,z2) memcpy((z2).v, (z1).v, (z1).len * sizeof(HALF))
|
||||||
#define zquicktrim(z) {if (((z).len > 1) && ((z).v[(z).len-1] == 0)) \
|
#define zquicktrim(z) {if (((z).len > 1) && ((z).v[(z).len-1] == 0)) \
|
||||||
(z).len--;}
|
(z).len--;}
|
||||||
#define zfree(z) freeh((z).v)
|
#define zfree(z) {if ((z).len != 0 && (z).v != NULL) { freeh((z).v); (z).len = 0; (z).v = NULL; (z).sign = 0; } }
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
83
zmod.c
83
zmod.c
@@ -480,7 +480,7 @@ zmod6(ZVALUE z1, ZVALUE *res)
|
|||||||
* slightly faster.
|
* slightly faster.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
zpowermod(ZVALUE z1_arg, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
||||||
{
|
{
|
||||||
HALF *hp; /* pointer to current word of the power */
|
HALF *hp; /* pointer to current word of the power */
|
||||||
REDC *rp; /* REDC information to be used */
|
REDC *rp; /* REDC information to be used */
|
||||||
@@ -488,7 +488,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
ZVALUE ans, temp; /* calculation values */
|
ZVALUE ans, temp; /* calculation values */
|
||||||
ZVALUE modpow; /* current small power */
|
ZVALUE modpow; /* current small power */
|
||||||
ZVALUE lowpowers[POWNUMS]; /* low powers */
|
ZVALUE lowpowers[POWNUMS]; /* low powers */
|
||||||
ZVALUE ztmp;
|
ZVALUE z1;
|
||||||
int curshift; /* shift value for word of power */
|
int curshift; /* shift value for word of power */
|
||||||
HALF curhalf; /* current word of power */
|
HALF curhalf; /* current word of power */
|
||||||
unsigned int curpow; /* current low power */
|
unsigned int curpow; /* current low power */
|
||||||
@@ -508,7 +508,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
/*
|
/*
|
||||||
* Check easy cases first.
|
* Check easy cases first.
|
||||||
*/
|
*/
|
||||||
if ((ziszero(z1) && !ziszero(z2)) || zisunit(z3)) {
|
if ((ziszero(z1_arg) && !ziszero(z2)) || zisunit(z3)) {
|
||||||
/* 0^(non_zero) or x^y mod 1 always produces zero */
|
/* 0^(non_zero) or x^y mod 1 always produces zero */
|
||||||
*res = _zero_;
|
*res = _zero_;
|
||||||
return;
|
return;
|
||||||
@@ -518,13 +518,13 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (zistwo(z3)) { /* mod 2 */
|
if (zistwo(z3)) { /* mod 2 */
|
||||||
if (zisodd(z1))
|
if (zisodd(z1_arg))
|
||||||
*res = _one_;
|
*res = _one_;
|
||||||
else
|
else
|
||||||
*res = _zero_;
|
*res = _zero_;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (zisunit(z1) && (!z1.sign || ziseven(z2))) {
|
if (zisunit(z1_arg) && (!z1_arg.sign || ziseven(z2))) {
|
||||||
/* 1^x or (-1)^(2x) */
|
/* 1^x or (-1)^(2x) */
|
||||||
*res = _one_;
|
*res = _one_;
|
||||||
return;
|
return;
|
||||||
@@ -534,20 +534,18 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
* Normalize the number being raised to be non-negative and to lie
|
* Normalize the number being raised to be non-negative and to lie
|
||||||
* within the modulo range. Then check for zero or one specially.
|
* within the modulo range. Then check for zero or one specially.
|
||||||
*/
|
*/
|
||||||
ztmp.len = 0;
|
if (zisneg(z1_arg) || zrel(z1_arg, z3) >= 0) {
|
||||||
if (zisneg(z1) || zrel(z1, z3) >= 0) {
|
zmod(z1_arg, z3, &z1, 0);
|
||||||
zmod(z1, z3, &ztmp, 0);
|
} else {
|
||||||
z1 = ztmp;
|
zcopy(z1_arg, &z1);
|
||||||
}
|
}
|
||||||
if (ziszero(z1)) {
|
if (ziszero(z1)) {
|
||||||
if (ztmp.len)
|
zfree(z1);
|
||||||
zfree(ztmp);
|
|
||||||
*res = _zero_;
|
*res = _zero_;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (zisone(z1)) {
|
if (zisone(z1)) {
|
||||||
if (ztmp.len)
|
zfree(z1);
|
||||||
zfree(ztmp);
|
|
||||||
*res = _one_;
|
*res = _one_;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -575,7 +573,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
pp->v = NULL;
|
pp->v = NULL;
|
||||||
}
|
}
|
||||||
lowpowers[0] = _one_;
|
lowpowers[0] = _one_;
|
||||||
lowpowers[1] = z1;
|
zcopy(z1, &lowpowers[1]);
|
||||||
ans = _one_;
|
ans = _one_;
|
||||||
|
|
||||||
hp = &z2.v[z2.len - 1];
|
hp = &z2.v[z2.len - 1];
|
||||||
@@ -666,13 +664,11 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (pp = &lowpowers[2]; pp <= &lowpowers[POWNUMS-1]; pp++) {
|
for (pp = &lowpowers[1]; pp <= &lowpowers[POWNUMS-1]; pp++) {
|
||||||
if (pp->v != NULL)
|
zfree(*pp);
|
||||||
freeh(pp->v);
|
|
||||||
}
|
}
|
||||||
*res = ans;
|
*res = ans;
|
||||||
if (ztmp.len)
|
zfree(z1);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -689,6 +685,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
powermodredc = zredcalloc(z3);
|
powermodredc = zredcalloc(z3);
|
||||||
rp = powermodredc;
|
rp = powermodredc;
|
||||||
zredcencode(rp, z1, &temp);
|
zredcencode(rp, z1, &temp);
|
||||||
|
zfree(z1);
|
||||||
zredcpower(rp, temp, z2, &z1);
|
zredcpower(rp, temp, z2, &z1);
|
||||||
zfree(temp);
|
zfree(temp);
|
||||||
zredcdecode(rp, z1, res);
|
zredcdecode(rp, z1, res);
|
||||||
@@ -705,7 +702,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
pp->v = NULL;
|
pp->v = NULL;
|
||||||
}
|
}
|
||||||
lowpowers[0] = _one_;
|
lowpowers[0] = _one_;
|
||||||
lowpowers[1] = z1;
|
zcopy(z1, &lowpowers[1]);
|
||||||
ans = _one_;
|
ans = _one_;
|
||||||
|
|
||||||
hp = &z2.v[z2.len - 1];
|
hp = &z2.v[z2.len - 1];
|
||||||
@@ -747,9 +744,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pp = &lowpowers[curpow];
|
pp = &lowpowers[curpow];
|
||||||
if (pp->v != NULL) {
|
zfree(*pp);
|
||||||
zfree(*pp);
|
|
||||||
}
|
|
||||||
*pp = modpow;
|
*pp = modpow;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -788,13 +783,12 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (pp = &lowpowers[2]; pp <= &lowpowers[POWNUMS-1]; pp++) {
|
for (pp = &lowpowers[1]; pp <= &lowpowers[POWNUMS-1]; pp++) {
|
||||||
if (pp->v != NULL)
|
zfree(*pp);
|
||||||
freeh(pp->v);
|
|
||||||
}
|
}
|
||||||
*res = ans;
|
*res = ans;
|
||||||
if (ztmp.len)
|
zfree(z1);
|
||||||
zfree(ztmp);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1135,8 +1129,7 @@ zredcdecode(REDC *rp, ZVALUE z1, ZVALUE *res)
|
|||||||
if (ztop.len) {
|
if (ztop.len) {
|
||||||
zadd(*res, ztop, &tmp1);
|
zadd(*res, ztop, &tmp1);
|
||||||
zfree(*res);
|
zfree(*res);
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
*res = tmp1;
|
*res = tmp1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1504,15 +1497,13 @@ zredcsquare(REDC *rp, ZVALUE z1, ZVALUE *res)
|
|||||||
}
|
}
|
||||||
if (ziszero(z1)) {
|
if (ziszero(z1)) {
|
||||||
*res = _zero_;
|
*res = _zero_;
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ((z1.len == rp->one.len) && (z1.v[0] == rp->one.v[0]) &&
|
if ((z1.len == rp->one.len) && (z1.v[0] == rp->one.v[0]) &&
|
||||||
(zcmp(z1, rp->one) == 0)) {
|
(zcmp(z1, rp->one) == 0)) {
|
||||||
zcopy(z1, res);
|
zcopy(z1, res);
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1527,8 +1518,7 @@ zredcsquare(REDC *rp, ZVALUE z1, ZVALUE *res)
|
|||||||
zsquare(z1, &tmp);
|
zsquare(z1, &tmp);
|
||||||
zredcdecode(rp, tmp, res);
|
zredcdecode(rp, tmp, res);
|
||||||
zfree(tmp);
|
zfree(tmp);
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modlen = rp->mod.len;
|
modlen = rp->mod.len;
|
||||||
@@ -1633,8 +1623,7 @@ zredcsquare(REDC *rp, ZVALUE z1, ZVALUE *res)
|
|||||||
}
|
}
|
||||||
res->len = len;
|
res->len = len;
|
||||||
if (zrel(*res, rp->mod) < 0) {
|
if (zrel(*res, rp->mod) < 0) {
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1657,8 +1646,7 @@ zredcsquare(REDC *rp, ZVALUE z1, ZVALUE *res)
|
|||||||
len--;
|
len--;
|
||||||
}
|
}
|
||||||
res->len = len;
|
res->len = len;
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1717,8 +1705,7 @@ zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
|||||||
*res = _one_;
|
*res = _one_;
|
||||||
else
|
else
|
||||||
*res = _zero_;
|
*res = _zero_;
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (zcmp(z1, rp->one) == 0) {
|
if (zcmp(z1, rp->one) == 0) {
|
||||||
@@ -1726,8 +1713,7 @@ zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
|||||||
zsub(rp->mod, rp->one, res);
|
zsub(rp->mod, rp->one, res);
|
||||||
else
|
else
|
||||||
zcopy(rp->one, res);
|
zcopy(rp->one, res);
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1741,14 +1727,12 @@ zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
|||||||
if (zcmp(z1, temp) == 0) {
|
if (zcmp(z1, temp) == 0) {
|
||||||
if (zisodd(z2) ^ sign) {
|
if (zisodd(z2) ^ sign) {
|
||||||
*res = temp;
|
*res = temp;
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
zfree(temp);
|
zfree(temp);
|
||||||
zcopy(rp->one, res);
|
zcopy(rp->one, res);
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
zfree(temp);
|
zfree(temp);
|
||||||
@@ -1845,8 +1829,7 @@ zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
|
|||||||
} else {
|
} else {
|
||||||
*res = ans;
|
*res = ans;
|
||||||
}
|
}
|
||||||
if (ztmp.len)
|
zfree(ztmp);
|
||||||
zfree(ztmp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
10
zrandom.c
10
zrandom.c
@@ -2296,19 +2296,19 @@ zsrandom1(CONST ZVALUE seed, BOOL need_ret)
|
|||||||
last_r.v = NULL;
|
last_r.v = NULL;
|
||||||
do {
|
do {
|
||||||
/* free temp storage */
|
/* free temp storage */
|
||||||
if (last_r.v != NULL) {
|
zfree_random(last_r);
|
||||||
zfree_random(last_r);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* last_r = r;
|
* last_r = r;
|
||||||
* r = pmod(last_r, 2, n);
|
* r = pmod(last_r, 2, n);
|
||||||
*/
|
*/
|
||||||
last_r = r;
|
zcopy(r, &last_r);
|
||||||
|
zfree_random(r);
|
||||||
zsquaremod(last_r, blum.n, &r);
|
zsquaremod(last_r, blum.n, &r);
|
||||||
} while (zrel(r, last_r) > 0);
|
} while (zrel(r, last_r) > 0);
|
||||||
zfree_random(blum.r);
|
zfree_random(blum.r);
|
||||||
blum.r = r;
|
blum.r = r;
|
||||||
|
|
||||||
/* free temp storage */
|
/* free temp storage */
|
||||||
zfree_random(last_r);
|
zfree_random(last_r);
|
||||||
|
|
||||||
@@ -3062,6 +3062,7 @@ randomcopy(CONST RANDOM *state)
|
|||||||
void
|
void
|
||||||
randomfree(RANDOM *state)
|
randomfree(RANDOM *state)
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
/* avoid free of the pre-defined states */
|
/* avoid free of the pre-defined states */
|
||||||
if (state == &init_blum) {
|
if (state == &init_blum) {
|
||||||
return;
|
return;
|
||||||
@@ -3070,6 +3071,7 @@ randomfree(RANDOM *state)
|
|||||||
state <= &random_pregen[BLUM_PREGEN-1]) {
|
state <= &random_pregen[BLUM_PREGEN-1]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* free the values */
|
/* free the values */
|
||||||
zfree_random(state->n);
|
zfree_random(state->n);
|
||||||
|
Reference in New Issue
Block a user