提交 19f389d9 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

kalloc.c -> kalloc.cc

上级 63976a43
#include "types.h" #include "types.h"
#include "mmu.h" #include "mmu.h"
#include "spinlock.h"
#include "kalloc.h"
/*
* Data structures that use C99 designated initializers,
* which aren't avialable in C++11.
*/
struct segdesc __attribute__((aligned(16))) bootgdt[NSEGS] = { struct segdesc __attribute__((aligned(16))) bootgdt[NSEGS] = {
// null // null
...@@ -18,3 +25,21 @@ struct segdesc __attribute__((aligned(16))) bootgdt[NSEGS] = { ...@@ -18,3 +25,21 @@ struct segdesc __attribute__((aligned(16))) bootgdt[NSEGS] = {
[7]=SEGDESC(0, 0, SEG_R|SEG_CODE|SEG_S|SEG_DPL(3)|SEG_P|SEG_L|SEG_G), [7]=SEGDESC(0, 0, SEG_R|SEG_CODE|SEG_S|SEG_DPL(3)|SEG_P|SEG_L|SEG_G),
}; };
struct kmem slabmem[slab_type_max][NCPU] = {
[slab_stack][0 ... NCPU-1] = {
.name = " kstack",
.size = KSTACKSIZE,
.ninit = CPUKSTACKS,
},
[slab_perf][0 ... NCPU-1] = {
.name = " kperf",
.size = PERFSIZE,
.ninit = 1,
},
[slab_kshared][0 ... NCPU-1] = {
.name = " kshared",
.size = KSHAREDSIZE,
.ninit = CPUKSTACKS,
},
};
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// Slab allocator, for chunks larger than one page. // Slab allocator, for chunks larger than one page.
// //
extern "C" {
#include "types.h" #include "types.h"
#include "mmu.h" #include "mmu.h"
#include "kernel.h" #include "kernel.h"
...@@ -11,29 +12,12 @@ ...@@ -11,29 +12,12 @@
#include "mtrace.h" #include "mtrace.h"
#include "cpu.h" #include "cpu.h"
#include "multiboot.h" #include "multiboot.h"
}
static struct Mbmem mem[128]; static struct Mbmem mem[128];
static u64 nmem; static u64 nmem;
static u64 membytes; static u64 membytes;
struct kmem kmems[NCPU]; struct kmem kmems[NCPU];
static struct kmem slabmem[][NCPU] = {
[slab_stack][0 ... NCPU-1] = {
.name = " kstack",
.size = KSTACKSIZE,
.ninit = CPUKSTACKS,
},
[slab_perf][0 ... NCPU-1] = {
.name = " kperf",
.size = PERFSIZE,
.ninit = 1,
},
[slab_kshared][0 ... NCPU-1] = {
.name = " kshared",
.size = KSHAREDSIZE,
.ninit = CPUKSTACKS,
},
};
extern char end[]; // first address after kernel loaded from ELF file extern char end[]; // first address after kernel loaded from ELF file
char *newend; char *newend;
...@@ -50,7 +34,7 @@ memsearch(paddr pa) ...@@ -50,7 +34,7 @@ memsearch(paddr pa)
for (e = &mem[0]; e < q; e++) for (e = &mem[0]; e < q; e++)
if ((e->base <= pa) && ((e->base+e->length) > pa)) if ((e->base <= pa) && ((e->base+e->length) > pa))
return e; return e;
return NULL; return 0;
} }
static u64 static u64
...@@ -93,11 +77,11 @@ initmem(u64 mbaddr) ...@@ -93,11 +77,11 @@ initmem(u64 mbaddr)
struct Mbmem *mbmem; struct Mbmem *mbmem;
u8 *p, *ep; u8 *p, *ep;
mb = p2v(mbaddr); mb = (Mbdata*) p2v(mbaddr);
if(!(mb->flags & (1<<6))) if(!(mb->flags & (1<<6)))
panic("multiboot header has no memory map"); panic("multiboot header has no memory map");
p = p2v(mb->mmap_addr); p = (u8*) p2v(mb->mmap_addr);
ep = p + mb->mmap_length; ep = p + mb->mmap_length;
while (p < ep) { while (p < ep) {
...@@ -121,7 +105,7 @@ pgalloc(void) ...@@ -121,7 +105,7 @@ pgalloc(void)
void *p = (void*)PGROUNDUP((uptr)newend); void *p = (void*)PGROUNDUP((uptr)newend);
memset(p, 0, PGSIZE); memset(p, 0, PGSIZE);
newend = newend + PGSIZE; newend = newend + PGSIZE;
return p; return (char*) p;
} }
// Free the page of physical memory pointed at by v, // Free the page of physical memory pointed at by v,
...@@ -210,7 +194,7 @@ kalloc(void) ...@@ -210,7 +194,7 @@ kalloc(void)
} }
void * void *
ksalloc(slab_t slab) ksalloc(int slab)
{ {
return kalloc_pool(slabmem[slab]); return kalloc_pool(slabmem[slab]);
} }
...@@ -225,7 +209,7 @@ slabinit(struct kmem *k, char **p, u64 *off) ...@@ -225,7 +209,7 @@ slabinit(struct kmem *k, char **p, u64 *off)
if (memsize(p) < k->size) if (memsize(p) < k->size)
panic("slabinit: memsize"); panic("slabinit: memsize");
kfree_pool(k, *p); kfree_pool(k, *p);
*p = memnext(*p, k->size); *p = (char*) memnext(*p, k->size);
*off = *off+k->size; *off = *off+k->size;
} }
} }
...@@ -247,7 +231,7 @@ initkalloc(u64 mbaddr) ...@@ -247,7 +231,7 @@ initkalloc(u64 mbaddr)
kmems[c].size = PGSIZE; kmems[c].size = PGSIZE;
} }
for (int i = 0; i < NELEM(slabmem); i++) { for (int i = 0; i < slab_type_max; i++) {
for (int c = 0; c < NCPU; c++) { for (int c = 0; c < NCPU; c++) {
slabmem[i][c].name[0] = (char) c + '0'; slabmem[i][c].name[0] = (char) c + '0';
initlock(&slabmem[i][c].lock, initlock(&slabmem[i][c].lock,
...@@ -269,7 +253,7 @@ initkalloc(u64 mbaddr) ...@@ -269,7 +253,7 @@ initkalloc(u64 mbaddr)
slabinit(&slabmem[i][c], &p, &k); slabinit(&slabmem[i][c], &p, &k);
// The rest goes to the page allocator // The rest goes to the page allocator
for (; k != n; k += PGSIZE, p = memnext(p, PGSIZE)) { for (; k != n; k += PGSIZE, p = (char*) memnext(p, PGSIZE)) {
if (p == (void *)-1) if (p == (void *)-1)
panic("initkalloc: e820next"); panic("initkalloc: e820next");
kfree_pool(&kmems[c], p); kfree_pool(&kmems[c], p);
...@@ -285,14 +269,14 @@ initkalloc(u64 mbaddr) ...@@ -285,14 +269,14 @@ initkalloc(u64 mbaddr)
void void
kfree(void *v) kfree(void *v)
{ {
verifyfree(v, mykmem()->size); verifyfree((char*) v, mykmem()->size);
kfree_pool(mykmem(), v); kfree_pool(mykmem(), (char*) v);
} }
void void
ksfree(slab_t slab, void *v) ksfree(int slab, void *v)
{ {
kfree_pool(slabmem[slab], v); kfree_pool(slabmem[slab], (char*) v);
} }
void void
......
...@@ -11,4 +11,13 @@ struct kmem { ...@@ -11,4 +11,13 @@ struct kmem {
u64 nfree; u64 nfree;
} __mpalign__; } __mpalign__;
enum {
slab_stack,
slab_perf,
slab_kshared,
slab_type_max
};
extern struct kmem kmems[NCPU]; extern struct kmem kmems[NCPU];
extern struct kmem slabmem[slab_type_max][NCPU];
...@@ -125,15 +125,10 @@ void iderw(struct buf*); ...@@ -125,15 +125,10 @@ void iderw(struct buf*);
void ioapicenable(int irq, int cpu); void ioapicenable(int irq, int cpu);
// kalloc.c // kalloc.c
typedef enum {
slab_stack,
slab_perf,
slab_kshared,
} slab_t;
char* kalloc(void); char* kalloc(void);
void kfree(void*); void kfree(void*);
void* ksalloc(slab_t); void* ksalloc(int slabtype);
void ksfree(slab_t, void*); void ksfree(int slabtype, void*);
void* kmalloc(u64); void* kmalloc(u64);
void kmfree(void*); void kmfree(void*);
int kmalign(void **p, int align, u64 size); int kmalign(void **p, int align, u64 size);
......
...@@ -2,6 +2,8 @@ extern "C" { ...@@ -2,6 +2,8 @@ extern "C" {
#include "types.h" #include "types.h"
#include "multiboot.h" #include "multiboot.h"
#include "kernel.h" #include "kernel.h"
#include "spinlock.h"
#include "kalloc.h"
#include "cpu.h" #include "cpu.h"
#include "amd64.h" #include "amd64.h"
} }
......
...@@ -11,6 +11,7 @@ extern "C" { ...@@ -11,6 +11,7 @@ extern "C" {
#include "bits.h" #include "bits.h"
#include "kmtrace.h" #include "kmtrace.h"
#include "sched.h" #include "sched.h"
#include "kalloc.h"
} }
#include "vm.hh" #include "vm.hh"
......
...@@ -5,6 +5,7 @@ extern "C" { ...@@ -5,6 +5,7 @@ extern "C" {
#include "fs.h" #include "fs.h"
#include "kernel.h" #include "kernel.h"
#include "stat.h" #include "stat.h"
#include "kalloc.h"
} }
#include "file.hh" #include "file.hh"
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论