PMM - alloc two pages

This commit is contained in:
Thomas Lovén 2017-12-21 16:15:55 +01:00
parent e7bdea771b
commit 1daf8ed47b
2 changed files with 28 additions and 2 deletions

View File

@ -4,12 +4,13 @@ uintptr_t *first = 0;
void pmm_free(void *c) void pmm_free(void *c)
{ {
*(uintptr_t *)c = (uintptr_t)first;
first = c; first = c;
} }
void *pmm_alloc() void *pmm_alloc()
{ {
void *c = first; void *c = first;
first = 0; first = (uintptr_t *)(c?*(uintptr_t *)c:0);
return c; return c;
} }

View File

@ -5,7 +5,7 @@
struct { struct {
uint8_t data[PAGE_SIZE]; uint8_t data[PAGE_SIZE];
}__attribute__((packed)) mem[4]; }__attribute__((packed)) mem[2];
TEST(alloc_returns_freed_page) TEST(alloc_returns_freed_page)
{ {
@ -25,3 +25,28 @@ TEST(alloc_zero_after_all_free_pages)
void *a = pmm_alloc(); void *a = pmm_alloc();
ASSERT_EQ_PTR(a, 0); ASSERT_EQ_PTR(a, 0);
} }
TEST(alloc_two_pages___first_page_is_not_zero)
{
pmm_free(&mem[0]);
pmm_free(&mem[1]);
void *a = pmm_alloc();
pmm_alloc();
ASSERT_NEQ_PTR(a, 0);
}
TEST(alloc_two_pages___second_page_is_not_zero)
{
pmm_free(&mem[0]);
pmm_free(&mem[1]);
pmm_alloc();
void *a = pmm_alloc();
ASSERT_NEQ_PTR(a, 0);
}
TEST(alloc_two_pages___doesnt_return_same_page_twice)
{
pmm_free(&mem[0]);
pmm_free(&mem[1]);
void *a = pmm_alloc();
void *b = pmm_alloc();
ASSERT_NEQ_PTR(a, b);
}