PMM -- finishing up and changing names, types

This commit is contained in:
Thomas Lovén 2017-12-21 21:03:06 +01:00
parent 28916257f1
commit 1a7852de4d
3 changed files with 28 additions and 25 deletions

View File

@ -30,4 +30,7 @@ void *memset(void *s, int c, size_t n);
void *memmove(void *dest, const void *src, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
size_t strlen(const char *s);
void pmm_free(uintptr_t page);
uintptr_t pmm_alloc();
#endif

View File

@ -1,18 +1,18 @@
#include <memory.h>
uintptr_t *first = 0;
uintptr_t first = 0;
void pmm_free(void *c)
void pmm_free(uintptr_t page)
{
c = (void *)P2V(c);
*(uintptr_t **)c = first;
first = c;
page = (uintptr_t)P2V(page);
*(uintptr_t *)page = first;
first = page;
}
void *pmm_alloc()
uintptr_t pmm_alloc()
{
void *c = first;
first = c?*(uintptr_t **)c:0;
c = (void *)V2P(c);
return c;
uintptr_t page = first;
first = page?*(uintptr_t *)page:0;
page = (uintptr_t)V2P(page);
return page;
}

View File

@ -14,44 +14,44 @@ struct {
TEST(alloc_returns_freed_page)
{
pmm_free(&mem[0]);
void *a = pmm_alloc();
pmm_free((uintptr_t)&mem[0]);
uintptr_t a = pmm_alloc();
ASSERT_EQ_PTR(a, &mem[0]);
}
TEST(alloc_returns_0_if_no_free_pages)
{
void *a = pmm_alloc();
uintptr_t a = pmm_alloc();
ASSERT_EQ_PTR(a, 0);
}
TEST(alloc_zero_after_all_free_pages)
{
pmm_free(&mem[0]);
pmm_free((uintptr_t)&mem[0]);
pmm_alloc();
void *a = pmm_alloc();
uintptr_t a = pmm_alloc();
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_free((uintptr_t)&mem[0]);
pmm_free((uintptr_t)&mem[1]);
uintptr_t 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_free((uintptr_t)&mem[0]);
pmm_free((uintptr_t)&mem[1]);
pmm_alloc();
void *a = pmm_alloc();
uintptr_t 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();
pmm_free((uintptr_t)&mem[0]);
pmm_free((uintptr_t)&mem[1]);
uintptr_t a = pmm_alloc();
uintptr_t b = pmm_alloc();
ASSERT_NEQ_PTR(a, b);
}