Initial page allocator

This commit is contained in:
Ali Mashtizadeh 2014-02-18 10:48:35 -08:00
parent 11c151b60e
commit 35dc79b899

View File

@ -14,14 +14,14 @@
typedef struct FreePage typedef struct FreePage
{ {
LIST_ENTRY(FreePage) entries; TAILQ_ENTRY(FreePage) entries;
} FreePage; } FreePage;
LIST_HEAD(FreeListHead, FreePage) freeList; TAILQ_HEAD(FreeListHead, FreePage) freeList;
void PAlloc_Init() void PAlloc_Init()
{ {
LIST_INIT(&freeList); TAILQ_INIT(&freeList);
} }
void PAlloc_AddRegion(uintptr_t start, uintptr_t len) 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) for (i = 0; i < len; i += PGSIZE)
{ {
pg = (void *)(start + i); pg = (void *)(start + i);
LIST_INSERT_HEAD(&freeList, pg, entries); TAILQ_INSERT_TAIL(&freeList, pg, entries);
} }
} }
void *PAlloc_AllocPage() 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) void PAlloc_FreePage(void *region)
{ {
FreePage *pg = (FreePage *)region;
// Assert page aligned
ASSERT((((uintptr_t)region) & PGMASK) == 0);
TAILQ_INSERT_HEAD(&freeList, pg, entries);
} }