From 322f72313d66550698e918707ea5d32e44da9dca Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Mon, 14 Nov 2011 12:59:48 +0100 Subject: [PATCH] mothra: limit amount of image data kept in devdraw to 8MB --- sys/src/cmd/mothra/getpix.c | 21 +++++++++++---------- sys/src/cmd/mothra/mothra.c | 23 +++++++++++++++++------ sys/src/cmd/mothra/mothra.h | 5 ++++- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/sys/src/cmd/mothra/getpix.c b/sys/src/cmd/mothra/getpix.c index 2979f5f94..d82957c3c 100644 --- a/sys/src/cmd/mothra/getpix.c +++ b/sys/src/cmd/mothra/getpix.c @@ -21,12 +21,6 @@ char *pixcmd[]={ [BMP] "bmp -9t", }; -void storebitmap(Rtext *t, Image *b){ - t->b=b; - free(t->text); - t->text=0; -} - void getimage(Rtext *t, Www *w){ int pfd[2]; Action *ap; @@ -40,7 +34,7 @@ void getimage(Rtext *t, Www *w){ seturl(&url, ap->image, w->url->fullname); for(p=w->pix;p!=nil; p=p->next) if(strcmp(ap->image, p->name)==0 && ap->width==p->width && ap->height==p->height){ - storebitmap(t, p->b); + t->b = p->b; w->changed=1; return; } @@ -87,7 +81,7 @@ void getimage(Rtext *t, Www *w){ p->height=ap->height; p->next=w->pix; w->pix=p; - storebitmap(t, b); + t->b=b; w->changed=1; } @@ -144,8 +138,15 @@ void getpix(Rtext *t, Www *w){ } } -void freepix(void *p) -{ +ulong countpix(void *p){ + ulong n=0; + Pix *x; + for(x = p; x; x = x->next) + n += Dy(x->b->r)*bytesperline(x->b->r, x->b->depth); + return n; +} + +void freepix(void *p){ Pix *x, *xx; xx = p; while(x = xx){ diff --git a/sys/src/cmd/mothra/mothra.c b/sys/src/cmd/mothra/mothra.c index ee0144494..e8d4ba019 100644 --- a/sys/src/cmd/mothra/mothra.c +++ b/sys/src/cmd/mothra/mothra.c @@ -961,9 +961,10 @@ void freeurl(Url *u){ * get the file at the given url */ void geturl(char *urlname, int method, char *body, int plumb, int map){ - int i, fd, typ; + int i, fd, typ, pfd[2]; char cmd[NNAME]; - int pfd[2]; + Rtext *t; + ulong n; Www *w; if(*urlname == '#'){ @@ -1013,18 +1014,28 @@ void geturl(char *urlname, int method, char *body, int plumb, int map){ case HTML: fd = pipeline("/bin/uhtml", fd); case PLAIN: + n=0; + for(i=wwwtop-1; i>=0 && i!=(wwwtop-NWWW-1); i--){ + w = www(i); + n += countpix(w->pix); + if(n < NPIXMB*1024*1024) + continue; + if(!w->finished && !w->alldone) + continue; + for(t=w->text; t; t=t->next) + if(t->b && t->user) + t->b=0; + freepix(w->pix); + w->pix=0; + } w = www(i = wwwtop++); if(i >= NWWW){ - extern void freeform(void *p); - extern void freepix(void *p); - /* wait for the reader to finish the document */ while(!w->finished && !w->alldone){ unlockdisplay(display); sleep(10); lockdisplay(display); } - freetext(w->text); freeform(w->form); freepix(w->pix); diff --git a/sys/src/cmd/mothra/mothra.h b/sys/src/cmd/mothra/mothra.h index 288fb2366..8c45b8ec6 100644 --- a/sys/src/cmd/mothra/mothra.h +++ b/sys/src/cmd/mothra/mothra.h @@ -1,6 +1,7 @@ enum{ NWWW=64, /* # of pages we hold in the log */ NXPROC=5, /* # of parallel procs loading the pix */ + NPIXMB=8, /* megabytes of image data to keep arround */ NNAME=512, NLINE=256, NAUTH=128, @@ -82,12 +83,14 @@ void plrdplain(char *, int, Www *); void htmlerror(char *, int, char *, ...); /* user-supplied routine */ void seturl(Url *, char *, char *); void getpix(Rtext *, Www *); +ulong countpix(void *p); +void freepix(void *p); int pipeline(char *, int); int urlopen(Url *, int, char *); void getfonts(void); void *emalloc(int); void *emallocz(int, int); -void setbitmap(Rtext *); +void freeform(void *p); void message(char *, ...); int snooptype(int fd); void mkfieldpanel(Rtext *);