diff --git a/include/pageallocator.h b/include/pageallocator.h index bccca63..5b52d54 100644 --- a/include/pageallocator.h +++ b/include/pageallocator.h @@ -1,5 +1,6 @@ #pragma once +#include "memorymap.h" #include "types/physaddr.h" #include @@ -64,3 +65,11 @@ int free_page(struct page_stack_t *stack, physaddr_t location); * @return size_t */ size_t free_page_count(struct page_stack_t *stack); + +/** + * @brief Push all available pages in `map` onto the stack + * + * @param stack + * @param map + */ +int initialize_page_stack(struct page_stack_t *stack, struct memory_map_t *map, size_t page_size); diff --git a/src/pageallocator.c b/src/pageallocator.c index 631e578..488fa33 100644 --- a/src/pageallocator.c +++ b/src/pageallocator.c @@ -1,4 +1,5 @@ #include "pageallocator.h" +#include "types/memorytype.h" #include "types/status.h" physaddr_t reserve_page(struct page_stack_t *stack) @@ -27,4 +28,26 @@ int free_page(struct page_stack_t *stack, physaddr_t location) size_t free_page_count(struct page_stack_t *stack) { return stack->base_pointer - stack->stack_pointer; -} \ No newline at end of file +} + +int initialize_page_stack(struct page_stack_t *stack, struct memory_map_t *map, size_t page_size) +{ + stack->total_pages = 0; + for(int i = 0; i < map->size; i++) + { + if(map->array[i].type != M_AVAILABLE) + { + continue; + } + size_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1); + while(location + page_size <= map->array[i].location + map->array[i].size) + { + if(free_page(stack, location) != S_OK) + { + return S_OUT_OF_MEMORY; + } + stack->total_pages++; + location += page_size; + } + } +}