Simple slab allocator allocator

This commit is contained in:
Ali Mashtizadeh 2014-09-04 23:26:02 -07:00
parent 1627b4b5cc
commit 03343a774c
7 changed files with 170 additions and 32 deletions

11
include/errno.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef __ERRNO_H__
#define __ERRNO_H__
#define EIO 1
#define EBADF 2
#define EINVAL 3
#define EFAULT 4
#endif /* __ERRNO_H__ */

View File

@ -44,6 +44,7 @@ src_common = [
"kern/palloc.c",
"kern/printf.c",
"kern/sga.c",
"kern/slab.c",
"kern/spinlock.c",
"kern/syscall.c",
"kern/thread.c",

View File

@ -12,5 +12,7 @@
#define UNREACHABLE __builtin_unreachable()
#define ROUNDUP(_x, _n) (((_x) + (_n) - 1) & ~((_n) - 1))
#endif /* __CDEFS_H__ */

View File

@ -2,6 +2,9 @@
#ifndef __KMEM_H__
#define __KMEM_H__
#include <sys/queue.h>
#include <sys/spinlock.h>
typedef struct Page {
uint32_t refcount; // Number of references
uint32_t pincount; // Pin count (HW, Software)
@ -29,5 +32,34 @@ uintptr_t XMem_GetBase(XMem *xmem);
uintptr_t XMem_GetLength(XMem *xmem);
bool XMem_Allocate(XMem *xmem, uintptr_t length);
/*
* Slab Allocator
*/
#define SLAB_NAMELEN 32
typedef struct SlabElement {
LIST_ENTRY(SlabElement) free;
} SlabElement;
typedef struct Slab {
uintptr_t objsz;
uintptr_t align;
XMem *xmem;
Spinlock lock;
uint64_t objs;
uint64_t freeObjs;
LIST_HEAD(SlabElementHead, SlabElement) freeList;
// Debugging
uint64_t allocs;
uint64_t frees;
char name[SLAB_NAMELEN];
LIST_ENTRY(Slab) slabList;
} Slab;
void Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align);
void *Slab_Alloc(Slab *slab);
void Slab_Free(Slab *slab, void *obj);
#endif /* __KMEM_H__ */

View File

@ -1,30 +0,0 @@
/*
* Copyright (c) 2013-2014 Stanford University
* All rights reserved.
*/
#include <stdarg.h>
#include <stdint.h>
#include <kassert.h>
Slab *
salloc_new(uintptr_t objsz)
{
}
void
salloc_destroy(Slab *)
{
}
void *
salloc_alloc(Slab *slab)
{
}
void
salloc_free(Slab *slab, void *region)
{
}

117
sys/kern/slab.c Normal file
View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2013-2014 Stanford University
* All rights reserved.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/cdefs.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/queue.h>
#include <sys/kmem.h>
#include <machine/pmap.h>
LIST_HEAD(SlabListHead, Slab) slabList = LIST_HEAD_INITIALIZER(slabList);
void
Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align)
{
ASSERT(objsz >= sizeof(SlabElement));
slab->objsz = objsz;
slab->align = align;
slab->xmem = XMem_New();
slab->objs = 0;
slab->freeObjs = 0;
slab->allocs = 0;
slab->frees = 0;
LIST_INIT(&slab->freeList);
ASSERT(slab->xmem != NULL);
strncpy(&slab->name[0], name, SLAB_NAMELEN);
Spinlock_Init(&slab->lock, name);
LIST_INSERT_HEAD(&slabList, slab, slabList);
}
int
SlabExtend(Slab *slab)
{
uintptr_t base = XMem_GetBase(slab->xmem);
uintptr_t len = XMem_GetLength(slab->xmem);
uintptr_t inc;
uintptr_t realObjSz = ROUNDUP(slab->objsz, slab->align);
inc = ROUNDUP(realObjSz * 64, PGSIZE);
if (inc < 4 * PGSIZE) {
inc = 4 * PGSIZE;
}
if (!XMem_Allocate(slab->xmem, len + inc))
return -1;
// Add empty objects to linked list
uintptr_t i;
uintptr_t objs = inc / realObjSz;
for (i = 0; i < objs; i++) {
SlabElement *elem = (SlabElement *)(base + i * realObjSz);
LIST_INSERT_HEAD(&slab->freeList, elem, free);
}
slab->objs += objs;
slab->freeObjs += objs;
return 0;
}
void *
Slab_Alloc(Slab *slab)
{
SlabElement *elem;
Spinlock_Lock(&slab->lock);
if (slab->freeObjs == 0)
SlabExtend(slab);
elem = LIST_FIRST(&slab->freeList);
LIST_REMOVE(elem, free);
slab->allocs++;
Spinlock_Unlock(&slab->lock);
return (void *)elem;
}
void
Slab_Free(Slab *slab, void *region)
{
Spinlock_Lock(&slab->lock);
SlabElement *elem = (SlabElement *)region;
LIST_INSERT_HEAD(&slab->freeList, elem, free);
slab->frees++;
Spinlock_Unlock(&slab->lock);
}
void
Debug_Slabs(int argc, const char *argv[])
{
Slab *slab;
kprintf("%-36s\n", "Slab Name");
LIST_FOREACH(slab, &slabList, slabList) {
kprintf("%-36s\n", slab->name);
}
}
REGISTER_DBGCMD(slabs, "Display list of slabs", Debug_Slabs);

View File

@ -18,11 +18,15 @@ uint64_t nextThreadID;
Thread *curProc;
TAILQ_HEAD(ThreadQueueHead, Thread) threadQueue;
Slab threadSlab;
void
Thread_Init()
{
nextThreadID = 1;
Slab_Init(&threadSlab, "Thread Objects", sizeof(Thread), 16);
// Create an thread object for current context
curProc = Thread_Create();
curProc->schedState = SCHED_STATE_RUNNING;
@ -30,6 +34,7 @@ Thread_Init()
Spinlock_Init(&threadLock, "Thread Lock");
TAILQ_INIT(&threadQueue);
}
Thread *
@ -41,7 +46,7 @@ Thread_Current()
Thread *
Thread_Create()
{
Thread *thr = PAlloc_AllocPage();
Thread *thr = (Thread *)Slab_Alloc(&threadSlab);
if (!thr)
return NULL;
@ -114,7 +119,7 @@ Thread_Destroy(Thread *thr)
// Free AS
PAlloc_FreePage((void *)thr->kstack);
PAlloc_FreePage(thr);
Slab_Free(&threadSlab, thr);
}
void