提交 41d63b70 创建 作者: Robert Morris's avatar Robert Morris

power-of-two kmalloc

should be much faster, though no more scalable
上级 a7b311ad
...@@ -10,6 +10,106 @@ ...@@ -10,6 +10,106 @@
#include "mtrace.h" #include "mtrace.h"
#include "cpu.h" #include "cpu.h"
// allocate in power-of-two sizes up to 2^KMMAX
// must be < 12
#define KMMAX 11
struct header {
struct header *next;
};
struct freelist {
struct header *buckets[KMMAX+1];
char name[MAXNAME];
struct spinlock lock;
};
struct freelist freelists[NCPU];
void
kminit(void)
{
for (int c = 0; c < NCPU; c++) {
freelists[c].name[0] = (char) c + '0';
safestrcpy(freelists[c].name+1, "freelist", MAXNAME-1);
initlock(&freelists[c].lock, freelists[c].name);
}
}
// get more space for freelists[c].buckets[b]
void
morecore(int c, int b)
{
char *p = kalloc();
if(p == 0)
return;
int sz = 1 << b;
for(char *q = p;
q + sz + sizeof(struct header) <= p + PGSIZE;
q += sz + sizeof(struct header)){
struct header *h = (struct header *) q;
h->next = freelists[c].buckets[b];
freelists[c].buckets[b] = h;
}
}
void *
kmalloc(u64 nbytes)
{
int nn = 1, b = 0;
void *r = 0;
struct header *h;
int c = mycpu()->id;
while(nn < nbytes && b <= KMMAX){
nn *= 2;
b++;
}
if(nn != (1 << b))
panic("kmalloc oops");
if(b > KMMAX)
panic("kmalloc too big");
acquire(&freelists[c].lock);
if(freelists[c].buckets[b] == 0)
morecore(c, b);
h = freelists[c].buckets[b];
if(h){
freelists[c].buckets[b] = h->next;
r = h + 1;
h->next = (void *) (long) b;
}
release(&freelists[c].lock);
if (r)
mtlabel(mtrace_label_heap, r, nbytes, "kmalloc'ed", sizeof("kmalloc'ed"));
if(r == 0)
cprintf("kmalloc(%d) failed\n", (int) nbytes);
return r;
}
void
kmfree(void *ap)
{
int c = mycpu()->id;
struct header *h;
int b;
acquire(&freelists[c].lock);
h = (struct header *) ((char *)ap - sizeof(struct header));
b = (long) h->next;
if(b < 0 || b > KMMAX)
panic("kmfree bad bucket");
h->next = freelists[c].buckets[b];
freelists[c].buckets[b] = h;
mtunlabel(mtrace_label_heap, ap);
release(&freelists[c].lock);
}
#if 0
// Memory allocator by Kernighan and Ritchie, // Memory allocator by Kernighan and Ritchie,
// The C programming Language, 2nd ed. Section 8.7. // The C programming Language, 2nd ed. Section 8.7.
...@@ -128,6 +228,8 @@ kmalloc(u64 nbytes) ...@@ -128,6 +228,8 @@ kmalloc(u64 nbytes)
return r; return r;
} }
#endif
int int
kmalign(void **p, int align, u64 size) kmalign(void **p, int align, u64 size)
{ {
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论