sorry about libhtml-alloc; i missed one.
remove _ltoStr; fix memory problems in targetmapinit (uninitialized), buftostr (leak),
begin reducing stack requirments.
Reference: /n/sources/patch/applied/libhtml-alloc2
Date: Sat Oct 28 01:40:13 CES 2006
Signed-off-by: quanstro@quanstro.net
--- /sys/src/libhtml/utils.c Sat Oct 28 01:38:03 2006
+++ /sys/src/libhtml/utils.c Sat Oct 28 01:38:00 2006
@@ -578,14 +578,3 @@
return ans;
}
-
-// Convert n to emalloc'd String.
-Rune*
-_ltoStr(int n)
-{
- int m;
- uchar buf[20];
-
- m = snprint((char*)buf, sizeof(buf), "%d", n);
- return toStr(buf, m, US_Ascii);
-}
--- /sys/src/libhtml/impl.h Sat Oct 28 01:38:10 2006
+++ /sys/src/libhtml/impl.h Sat Oct 28 01:38:09 2006
@@ -18,7 +18,6 @@
extern int _inclass(Rune c, Rune* cl);
extern int _listlen(List* l);
-extern Rune* _ltoStr(int n);
extern List* _newlist(int val, List* rest);
extern Rune* _newstr(int n);
extern int _prefix(Rune* pre, Rune* s);
--- /sys/src/libhtml/lex.c Sat Oct 28 01:38:28 2006
+++ /sys/src/libhtml/lex.c Sat Oct 28 01:38:22 2006
@@ -537,7 +537,7 @@
static StringInt* tagtable; // initialized from tagnames
static StringInt* attrtable; // initialized from attrnames
-static void lexinit();
+static void lexinit(void);
static int getplaindata(TokenSource* ts, Token* a, int* pai);
static int getdata(TokenSource* ts, int firstc, int starti, Token* a, int* pai);
static int getscriptdata(TokenSource* ts, int firstc, int starti, Token* a, int* pai, int findtag);
@@ -584,7 +584,7 @@
}
enum {
- ToksChunk = 500
+ ToksChunk = 500,
};
// Call this to get the tokens.
@@ -603,16 +603,16 @@
if(!lexinited)
lexinit();
ts = newtokensource(data, datalen, chset, mtype);
- alen = ToksChunk;
- a = (Token*)emalloc(alen * sizeof(Token));
- ai = 0;
if(dbglex)
fprint(2, "_gettoks starts, ts.i=%d, ts.edata=%d\n", ts->i, ts->edata);
+ alen = 0;
+ ai = 0;
+ a = 0;
if(ts->mtype == TextHtml) {
for(;;) {
if(ai == alen) {
- a = (Token*)erealloc(a, (alen+ToksChunk)*sizeof(Token));
alen += ToksChunk;
+ a = erealloc(a, alen*sizeof *a);
}
starti = ts->i;
c = getchar(ts);
@@ -639,8 +639,8 @@
// plain text (non-html) tokens
for(;;) {
if(ai == alen) {
- a = (Token*)erealloc(a, (alen+ToksChunk)*sizeof(Token));
alen += ToksChunk;
+ a = erealloc(a, alen*sizeof *a);
}
tag = getplaindata(ts, a, &ai);
if(tag == -1)
@@ -653,8 +653,10 @@
if(dbglex)
fprint(2, "lex: returning %d tokens\n", ai);
*plen = ai;
- if(ai == 0)
- return nil;
+ if(ai == 0){
+ free(a);
+ a = 0;
+ }
return a;
}
@@ -696,7 +698,7 @@
}
if(c != 0) {
buf[j++] = c;
- if(j == sizeof(buf)-1) {
+ if(j == nelem(buf)-1) {
s = buftostr(s, buf, j);
j = 0;
}
@@ -719,11 +721,16 @@
static Rune*
buftostr(Rune* s, Rune* buf, int j)
{
- buf[j] = 0;
+ int i;
+
if(s == nil)
s = _Strndup(buf, j);
- else
- s = _Strdup2(s, buf);
+ else {
+ i = _Strlen(s);
+ s = realloc(s, ( i+j+1)*sizeof *s);
+ memcpy(&s[i], buf, j*sizeof *s);
+ s[i+j] = 0;
+ }
return s;
}
@@ -739,12 +746,11 @@
int j;
int c;
Token* tok;
- Rune buf[BIGBUFSIZE];
+ Rune buf[SMALLBUFSIZE];
s = nil;
j = 0;
- c = firstc;
- while(c >= 0) {
+ for(c = firstc; c >= 0; c = getchar(ts)){
if(c == '&') {
c = ampersand(ts);
if(c < 0)
@@ -775,12 +781,11 @@
}
if(c != 0) {
buf[j++] = c;
- if(j == BIGBUFSIZE-1) {
+ if(j == nelem(buf)-1) {
s = buftostr(s, buf, j);
j = 0;
}
}
- c = getchar(ts);
}
s = buftostr(s, buf, j);
if(s == nil)
@@ -819,8 +824,6 @@
savei = ts->i;
c = getchar(ts);
if(c == '!') {
-// while(c >= 0 && c != '\n' && c != '\r')
-// c = getchar(ts);
if(comment(ts) == -1)
break;
if(c == '\r')
@@ -848,7 +851,7 @@
break;
if(c != 0) {
buf[j++] = c;
- if(j == BIGBUFSIZE-1) {
+ if(j == nelem(buf)-1) {
s = buftostr(s, buf, j);
j = 0;
}
@@ -865,6 +868,7 @@
tok->starti = starti;
return Data;
}
+ free(s);
backup(ts, starti);
return -1;
}
@@ -942,7 +946,6 @@
tok->tag = tag + rbra;
else
tok->text = _Strndup(buf, i); // for warning print, in build
-
// attribute gathering loop
al = nil;
while(1) {
@@ -1211,7 +1214,7 @@
int ans;
int v;
int k;
- Rune buf[SMALLBUFSIZE];
+ Rune buf[25];
savei = ts->i;
c = getchar(ts);
@@ -1258,7 +1261,7 @@
if(c < 0)
break;
if(c < 256 && (isalpha(c) || isdigit(c))) {
- if(k < SMALLBUFSIZE-1)
+ if(k < nelem(buf)-1)
buf[k++] = c;
}
else {
--- /sys/src/libhtml/build.c Sat Oct 28 01:39:02 2006
+++ /sys/src/libhtml/build.c Sat Oct 28 01:38:45 2006
@@ -908,11 +908,8 @@
ks->kidinfos = kd = newkidinfo(0, ks->kidinfos);
kd->src = aurlval(tok, Asrc, nil, di->base);
kd->name = aval(tok, Aname);
- if(kd->name == nil) {
- s = _ltoStr(++is->nframes);
- kd->name = _Strdup2(L"_fr", s);
- free(s);
- }
+ if(kd->name == nil)
+ kd->name = runesmprint("_fr%d", ++is->nframes);
kd->marginw = auintval(tok, Amarginwidth, 0);
kd->marginh = auintval(tok, Amarginheight, 0);
kd->framebd = auintval(tok, Aframeborder, 1);
@@ -2635,12 +2632,7 @@
break;
case LT1:
- t = _ltoStr(n);
- n2 = _Strlen(t);
- s = _newstr(n2+1);
- t = _Stradd(s, t, n2);
- *t++ = '.';
- *t = 0;
+ s = runesmprint("%d.", n);
break;
case LTa:
@@ -3887,9 +3879,12 @@
static void
targetmapinit(void)
{
+ int l;
+
targetmapsize = 10;
- targetmap = (StringInt*)emalloc(targetmapsize*sizeof(StringInt));
- memset(targetmap, 0, targetmapsize*sizeof(StringInt));
+ l = targetmapsize*sizeof *targetmap;
+ targetmap = emalloc(l);
+ memset(targetmap, 0, l);
targetmap[0].key = _Strdup(L"_top");
targetmap[0].val = FTtop;
targetmap[1].key = _Strdup(L"_self");
@@ -3913,12 +3908,11 @@
for(i = 0; i < ntargets; i++)
if(_Strcmp(s, targetmap[i].key) == 0)
return targetmap[i].val;
- if(i >= targetmapsize) {
+ if(i == targetmapsize) {
targetmapsize += 10;
- targetmap = (StringInt*)erealloc(targetmap, targetmapsize*sizeof(StringInt));
+ targetmap = erealloc(targetmap, targetmapsize*sizeof(StringInt));
}
- targetmap[i].key = (Rune*)emalloc((n+1)*sizeof(Rune));
- memmove(targetmap[i].key, s, (n+1)*sizeof(Rune));
+ targetmap[i].key = _Strdup(s);
targetmap[i].val = i;
ntargets++;
return i;