Remove freelists managed by Balloc/Bfree.

Change __dtoa to not free the string it allocated the previous time it was
called.  The caller now frees the string after usage if appropiate.

PR:		15070
Reviewed by:	deischen
This commit is contained in:
Tor Egge 2001-02-15 22:12:50 +00:00
parent 5d56fa0cc6
commit 2ffc61ba9c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=72523
3 changed files with 41 additions and 69 deletions

View File

@ -268,7 +268,7 @@ vfprintf(FILE *fp, const char *fmt0, va_list ap)
#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */
#define DEFPREC 6 #define DEFPREC 6
static char *cvt __P((double, int, int, char *, int *, int, int *)); static char *cvt __P((double, int, int, char *, int *, int, int *, char **));
static int exponent __P((char *, int, int)); static int exponent __P((char *, int, int));
#else /* no FLOATING_POINT */ #else /* no FLOATING_POINT */
@ -315,6 +315,7 @@ __vfprintf(FILE *fp, const char *fmt0, va_list ap)
int expsize; /* character count for expstr */ int expsize; /* character count for expstr */
int ndig; /* actual number of digits returned by cvt */ int ndig; /* actual number of digits returned by cvt */
char expstr[7]; /* buffer for exponent string */ char expstr[7]; /* buffer for exponent string */
char *dtoaresult; /* buffer allocated by dtoa */
#endif #endif
u_long ulval; /* integer arguments %[diouxX] */ u_long ulval; /* integer arguments %[diouxX] */
u_quad_t uqval; /* %q integers */ u_quad_t uqval; /* %q integers */
@ -423,6 +424,9 @@ __vfprintf(FILE *fp, const char *fmt0, va_list ap)
} }
#ifdef FLOATING_POINT
dtoaresult = NULL;
#endif
/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
if (cantwrite(fp)) if (cantwrite(fp))
return (EOF); return (EOF);
@ -608,8 +612,12 @@ fp_begin: if (prec == -1)
break; break;
} }
flags |= FPT; flags |= FPT;
if (dtoaresult != NULL) {
free(dtoaresult);
dtoaresult = NULL;
}
cp = cvt(_double, prec, flags, &softsign, cp = cvt(_double, prec, flags, &softsign,
&expt, ch, &ndig); &expt, ch, &ndig, &dtoaresult);
if (ch == 'g' || ch == 'G') { if (ch == 'g' || ch == 'G') {
if (expt <= -4 || expt > prec) if (expt <= -4 || expt > prec)
ch = (ch == 'g') ? 'e' : 'E'; ch = (ch == 'g') ? 'e' : 'E';
@ -864,6 +872,10 @@ number: if ((dprec = prec) >= 0)
done: done:
FLUSH(); FLUSH();
error: error:
#ifdef FLOATING_POINT
if (dtoaresult != NULL)
free(dtoaresult);
#endif
if (__sferror(fp)) if (__sferror(fp))
ret = EOF; ret = EOF;
if ((argtable != NULL) && (argtable != statargtable)) if ((argtable != NULL) && (argtable != statargtable))
@ -1195,11 +1207,11 @@ __grow_type_table (int nextarg, unsigned char **typetable, int *tablesize)
#ifdef FLOATING_POINT #ifdef FLOATING_POINT
extern char *__dtoa __P((double, int, int, int *, int *, char **)); extern char *__dtoa __P((double, int, int, int *, int *, char **, char **));
static char * static char *
cvt(double value, int ndigits, int flags, char *sign, int *decpt, cvt(double value, int ndigits, int flags, char *sign, int *decpt,
int ch, int *length) int ch, int *length, char **dtoaresultp)
{ {
int mode, dsgn; int mode, dsgn;
char *digits, *bp, *rve; char *digits, *bp, *rve;
@ -1221,7 +1233,8 @@ cvt(double value, int ndigits, int flags, char *sign, int *decpt,
*sign = '-'; *sign = '-';
} else } else
*sign = '\000'; *sign = '\000';
digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve,
dtoaresultp);
if ((ch != 'g' && ch != 'G') || flags & ALT) { if ((ch != 'g' && ch != 'G') || flags & ALT) {
/* print trailing zeros */ /* print trailing zeros */
bp = digits + ndigits; bp = digits + ndigits;

View File

@ -142,7 +142,7 @@ __RCSID("$NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp $");
#include "memory.h" #include "memory.h"
#endif #endif
#endif #endif
char *__dtoa __P((double, int, int, int *, int *, char **)); char *__dtoa __P((double, int, int, int *, int *, char **, char **));
#ifdef MALLOC #ifdef MALLOC
#ifdef KR_headers #ifdef KR_headers
@ -382,8 +382,6 @@ Bigint {
typedef struct Bigint Bigint; typedef struct Bigint Bigint;
static Bigint *freelist[Kmax+1];
static Bigint * static Bigint *
Balloc Balloc
#ifdef KR_headers #ifdef KR_headers
@ -395,15 +393,10 @@ Balloc
int x; int x;
Bigint *rv; Bigint *rv;
if ((rv = freelist[k]) != NULL) { x = 1 << k;
freelist[k] = rv->next; rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
} rv->k = k;
else { rv->maxwds = x;
x = 1 << k;
rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
rv->k = k;
rv->maxwds = x;
}
rv->sign = rv->wds = 0; rv->sign = rv->wds = 0;
return rv; return rv;
} }
@ -416,10 +409,7 @@ Bfree
(Bigint *v) (Bigint *v)
#endif #endif
{ {
if (v) { free(v);
v->next = freelist[v->k];
freelist[v->k] = v;
}
} }
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
@ -1900,10 +1890,11 @@ quorem
char * char *
__dtoa __dtoa
#ifdef KR_headers #ifdef KR_headers
(d, mode, ndigits, decpt, sign, rve) (d, mode, ndigits, decpt, sign, rve, resultp)
double d; int mode, ndigits, *decpt, *sign; char **rve; double d; int mode, ndigits, *decpt, *sign; char **rve, **resultp;
#else #else
(double d, int mode, int ndigits, int *decpt, int *sign, char **rve) (double d, int mode, int ndigits, int *decpt, int *sign, char **rve,
char **resultp)
#endif #endif
{ {
/* Arguments ndigits, decpt, sign are similar to those /* Arguments ndigits, decpt, sign are similar to those
@ -1953,15 +1944,6 @@ __dtoa
Bigint *mlo = NULL; /* pacify gcc */ Bigint *mlo = NULL; /* pacify gcc */
double d2, ds, eps; double d2, ds, eps;
char *s, *s0; char *s, *s0;
static Bigint *result;
static int result_k;
if (result) {
result->k = result_k;
result->maxwds = 1 << result_k;
Bfree(result);
result = 0;
}
if (word0(d) & Sign_bit) { if (word0(d) & Sign_bit) {
/* set sign for everything, including 0's and NaNs */ /* set sign for everything, including 0's and NaNs */
@ -2123,11 +2105,8 @@ __dtoa
if (i <= 0) if (i <= 0)
i = 1; i = 1;
} }
j = sizeof(ULong); *resultp = (char *) malloc(i + 1);
for(result_k = 0; sizeof(Bigint) - sizeof(ULong) + j <= i; s = s0 = *resultp;
j <<= 1) result_k++;
result = Balloc(result_k);
s = s0 = (char *)result;
if (ilim >= 0 && ilim <= Quick_max && try_quick) { if (ilim >= 0 && ilim <= Quick_max && try_quick) {

View File

@ -372,8 +372,6 @@ Bigint {
typedef struct Bigint Bigint; typedef struct Bigint Bigint;
static Bigint *freelist[Kmax+1];
static Bigint * static Bigint *
Balloc Balloc
#ifdef KR_headers #ifdef KR_headers
@ -385,14 +383,10 @@ Balloc
int x; int x;
Bigint *rv; Bigint *rv;
if ( (rv = freelist[k]) ) { x = 1 << k;
freelist[k] = rv->next; rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long));
} else { rv->k = k;
x = 1 << k; rv->maxwds = x;
rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long));
rv->k = k;
rv->maxwds = x;
}
rv->sign = rv->wds = 0; rv->sign = rv->wds = 0;
return rv; return rv;
} }
@ -405,10 +399,7 @@ Bfree
(Bigint *v) (Bigint *v)
#endif #endif
{ {
if (v) { free(v);
v->next = freelist[v->k];
freelist[v->k] = v;
}
} }
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
@ -1844,10 +1835,11 @@ quorem
char * char *
__dtoa __dtoa
#ifdef KR_headers #ifdef KR_headers
(d, mode, ndigits, decpt, sign, rve) (d, mode, ndigits, decpt, sign, rve, resultp)
double d; int mode, ndigits, *decpt, *sign; char **rve; double d; int mode, ndigits, *decpt, *sign; char **rve, **resultp;
#else #else
(double d, int mode, int ndigits, int *decpt, int *sign, char **rve) (double d, int mode, int ndigits, int *decpt, int *sign, char **rve,
char **resultp)
#endif #endif
{ {
/* Arguments ndigits, decpt, sign are similar to those /* Arguments ndigits, decpt, sign are similar to those
@ -1895,15 +1887,6 @@ __dtoa
Bigint *b, *b1, *delta, *mlo, *mhi, *S; Bigint *b, *b1, *delta, *mlo, *mhi, *S;
double d2, ds, eps; double d2, ds, eps;
char *s, *s0; char *s, *s0;
static Bigint *result;
static int result_k;
if (result) {
result->k = result_k;
result->maxwds = 1 << result_k;
Bfree(result);
result = 0;
}
if (word0(d) & Sign_bit) { if (word0(d) & Sign_bit) {
/* set sign for everything, including 0's and NaNs */ /* set sign for everything, including 0's and NaNs */
@ -2062,11 +2045,8 @@ __dtoa
if (i <= 0) if (i <= 0)
i = 1; i = 1;
} }
j = sizeof(unsigned long); *resultp = (char *) malloc(i + 1);
for (result_k = 0; sizeof(Bigint) - sizeof(unsigned long) + j < i; s = s0 = *resultp;
j <<= 1) result_k++;
result = Balloc(result_k);
s = s0 = (char *)result;
if (ilim >= 0 && ilim <= Quick_max && try_quick) { if (ilim >= 0 && ilim <= Quick_max && try_quick) {