2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
|
|
|
#include "user/user.h"
|
|
|
|
#include "kernel/param.h"
|
2006-08-24 02:44:41 +00:00
|
|
|
|
2007-08-27 23:26:33 +00:00
|
|
|
// Memory allocator by Kernighan and Ritchie,
|
|
|
|
// The C programming Language, 2nd ed. Section 8.7.
|
2006-08-24 02:44:41 +00:00
|
|
|
|
|
|
|
typedef long Align;
|
|
|
|
|
|
|
|
union header {
|
|
|
|
struct {
|
|
|
|
union header *ptr;
|
|
|
|
uint size;
|
|
|
|
} s;
|
|
|
|
Align x;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef union header Header;
|
|
|
|
|
|
|
|
static Header base;
|
2007-08-10 17:17:42 +00:00
|
|
|
static Header *freep;
|
2006-08-24 02:44:41 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
free(void *ap)
|
|
|
|
{
|
|
|
|
Header *bp, *p;
|
2006-08-24 19:24:36 +00:00
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
bp = (Header*)ap - 1;
|
2006-09-06 17:27:19 +00:00
|
|
|
for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
|
|
|
|
if(p >= p->s.ptr && (bp > p || bp < p->s.ptr))
|
2006-08-24 02:44:41 +00:00
|
|
|
break;
|
2007-08-28 18:32:08 +00:00
|
|
|
if(bp + bp->s.size == p->s.ptr){
|
2006-08-24 02:44:41 +00:00
|
|
|
bp->s.size += p->s.ptr->s.size;
|
|
|
|
bp->s.ptr = p->s.ptr->s.ptr;
|
2007-08-28 18:37:41 +00:00
|
|
|
} else
|
2006-08-24 02:44:41 +00:00
|
|
|
bp->s.ptr = p->s.ptr;
|
2007-08-28 18:32:08 +00:00
|
|
|
if(p + p->s.size == bp){
|
2006-08-24 02:44:41 +00:00
|
|
|
p->s.size += bp->s.size;
|
|
|
|
p->s.ptr = bp->s.ptr;
|
2007-08-28 18:37:41 +00:00
|
|
|
} else
|
2006-08-24 02:44:41 +00:00
|
|
|
p->s.ptr = bp;
|
|
|
|
freep = p;
|
|
|
|
}
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
static Header*
|
2006-08-24 02:44:41 +00:00
|
|
|
morecore(uint nu)
|
|
|
|
{
|
2007-08-09 17:32:40 +00:00
|
|
|
char *p;
|
|
|
|
Header *hp;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2010-08-31 16:54:47 +00:00
|
|
|
if(nu < 4096)
|
|
|
|
nu = 4096;
|
2007-08-09 17:32:40 +00:00
|
|
|
p = sbrk(nu * sizeof(Header));
|
2011-01-11 18:01:13 +00:00
|
|
|
if(p == (char*)-1)
|
2006-08-24 02:44:41 +00:00
|
|
|
return 0;
|
2007-08-09 17:32:40 +00:00
|
|
|
hp = (Header*)p;
|
|
|
|
hp->s.size = nu;
|
|
|
|
free((void*)(hp + 1));
|
2006-08-24 02:44:41 +00:00
|
|
|
return freep;
|
|
|
|
}
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
void*
|
2006-08-24 02:44:41 +00:00
|
|
|
malloc(uint nbytes)
|
|
|
|
{
|
|
|
|
Header *p, *prevp;
|
|
|
|
uint nunits;
|
|
|
|
|
|
|
|
nunits = (nbytes + sizeof(Header) - 1)/sizeof(Header) + 1;
|
2007-08-28 18:32:08 +00:00
|
|
|
if((prevp = freep) == 0){
|
2006-08-24 02:44:41 +00:00
|
|
|
base.s.ptr = freep = prevp = &base;
|
|
|
|
base.s.size = 0;
|
|
|
|
}
|
2007-08-28 18:32:08 +00:00
|
|
|
for(p = prevp->s.ptr; ; prevp = p, p = p->s.ptr){
|
|
|
|
if(p->s.size >= nunits){
|
2006-09-06 17:27:19 +00:00
|
|
|
if(p->s.size == nunits)
|
2006-09-06 17:04:06 +00:00
|
|
|
prevp->s.ptr = p->s.ptr;
|
2007-08-28 18:37:41 +00:00
|
|
|
else {
|
2006-09-06 17:04:06 +00:00
|
|
|
p->s.size -= nunits;
|
|
|
|
p += p->s.size;
|
|
|
|
p->s.size = nunits;
|
2006-08-24 02:44:41 +00:00
|
|
|
}
|
|
|
|
freep = prevp;
|
2011-01-11 18:01:13 +00:00
|
|
|
return (void*)(p + 1);
|
2006-08-24 02:44:41 +00:00
|
|
|
}
|
2006-09-06 17:27:19 +00:00
|
|
|
if(p == freep)
|
|
|
|
if((p = morecore(nunits)) == 0)
|
2006-09-06 17:04:06 +00:00
|
|
|
return 0;
|
2006-08-24 02:44:41 +00:00
|
|
|
}
|
|
|
|
}
|