cwfs: fix 1GB memsize limitation

the malloc pool allocator is limited in its allocation
size. as almost all data structures in cwfs are never
freed, use brk() in ialloc() instead of mallocalign().
this means memory returned by ialloc() cannot be freed!

to make sure we do not call free by accident, remove
the #define malloc(n) ialloc(n, 0) macro and use ialloc()
directly as in the original code to show the intend
of permanent allocations.
This commit is contained in:
cinap_lenrek 2014-05-03 00:51:45 +02:00
parent 72e4d850a4
commit 4c639475ce
13 changed files with 55 additions and 49 deletions

View file

@ -10,8 +10,6 @@
#include "dat.h" #include "dat.h"
#include "portfns.h" #include "portfns.h"
#define malloc(n) ialloc(n, 0)
#define CHAT(cp) ((cons.flags&chatflag) || \ #define CHAT(cp) ((cons.flags&chatflag) || \
((cp) && (((Chan*)(cp))->flags&chatflag))) ((cp) && (((Chan*)(cp))->flags&chatflag)))
#define QID9P1(a,b) (Qid9p1){a,b} #define QID9P1(a,b) (Qid9p1){a,b}

View file

@ -59,7 +59,6 @@ static void *
chkalloc(ulong n) chkalloc(ulong n)
{ {
char *p = mallocz(n, 1); char *p = mallocz(n, 1);
if (p == nil) if (p == nil)
panic("chkalloc: out of memory"); panic("chkalloc: out of memory");
return p; return p;

View file

@ -175,7 +175,7 @@ config1(int c)
Device *d, *t; Device *d, *t;
int m; int m;
d = malloc(sizeof(Device)); d = ialloc(sizeof(Device), 0);
do { do {
t = config(); t = config();
if(d->cat.first == 0) if(d->cat.first == 0)
@ -233,8 +233,7 @@ config(void)
if(f.error) if(f.error)
return devnone; return devnone;
d = malloc(sizeof(Device)); d = ialloc(sizeof(Device), 0);
c = *f.charp++; c = *f.charp++;
switch(c) { switch(c) {
default: default:
@ -277,7 +276,7 @@ config(void)
d->wren.ctrl = -1; d->wren.ctrl = -1;
d->wren.targ = -1; d->wren.targ = -1;
d->wren.lun = -1; d->wren.lun = -1;
d->wren.file = malloc((e - s) + 1); d->wren.file = ialloc((e - s) + 1, 0);
memmove(d->wren.file, s, e - s); memmove(d->wren.file, s, e - s);
d->wren.file[e - s] = 0; d->wren.file[e - s] = 0;
break; break;
@ -336,7 +335,7 @@ config(void)
d->type = Devcw; d->type = Devcw;
d->cw.c = config(); d->cw.c = config();
d->cw.w = config(); d->cw.w = config();
d->cw.ro = malloc(sizeof(Device)); d->cw.ro = ialloc(sizeof(Device), 0);
d->cw.ro->type = Devro; d->cw.ro->type = Devro;
d->cw.ro->ro.parent = d; d->cw.ro->ro.parent = d;
f.lastcw = d; f.lastcw = d;

View file

@ -400,7 +400,7 @@ cwinit1(Device *dev)
roflag = flag_install("ro", "-- ro reads and writes"); roflag = flag_install("ro", "-- ro reads and writes");
first = 1; first = 1;
} }
cw = malloc(sizeof(Cw)); cw = ialloc(sizeof(Cw), 0);
dev->private = cw; dev->private = cw;
cw->allflag = 0; cw->allflag = 0;

View file

@ -1,7 +1,7 @@
#include "all.h" #include "all.h"
#include "io.h" #include "io.h"
extern long nhiob; extern uint nhiob;
extern Hiob *hiob; extern Hiob *hiob;
Iobuf* Iobuf*

View file

@ -1166,7 +1166,7 @@ querychanger(Device *xdev)
* allocate a juke structure * allocate a juke structure
* no locking problems. * no locking problems.
*/ */
w = malloc(sizeof(Juke)); w = ialloc(sizeof(Juke), 0);
w->magic = Jukemagic; w->magic = Jukemagic;
w->isfixedsize = FIXEDSIZE; w->isfixedsize = FIXEDSIZE;
w->link = jukelist; w->link = jukelist;

View file

@ -71,7 +71,7 @@ mapinit(char *mapfile)
fields[0], mapfile); fields[0], mapfile);
continue; continue;
} }
map = malloc(sizeof *map); map = ialloc(sizeof(Map), 0);
map->from = strdup(fields[0]); map->from = strdup(fields[0]);
map->to = strdup(fields[1]); map->to = strdup(fields[1]);
map->fdev = iconfig(fields[0]); map->fdev = iconfig(fields[0]);
@ -323,15 +323,15 @@ main(int argc, char **argv)
netinit(); netinit();
scsiinit(); scsiinit();
files = malloc(conf.nfile * sizeof *files); files = ialloc(conf.nfile * sizeof(*files), 0);
for(i=0; i < conf.nfile; i++) { for(i=0; i < conf.nfile; i++) {
qlock(&files[i]); qlock(&files[i]);
qunlock(&files[i]); qunlock(&files[i]);
} }
wpaths = malloc(conf.nwpath * sizeof(*wpaths)); wpaths = ialloc(conf.nwpath * sizeof(*wpaths), 0);
uid = malloc(conf.nuid * sizeof(*uid)); uid = ialloc(conf.nuid * sizeof(*uid), 0);
gidspace = malloc(conf.gidspace * sizeof(*gidspace)); gidspace = ialloc(conf.gidspace * sizeof(*gidspace), 0);
iobufinit(); iobufinit();
@ -597,11 +597,12 @@ Devsize
inqsize(char *file) inqsize(char *file)
{ {
int nf; int nf;
char *ln, *end, *data = malloc(strlen(file) + 5 + 1); char *ln, *end, *data;
char *fields[4]; char *fields[4];
Devsize rv = -1; Devsize rv = -1;
Biobuf *bp; Biobuf *bp;
data = malloc(strlen(file) + 5 + 1);
strcpy(data, file); strcpy(data, file);
end = strstr(data, "/data"); end = strstr(data, "/data");
if (end == nil) if (end == nil)
@ -621,4 +622,3 @@ inqsize(char *file)
free(data); free(data);
return rv; return rv;
} }

