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); }