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 "portfns.h"
#define malloc(n) ialloc(n, 0)
#define CHAT(cp) ((cons.flags&chatflag) || \
((cp) && (((Chan*)(cp))->flags&chatflag)))
#define QID9P1(a,b) (Qid9p1){a,b}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,10 +1,12 @@
#include "all.h"
#include "io.h"
static ulong
#include <pool.h>
static uvlong
memsize(void)
{
ulong pgsize, pgmax, userpgs, userused;
ulong pgsize, userpgs, userused;
char *s, *f[2];
int n, mpcnt;
Biobuf *bp;
@ -37,17 +39,13 @@ memsize(void)
if(mpcnt < 1)
mpcnt = 1;
userpgs = (userpgs*mpcnt)/100;
pgmax = (1024*1024*1024)/pgsize; /* 1GB max */
if(userpgs > pgmax)
userpgs = pgmax;
return userpgs*pgsize;
return (uvlong)userpgs*pgsize;
}
return 16*MB;
}
long niob;
long nhiob;
uint niob;
uint nhiob;
Hiob *hiob;
/*
@ -56,14 +54,26 @@ Hiob *hiob;
* end of the allocated memory.
*/
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");
setmalloctag(p, getcallerpc(&n));
memset(p, 0, n);
mainmem->unlock(mainmem);
return p;
}
@ -89,7 +99,7 @@ iobufinit(void)
while(!prime(nhiob))
nhiob++;
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);
hp = hiob;
for(i=0; i<nhiob; i++) {

View file

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

View file

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

View file

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

View file

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

View file

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