From 35dc79b899e57da1292f0b98bb35de23c6b2e083 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Tue, 18 Feb 2014 10:48:35 -0800 Subject: [PATCH] Initial page allocator --- sys/kern/palloc.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/sys/kern/palloc.c b/sys/kern/palloc.c index deec004..d30b45c 100644 --- a/sys/kern/palloc.c +++ b/sys/kern/palloc.c @@ -14,14 +14,14 @@ typedef struct FreePage { - LIST_ENTRY(FreePage) entries; + TAILQ_ENTRY(FreePage) entries; } FreePage; -LIST_HEAD(FreeListHead, FreePage) freeList; +TAILQ_HEAD(FreeListHead, FreePage) freeList; void PAlloc_Init() { - LIST_INIT(&freeList); + TAILQ_INIT(&freeList); } void PAlloc_AddRegion(uintptr_t start, uintptr_t len) @@ -39,15 +39,29 @@ void PAlloc_AddRegion(uintptr_t start, uintptr_t len) for (i = 0; i < len; i += PGSIZE) { pg = (void *)(start + i); - LIST_INSERT_HEAD(&freeList, pg, entries); + TAILQ_INSERT_TAIL(&freeList, pg, entries); } } void *PAlloc_AllocPage() { + FreePage *pg = TAILQ_FIRST(&freeList); + + TAILQ_REMOVE(&freeList, pg, entries); + + // Assert page aligned + ASSERT((((uintptr_t)pg) & PGMASK) == 0); + + return (void *)pg; } void PAlloc_FreePage(void *region) { + FreePage *pg = (FreePage *)region; + + // Assert page aligned + ASSERT((((uintptr_t)region) & PGMASK) == 0); + + TAILQ_INSERT_HEAD(&freeList, pg, entries); }