cc: remove mysbrk(), exponentially increase gethunk() allocation delta

mysbrk() was only used in gethunk() and should not be
called by anyone, so dont export the symbol.

simplify gethunk() using brk().

double allocation size on each call until we reach
1000*NHUNK.

use signed long for nhunk as alignment rountin might
make it negative and handle that case.
This commit is contained in:
cinap_lenrek 2020-04-11 14:19:35 +02:00
parent 49c159b50f
commit ca9d65e40b
2 changed files with 20 additions and 19 deletions

View file

@ -4,12 +4,6 @@ myaccess(char *f)
return access(f, AEXIST);
}
void*
mysbrk(ulong size)
{
return sbrk(size);
}
int
mycreat(char *n, int p)
{
@ -77,27 +71,35 @@ myfork(void)
return fork();
}
/*
* real allocs
*/
extern char end[];
char* hunk = end;
long nhunk;
uintptr thunk;
void
gethunk(void)
{
char *h;
ulong nh;
long nh;
nh = NHUNK;
if(thunk >= 10L*NHUNK)
nh = 10L*NHUNK;
h = (char*)mysbrk(nh);
if(h == (char*)-1)
sysfatal("out of memory");
if(nhunk == 0)
hunk = h;
if(thunk < NHUNK)
nh = NHUNK;
else if(thunk < 1000*NHUNK)
nh = thunk;
else
nh += (h - hunk) - nhunk;
nh = 1000*NHUNK;
if(nhunk < 0)
nhunk = 0;
nhunk += nh;
thunk += nh;
if(brk(hunk+nhunk) < 0)
sysfatal("out of memory");
}
void*

View file

@ -23,12 +23,11 @@ EXTERN int myexec(char*, char*[]);
EXTERN int mydup(int, int);
EXTERN int myfork(void);
EXTERN int mypipe(int*);
EXTERN void* mysbrk(ulong);
EXTERN void gethunk(void);
EXTERN char* hunk;
EXTERN uintptr nhunk;
EXTERN long nhunk;
EXTERN uintptr thunk;
EXTERN void* alloc(long n);