#include "defs.h"
#define BUSY 01
#define busy(x) (Rcheat((x)->word) & BUSY)
unsigned brkincr = BRKINCR;
struct blk *blokp;
struct blk *bloktop;
unsigned char *brkbegin;
extern unsigned char *setbrk();
#ifdef DEBUG
#define PAT_MAGIC 0xfeedface
#define PAT_INIT 0xbaddcafe
#define PAT_FREE 0xdeadbeef
static void fill_pat(struct blk *, uint32_t);
static void chkbptr(struct blk *);
#endif
void *
alloc(size_t nbytes)
{
size_t rbytes = round(nbytes + ALIGNSIZ, ALIGNSIZ);
if (stakbot == 0) {
addblok((unsigned int)0);
}
for (;;) {
int c = 0;
struct blk *p = blokp;
struct blk *q;
do
{
if (!busy(p)) {
while (!busy(q = p->word))
p->word = q->word;
if ((char *)q - (char *)p >= rbytes) {
blokp = (struct blk *)
((char *)p + rbytes);
if (q > blokp)
blokp->word = p->word;
p->word = (struct blk *)
(Rcheat(blokp) | BUSY);
#ifdef DEBUG
fill_pat(p, PAT_INIT);
#endif
return ((char *)(p + 1));
}
}
q = p;
p = (struct blk *)(Rcheat(p->word) & ~BUSY);
} while (p > q || (c++) == 0);
addblok(rbytes);
}
}
void
addblok(unsigned int reqd)
{
if (stakbot == 0) {
brkbegin = setbrk(3 * BRKINCR);
brkbegin = (unsigned char *)round(brkbegin, ALIGNSIZ);
bloktop = (struct blk *)brkbegin;
}
if (stakbas != staktop) {
unsigned char *rndstak;
struct blk *blokstak;
if (staktop >= brkend)
growstak(staktop);
pushstak(0);
rndstak = (unsigned char *)round(staktop, ALIGNSIZ);
blokstak = (struct blk *)(stakbas) - 1;
blokstak->word = stakbsy;
stakbsy = blokstak;
bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY);
bloktop = (struct blk *)(rndstak);
}
reqd += brkincr;
reqd &= ~(brkincr - 1);
blokp = bloktop;
if ((unsigned char *)&bloktop->word >= brkend) {
if (setbrk((unsigned)((unsigned char *)
(&bloktop->word) - brkend + sizeof (struct blk))) ==
(unsigned char *)-1)
error(nospace);
}
bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd);
if ((unsigned char *)&bloktop->word >= brkend) {
if (setbrk((unsigned)((unsigned char *)
(&bloktop->word) - brkend + sizeof (struct blk))) ==
(unsigned char *)-1)
error(nospace);
}
bloktop->word = (struct blk *)(brkbegin + 1);
{
unsigned char *stakadr = (unsigned char *)
(bloktop + 2);
unsigned char *sp = stakadr;
if (reqd = (staktop-stakbot)) {
if (stakadr + reqd >= brkend)
growstak(stakadr + reqd);
while (reqd-- > 0)
*sp++ = *stakbot++;
sp--;
}
staktop = sp;
if (staktop >= brkend)
growstak(staktop);
stakbas = stakbot = stakadr;
}
}
void
free(ap)
void *ap;
{
struct blk *p;
if ((p = (struct blk *)ap) && p < bloktop && p > (struct blk *)brkbegin)
{
#ifdef DEBUG
chkbptr(p);
#endif
--p;
p->word = (struct blk *)(Rcheat(p->word) & ~BUSY);
#ifdef DEBUG
fill_pat(p, PAT_FREE);
#endif
}
}
#ifdef DEBUG
static void
fill_pat(struct blk *ptr, uint32_t pat)
{
uint32_t *ui, *eui;
*(uint32_t *)ptr->pad = PAT_MAGIC;
eui = (uint32_t *)(Rcheat(ptr->word) & ~BUSY);
for (ui = (uint32_t *)(ptr + 1); ui < eui; ui++)
*ui = pat;
}
static void
chkbptr(struct blk *ptr)
{
int exf = 0;
struct blk *p = (struct blk *)brkbegin;
struct blk *q;
int us = 0, un = 0;
for (;;) {
q = (struct blk *)(Rcheat(p->word) & ~BUSY);
if (p+1 == ptr)
exf++;
if (q < (struct blk *)brkbegin || q > bloktop)
abort();
if (p == bloktop)
break;
if (busy(p))
us += q - p;
else
un += q - p;
if (p >= q)
abort();
p = q;
}
if (exf == 0)
abort();
}
static void
chkmem()
{
struct blk *p = (struct blk *)brkbegin;
struct blk *q;
int us = 0, un = 0;
for (;;) {
q = (struct blk *)(Rcheat(p->word) & ~BUSY);
if (q < (struct blk *)brkbegin || q > bloktop)
abort();
if (p == bloktop)
break;
if (busy(p))
us += q - p;
else
un += q - p;
if (p >= q)
abort();
p = q;
}
prs("un/used/avail ");
prn(un);
blank();
prn(us);
blank();
prn((uintptr_t)bloktop - (uintptr_t)brkbegin - (un + us));
newline();
}
#endif
size_t
blklen(q)
char *q;
{
struct blk *pp = (struct blk *)q;
struct blk *p;
--pp;
p = (struct blk *)(Rcheat(pp->word) & ~BUSY);
return ((size_t)((long)p - (long)q));
}
#ifdef __STDC__
void *
realloc(pp, nbytes)
void *pp;
size_t nbytes;
#else
char *
realloc(pp, nbytes)
char *pp;
size_t nbytes;
#endif
{
char *q;
size_t blen;
if (pp == NULL)
return (alloc(nbytes));
if ((nbytes == 0) && (pp != NULL))
free(pp);
blen = blklen(pp);
if (blen < nbytes) {
q = alloc(nbytes);
memcpy(q, pp, blen);
free(pp);
return ((char *)q);
} else if (blen == nbytes) {
return (pp);
} else {
q = alloc(nbytes);
memcpy(q, pp, nbytes);
free(pp);
return ((char *)q);
}
#ifdef undef
p = (struct blk *)pp;
if (sizeof (p) < nbytes) {
q = alloc(nbytes);
memcpy(q, pp, sizeof (p));
free(pp);
return ((char *)q);
} else if (sizeof (p) == nbytes) {
return (pp);
} else {
q = alloc(nbytes);
memcpy(q, pp, nbytes);
free(pp);
return ((char *)q);
}
#endif
}