View file

@ -1,10 +1,12 @@
#include "all.h" #include "all.h"
#include "io.h" #include "io.h"
static ulong #include <pool.h>
static uvlong
memsize(void) memsize(void)
{ {
ulong pgsize, pgmax, userpgs, userused; ulong pgsize, userpgs, userused;
char *s, *f[2]; char *s, *f[2];
int n, mpcnt; int n, mpcnt;
Biobuf *bp; Biobuf *bp;
@ -37,17 +39,13 @@ memsize(void)
if(mpcnt < 1) if(mpcnt < 1)
mpcnt = 1; mpcnt = 1;
userpgs = (userpgs*mpcnt)/100; userpgs = (userpgs*mpcnt)/100;
pgmax = (1024*1024*1024)/pgsize; /* 1GB max */ return (uvlong)userpgs*pgsize;
if(userpgs > pgmax)
userpgs = pgmax;
return userpgs*pgsize;
} }
return 16*MB; return 16*MB;
} }
uint niob;
long niob; uint nhiob;
long nhiob;
Hiob *hiob; Hiob *hiob;
/* /*
@ -56,14 +54,26 @@ Hiob *hiob;
* end of the allocated memory. * end of the allocated memory.
*/ */
void* void*
ialloc(ulong n, int align) ialloc(uintptr n, int align)
{ {
void *p = mallocalign(n, align, 0, 0); char *p;
int m;
if (p == nil) if(align <= 0)
align = sizeof(uintptr);
mainmem->lock(mainmem);
p = sbrk(0);
if(m = n % align)
n += align - m;
if(m = (uintptr)p % align)
p += align - m;
if(brk(p+n) < 0)
panic("ialloc: out of memory"); panic("ialloc: out of memory");
setmalloctag(p, getcallerpc(&n));
memset(p, 0, n); mainmem->unlock(mainmem);
return p; return p;
} }
@ -89,7 +99,7 @@ iobufinit(void)
while(!prime(nhiob)) while(!prime(nhiob))
nhiob++; nhiob++;
if(chatty) if(chatty)
print("\t%ld buffers; %ld hashes\n", niob, nhiob); print("\t%ud buffers; %ud hashes\n", niob, nhiob);
hiob = ialloc(nhiob * sizeof(Hiob), 0); hiob = ialloc(nhiob * sizeof(Hiob), 0);
hp = hiob; hp = hiob;
for(i=0; i<nhiob; i++) { for(i=0; i<nhiob; i++) {

View file

@ -14,7 +14,7 @@ mcatinit(Device *d)
d->cat.ndev++; d->cat.ndev++;
} }
list = malloc(d->cat.ndev*sizeof(Device*)); list = ialloc(d->cat.ndev * sizeof(Device*), 0);
d->private = list; d->private = list;
for(x=d->cat.first; x; x=x->link) { for(x=d->cat.first; x; x=x->link) {
*list++ = x; *list++ = x;

View file

@ -95,7 +95,7 @@ Chan* getlcp(uchar*, long);
Off getraddr(Device*); Off getraddr(Device*);
void hexdump(void*, int); void hexdump(void*, int);
int iaccess(File*, Dentry*, int); int iaccess(File*, Dentry*, int);
void* ialloc(ulong, int); void* ialloc(uintptr, int);
Off ibbpow(int); Off ibbpow(int);
Off ibbpowsum(int); Off ibbpowsum(int);
Device* iconfig(char *); Device* iconfig(char *);

View file

@ -46,8 +46,8 @@ scsiinit(void)
tp->ctlrno = ctlrno; tp->ctlrno = ctlrno;
tp->targetno = targetno; tp->targetno = targetno;
tp->inquiry = malloc(Ninquiry); tp->inquiry = ialloc(Ninquiry, 0);
tp->sense = malloc(Nsense); tp->sense = ialloc(Nsense, 0);
} }
} }
} }

