improved storage allocation

This commit is contained in:
ceriel 1987-03-17 22:31:43 +00:00
parent 10080ca0f2
commit 78303cdc07
4 changed files with 26 additions and 27 deletions

View file

@ -186,7 +186,7 @@ go_on:
ptok->tk_val = 0; ptok->tk_val = 0;
return ptok->tk_symb = INTEGER; return ptok->tk_symb = INTEGER;
} }
ptok->tk_str = Malloc(idfsize + 1); ptok->tk_str = Malloc(tg - buf);
strcpy(ptok->tk_str, buf); strcpy(ptok->tk_str, buf);
return ptok->tk_symb = IDENTIFIER; return ptok->tk_symb = IDENTIFIER;
} }
@ -290,7 +290,7 @@ string_token(nm, stop_char)
char *nm; char *nm;
{ {
register int c; register int c;
register int str_size; register unsigned int str_size;
register char *str = Malloc(str_size = ISTRSIZE); register char *str = Malloc(str_size = ISTRSIZE);
register int pos = 0; register int pos = 0;
@ -316,7 +316,7 @@ string_token(nm, stop_char)
} }
str[pos++] = c; str[pos++] = c;
if (pos == str_size) if (pos == str_size)
str = Srealloc(str, str_size += RSTRSIZE); str = Srealloc(str, str_size <<= 1);
LoadChar(c); LoadChar(c);
} }
str[pos++] = '\0'; /* for filenames etc. */ str[pos++] = '\0'; /* for filenames etc. */

View file

@ -33,10 +33,8 @@
!File: strsize.h !File: strsize.h
#define ISTRSIZE 32 /* minimum number of bytes allocated for #define ISTRSIZE 16 /* minimum number of bytes allocated for
storing a string */ storing a string */
#define RSTRSIZE 32 /* step size in enlarging the memory for
the storage of a string */
!File: botch_free.h !File: botch_free.h
@ -52,8 +50,7 @@
!File: textsize.h !File: textsize.h
#define ITEXTSIZE 64 /* 1st piece of memory for repl. text */ #define ITEXTSIZE 16 /* 1st piece of memory for repl. text */
#define RTEXTSIZE 64 /* stepsize for enlarging repl.text */
!File: inputtype.h !File: inputtype.h

View file

@ -586,9 +586,9 @@ get_text(formals, length)
parameter. Other tokens will not be seen as such. parameter. Other tokens will not be seen as such.
*/ */
register int c; register int c;
register int text_size; register unsigned int text_size;
char *text = Malloc(text_size = ITEXTSIZE); char *text = Malloc(text_size = ITEXTSIZE);
register int pos = 0; register unsigned int pos = 0;
LoadChar(c); LoadChar(c);
@ -607,7 +607,7 @@ get_text(formals, length)
else else
text[pos++] = '\\'; text[pos++] = '\\';
if (pos == text_size) if (pos == text_size)
text = Srealloc(text, text_size += RTEXTSIZE); text = Srealloc(text, text_size <<= 1);
} }
else else
if ( c == '/') { if ( c == '/') {
@ -620,43 +620,45 @@ get_text(formals, length)
else else
text[pos++] = '/'; text[pos++] = '/';
if (pos == text_size) if (pos == text_size)
text = Srealloc(text, text_size += RTEXTSIZE); text = Srealloc(text, text_size <<= 1);
} }
else else
if (formals && class(c) == STIDF) { if (formals && class(c) == STIDF) {
char id_buf[IDFSIZE + 1]; char id_buf[IDFSIZE + 1];
register id_size = 0; register char *idp = id_buf;
register n; int n;
/* read identifier: it may be a formal parameter */ /* read identifier: it may be a formal parameter */
id_buf[id_size++] = c; *idp++ = c;
do { do {
LoadChar(c); LoadChar(c);
if (id_size <= IDFSIZE) if (idp <= &id_buf[IDFSIZE])
id_buf[id_size++] = c; *idp++ = c;
} while (in_idf(c)); } while (in_idf(c));
id_buf[--id_size] = '\0'; *--idp = '\0';
if (n = find_name(id_buf, formals)) { if (n = find_name(id_buf, formals)) {
/* construct the formal parameter mark */ /* construct the formal parameter mark */
text[pos++] = FORMALP | (char) n; text[pos++] = FORMALP | (char) n;
if (pos == text_size) if (pos == text_size)
text = Srealloc(text, text = Srealloc(text,
text_size += RTEXTSIZE); text_size <<= 1);
} }
else { else {
register char *ptr = &id_buf[0]; int sz = idp - id_buf;
while (pos + id_size >= text_size) idp = id_buf;
while (pos + sz >= text_size)
text = Srealloc(text, text = Srealloc(text,
text_size += RTEXTSIZE); text_size <<= 1);
while (text[pos++] = *ptr++) ; while (text[pos++] = *idp++) ;
pos--; pos--;
} }
} }
else { else {
text[pos++] = c; text[pos++] = c;
if (pos == text_size) if (pos == text_size)
text = Srealloc(text, text_size += RTEXTSIZE); text = Srealloc(text, text_size <<= 1);
LoadChar(c); LoadChar(c);
} }
} }

View file

@ -157,7 +157,7 @@ macro2buffer(idef, actpars, siztext)
If there are no parameters, this function behaves If there are no parameters, this function behaves
the same as strcpy(). the same as strcpy().
*/ */
register int size = idef->id_macro->mc_length + ITEXTSIZE; register unsigned int size = idef->id_macro->mc_length + ITEXTSIZE;
register char *text = Malloc(size); register char *text = Malloc(size);
register int pos = 0; register int pos = 0;
register char *ptr = idef->id_macro->mc_text; register char *ptr = idef->id_macro->mc_text;
@ -174,13 +174,13 @@ macro2buffer(idef, actpars, siztext)
for (p = actpars[n - 1]; *p; p++) { for (p = actpars[n - 1]; *p; p++) {
text[pos++] = *p; text[pos++] = *p;
if (pos == size) if (pos == size)
text = Srealloc(text, size += RTEXTSIZE); text = Srealloc(text, size <<= 1);
} }
} }
else { else {
text[pos++] = *ptr++; text[pos++] = *ptr++;
if (pos == size) if (pos == size)
text = Srealloc(text, size += RTEXTSIZE); text = Srealloc(text, size <<= 1);
} }
} }
text[pos] = '\0'; text[pos] = '\0';