commit f4a846b7af7d9d9e1bb8875b5ca1a3127125da4f
parent b0e3260681c0a17e3d062479d7d1ca9e28a04d6e
Author: Andres Navarro <canavarro82@gmail.com>
Date: Tue, 12 Apr 2011 00:50:55 -0300
Bugfix: in IMATH changed code to check if there's space allocated from MP_DIGITS(z) != &z to MP_DIGITS(z) != &MP_SINGLE(x). This is a problem only in klisp because single in not the first member of the struct. Added code to free the allocated memory in kstate.
Diffstat:
3 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/src/imath.c b/src/imath.c
@@ -468,9 +468,8 @@ void mp_int_clear(mp_int z)
return;
if(MP_DIGITS(z) != NULL) {
- if((void *) MP_DIGITS(z) != (void *) z)
+ if((void *) MP_DIGITS(z) != (void *) &MP_SINGLE(z))
s_free(MP_DIGITS(z));
-
MP_DIGITS(z) = NULL;
}
}
@@ -785,7 +784,7 @@ mp_result mp_int_mul(mp_int a, mp_int b, mp_int c)
already using, and fix up its fields to reflect that.
*/
if(out != MP_DIGITS(c)) {
- if((void *) MP_DIGITS(c) != (void *) c)
+ if((void *) MP_DIGITS(c) != (void *) &MP_SINGLE(c))
s_free(MP_DIGITS(c));
MP_DIGITS(c) = out;
MP_ALLOC(c) = p;
@@ -864,7 +863,7 @@ mp_result mp_int_sqr(mp_int a, mp_int c)
fields to reflect the new digit array it's using
*/
if(out != MP_DIGITS(c)) {
- if((void *) MP_DIGITS(c) != (void *) c)
+ if((void *) MP_DIGITS(c) != (void *) &MP_SINGLE(c))
s_free(MP_DIGITS(c));
MP_DIGITS(c) = out;
MP_ALLOC(c) = p;
diff --git a/src/imath.h b/src/imath.h
@@ -68,6 +68,7 @@ typedef struct mpz {
*/
typedef Bigint mpz_t, *mp_int;
+#define MP_SINGLE(Z) ((Z)->single) /* added to correct check in mp_int_clear */
#define MP_DIGITS(Z) ((Z)->digits)
#define MP_ALLOC(Z) ((Z)->alloc)
#define MP_USED(Z) ((Z)->used)
diff --git a/src/kstate.c b/src/kstate.c
@@ -457,7 +457,11 @@ void klisp_close (klisp_State *K)
switch(type) {
case K_TBIGINT: {
Bigint *bigint = (Bigint *)obj;
- /* XXX / TODO free array */
+ /* XXX / TODO change when klisp allocator is used in IMath */
+ if (bigint->digits != NULL &&
+ bigint->digits != &(bigint->single)) {
+ free(bigint->digits);
+ }
klispM_free(K, bigint);
break;
}