View file

@ -38,7 +38,7 @@ fs_chaninit(int count, int data)
Chan *cp, *icp; Chan *cp, *icp;
int i; int i;
p = malloc(count * (sizeof(Chan)+data)); p = ialloc(count * (sizeof(Chan)+data), 0);
icp = (Chan*)p; icp = (Chan*)p;
for(i = 0; i < count; i++) { for(i = 0; i < count; i++) {
cp = (Chan*)p; cp = (Chan*)p;
@ -761,17 +761,17 @@ mbinit(void)
msgalloc.lmsgbuf = 0; msgalloc.lmsgbuf = 0;
msgalloc.smsgbuf = 0; msgalloc.smsgbuf = 0;
for(i=0; i<conf.nlgmsg; i++) { for(i=0; i<conf.nlgmsg; i++) {
mb = malloc(sizeof(Msgbuf)); mb = ialloc(sizeof(Msgbuf), 0);
mb->magic = Mbmagic; mb->magic = Mbmagic;
mb->xdata = malloc(LARGEBUF+Slop); mb->xdata = ialloc(LARGEBUF+Slop, 0);
mb->flags = LARGE; mb->flags = LARGE;
mbfree(mb); mbfree(mb);
cons.nlarge++; cons.nlarge++;
} }
for(i=0; i<conf.nsmmsg; i++) { for(i=0; i<conf.nsmmsg; i++) {
mb = malloc(sizeof(Msgbuf)); mb = ialloc(sizeof(Msgbuf), 0);
mb->magic = Mbmagic; mb->magic = Mbmagic;
mb->xdata = malloc(SMALLBUF+Slop); mb->xdata = ialloc(SMALLBUF+Slop, 0);
mb->flags = 0; mb->flags = 0;
mbfree(mb); mbfree(mb);
cons.nsmall++; cons.nsmall++;
@ -782,7 +782,7 @@ mbinit(void)
unlock(&rabuflock); unlock(&rabuflock);
rabuffree = 0; rabuffree = 0;
for(i=0; i<1000; i++) { for(i=0; i<1000; i++) {
rb = malloc(sizeof(*rb)); rb = ialloc(sizeof(*rb), 0);
rb->link = rabuffree; rb->link = rabuffree;
rabuffree = rb; rabuffree = rb;
} }
@ -799,8 +799,8 @@ mballoc(int count, Chan *cp, int category)
panic("msgbuf count"); panic("msgbuf count");
mb = msgalloc.lmsgbuf; mb = msgalloc.lmsgbuf;
if(mb == nil) { if(mb == nil) {
mb = malloc(sizeof(Msgbuf)); mb = ialloc(sizeof(Msgbuf), 0);
mb->xdata = malloc(LARGEBUF+Slop); mb->xdata = ialloc(LARGEBUF+Slop, 0);
cons.nlarge++; cons.nlarge++;
} else } else
msgalloc.lmsgbuf = mb->next; msgalloc.lmsgbuf = mb->next;
@ -808,8 +808,8 @@ mballoc(int count, Chan *cp, int category)
} else { } else {
mb = msgalloc.smsgbuf; mb = msgalloc.smsgbuf;
if(mb == nil) { if(mb == nil) {
mb = malloc(sizeof(Msgbuf)); mb = ialloc(sizeof(Msgbuf), 0);
mb->xdata = malloc(SMALLBUF+Slop); mb->xdata = ialloc(SMALLBUF+Slop, 0);
cons.nsmall++; cons.nsmall++;
} else } else
msgalloc.smsgbuf = mb->next; msgalloc.smsgbuf = mb->next;
@ -958,7 +958,7 @@ newqueue(int size, char *name)
{ {
Queue *q; Queue *q;
q = malloc(sizeof(Queue) + (size-1)*sizeof(void*)); q = ialloc(sizeof(Queue) + (size-1)*sizeof(void*), 0);
q->size = size; q->size = size;
q->avail = size; q->avail = size;
q->count = 0; q->count = 0;

View file

@ -39,7 +39,7 @@ wreninit(Device *d)
if(d->private) if(d->private)
return; return;
d->private = dr = malloc(sizeof(Wren)); d->private = dr = ialloc(sizeof(Wren), 0);
if (d->wren.file) if (d->wren.file)
d->wren.sddata = dataof(d->wren.file); d->wren.sddata = dataof(d->wren.file);