1989-11-30 14:59:18 +00:00
|
|
|
/* replace undef by define */
|
|
|
|
#undef DEBUG /* check assertions */
|
|
|
|
#undef SLOWDEBUG /* some extra test loops (requires DEBUG) */
|
1987-01-27 15:57:55 +00:00
|
|
|
|
1989-11-30 14:59:18 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define ASSERT(b) if (!(b)) assert_failed();
|
1987-01-27 15:57:55 +00:00
|
|
|
#else
|
1989-11-30 14:59:18 +00:00
|
|
|
#define ASSERT(b) /* empty */
|
1987-01-27 15:57:55 +00:00
|
|
|
#endif
|
|
|
|
|
1990-06-12 11:22:49 +00:00
|
|
|
#if EM_WSIZE == EM_PSIZE
|
1989-11-30 14:59:18 +00:00
|
|
|
#define ptrint int
|
1987-01-27 15:57:55 +00:00
|
|
|
#else
|
1989-11-30 14:59:18 +00:00
|
|
|
#define ptrint long
|
1987-01-27 15:57:55 +00:00
|
|
|
#endif
|
|
|
|
|
1990-01-19 11:30:16 +00:00
|
|
|
#if EM_PSIZE == 2
|
|
|
|
#define BRKSIZE 1024
|
|
|
|
#else
|
1989-11-30 14:59:18 +00:00
|
|
|
#define BRKSIZE 4096
|
1990-01-19 11:30:16 +00:00
|
|
|
#endif
|
1989-11-30 14:59:18 +00:00
|
|
|
#define PTRSIZE sizeof(char *)
|
1990-12-06 11:34:50 +00:00
|
|
|
#define Align(x,a) (((x) + (a - 1)) & ~(ptrint)(a - 1))
|
1989-11-30 14:59:18 +00:00
|
|
|
#define NextSlot(p) (* (char **) ((p) - PTRSIZE))
|
|
|
|
#define NextFree(p) (* (char **) (p))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A short explanation of the data structure and algorithms.
|
|
|
|
* An area returned by malloc() is called a slot. Each slot
|
|
|
|
* contains the number of bytes requested, but preceeded by
|
|
|
|
* an extra pointer to the next the slot in memory.
|
|
|
|
* '_bottom' and '_top' point to the first/last slot.
|
|
|
|
* More memory is asked for using brk() and appended to top.
|
|
|
|
* The list of free slots is maintained to keep malloc() fast.
|
|
|
|
* '_empty' points the the first free slot. Free slots are
|
|
|
|
* linked together by a pointer at the start of the
|
|
|
|
* user visable part, so just after the next-slot pointer.
|
|
|
|
* Free slots are merged together by free().
|
|
|
|
*/
|
|
|
|
|
1987-10-15 12:59:03 +00:00
|
|
|
extern char *sbrk(), *brk();
|
1989-11-30 14:59:18 +00:00
|
|
|
static char *_bottom, *_top, *_empty;
|
1987-01-27 15:57:55 +00:00
|
|
|
|
|
|
|
static grow(len)
|
|
|
|
unsigned len;
|
|
|
|
{
|
|
|
|
register char *p;
|
|
|
|
|
1989-11-30 14:59:18 +00:00
|
|
|
ASSERT(NextSlot(_top) == 0);
|
|
|
|
p = (char *) Align((ptrint)_top + len, BRKSIZE);
|
|
|
|
if (p < _top || brk(p) != 0)
|
|
|
|
return(0);
|
|
|
|
NextSlot(_top) = p;
|
|
|
|
NextSlot(p) = 0;
|
|
|
|
free(_top);
|
|
|
|
_top = p;
|
|
|
|
return(1);
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *malloc(size)
|
|
|
|
unsigned size;
|
|
|
|
{
|
1989-11-30 14:59:18 +00:00
|
|
|
register char *prev, *p, *next, *new;
|
|
|
|
register unsigned len, ntries;
|
1987-01-27 15:57:55 +00:00
|
|
|
|
1989-11-30 14:59:18 +00:00
|
|
|
if (size == 0)
|
|
|
|
size = PTRSIZE; /* avoid slots less that 2*PTRSIZE */
|
|
|
|
for (ntries = 0; ntries < 2; ntries++) {
|
|
|
|
len = Align(size, PTRSIZE) + PTRSIZE;
|
|
|
|
if (_bottom == 0) {
|
|
|
|
p = sbrk(2 * PTRSIZE);
|
|
|
|
p = (char *) Align((ptrint)p, PTRSIZE);
|
|
|
|
p += PTRSIZE;
|
|
|
|
_top = _bottom = p;
|
|
|
|
NextSlot(p) = 0;
|
|
|
|
}
|
|
|
|
#ifdef SLOWDEBUG
|
|
|
|
for (p = _bottom; (next = NextSlot(p)) != 0; p = next)
|
|
|
|
ASSERT(next > p);
|
|
|
|
ASSERT(p == _top);
|
|
|
|
#endif
|
|
|
|
for (prev = 0, p = _empty; p != 0; prev = p, p = NextFree(p)) {
|
|
|
|
next = NextSlot(p);
|
|
|
|
new = p + len;
|
|
|
|
if (new > next)
|
|
|
|
continue; /* too small */
|
|
|
|
if (new + PTRSIZE < next) { /* too big, so split */
|
|
|
|
/* + PTRSIZE avoids tiny slots on free list */
|
|
|
|
NextSlot(new) = next;
|
|
|
|
NextSlot(p) = new;
|
|
|
|
NextFree(new) = NextFree(p);
|
|
|
|
NextFree(p) = new;
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
1989-11-30 14:59:18 +00:00
|
|
|
if (prev)
|
|
|
|
NextFree(prev) = NextFree(p);
|
|
|
|
else
|
|
|
|
_empty = NextFree(p);
|
|
|
|
return(p);
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
1989-11-30 14:59:18 +00:00
|
|
|
if (grow(len) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ASSERT(ntries != 2);
|
|
|
|
return(0);
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *realloc(old, size)
|
|
|
|
char *old;
|
|
|
|
unsigned size;
|
|
|
|
{
|
1989-11-30 14:59:18 +00:00
|
|
|
register char *prev, *p, *next, *new;
|
|
|
|
register unsigned len, n;
|
1987-01-27 15:57:55 +00:00
|
|
|
|
1989-11-30 14:59:18 +00:00
|
|
|
len = Align(size, PTRSIZE) + PTRSIZE;
|
|
|
|
next = NextSlot(old);
|
|
|
|
n = (int)(next - old); /* old length */
|
|
|
|
/*
|
|
|
|
* extend old if there is any free space just behind it
|
|
|
|
*/
|
|
|
|
for (prev = 0, p = _empty; p != 0; prev = p, p = NextFree(p)) {
|
|
|
|
if (p > next)
|
|
|
|
break;
|
|
|
|
if (p == next) { /* 'next' is a free slot: merge */
|
|
|
|
NextSlot(old) = NextSlot(p);
|
|
|
|
if (prev)
|
|
|
|
NextFree(prev) = NextFree(p);
|
|
|
|
else
|
|
|
|
_empty = NextFree(p);
|
|
|
|
next = NextSlot(old);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new = old + len;
|
|
|
|
/*
|
|
|
|
* Can we use the old, possibly extended slot?
|
|
|
|
*/
|
|
|
|
if (new <= next) { /* it does fit */
|
|
|
|
if (new + PTRSIZE < next) { /* too big, so split */
|
|
|
|
/* + PTRSIZE avoids tiny slots on free list */
|
|
|
|
NextSlot(new) = next;
|
|
|
|
NextSlot(old) = new;
|
|
|
|
free(new);
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
|
|
|
return(old);
|
|
|
|
}
|
1989-11-30 14:59:18 +00:00
|
|
|
if ((new = malloc(size)) == 0) /* it didn't fit */
|
1987-01-27 15:57:55 +00:00
|
|
|
return(0);
|
1989-11-30 14:59:18 +00:00
|
|
|
bcopy(old, new, n); /* n < size */
|
|
|
|
free(old);
|
1987-01-27 15:57:55 +00:00
|
|
|
return(new);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(p)
|
|
|
|
char *p;
|
|
|
|
{
|
1989-11-30 14:59:18 +00:00
|
|
|
register char *prev, *next;
|
|
|
|
|
|
|
|
ASSERT(NextSlot(p) > p);
|
|
|
|
for (prev = 0, next = _empty; next != 0; prev = next, next = NextFree(next))
|
|
|
|
if (p < next)
|
|
|
|
break;
|
|
|
|
NextFree(p) = next;
|
|
|
|
if (prev)
|
|
|
|
NextFree(prev) = p;
|
|
|
|
else
|
|
|
|
_empty = p;
|
|
|
|
if (next) {
|
|
|
|
ASSERT(NextSlot(p) <= next);
|
|
|
|
if (NextSlot(p) == next) { /* merge p and next */
|
|
|
|
NextSlot(p) = NextSlot(next);
|
|
|
|
NextFree(p) = NextFree(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prev) {
|
|
|
|
ASSERT(NextSlot(prev) <= p);
|
|
|
|
if (NextSlot(prev) == p) { /* merge prev and p */
|
|
|
|
NextSlot(prev) = NextSlot(p);
|
|
|
|
NextFree(prev) = NextFree(p);
|
|
|
|
}
|
|
|
|
}
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
1989-11-30 14:59:18 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
static assert_failed()
|
|
|
|
{
|
|
|
|
write(2, "assert failed in lib/malloc.c\n", 30);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|