From 327fbc70c66400c18382dd3c188383e1eacfe338 Mon Sep 17 00:00:00 2001 From: ngiddings Date: Sat, 10 Apr 2021 19:16:31 -0500 Subject: [PATCH] Implemented page allocator in C; removed C++. --- src/pageallocator.c | 30 ++++++++++++++++++++++ src/pageallocator.hpp | 43 ------------------------------- src/pageallocatorstack.cpp | 52 -------------------------------------- src/pageallocatorstack.hpp | 34 ------------------------- 4 files changed, 30 insertions(+), 129 deletions(-) create mode 100644 src/pageallocator.c delete mode 100755 src/pageallocator.hpp delete mode 100644 src/pageallocatorstack.cpp delete mode 100644 src/pageallocatorstack.hpp diff --git a/src/pageallocator.c b/src/pageallocator.c new file mode 100644 index 0000000..631e578 --- /dev/null +++ b/src/pageallocator.c @@ -0,0 +1,30 @@ +#include "pageallocator.h" +#include "types/status.h" + +physaddr_t reserve_page(struct page_stack_t *stack) +{ + if(stack->stack_pointer < stack->base_pointer) + { + physaddr_t frame = *stack->stack_pointer; + *stack->stack_pointer = (physaddr_t) 0; + stack->stack_pointer++; + return frame; + } + return S_OUT_OF_MEMORY; +} + +int free_page(struct page_stack_t *stack, physaddr_t location) +{ + if(stack->stack_pointer > stack->limit_pointer) + { + stack->stack_pointer--; + *stack->stack_pointer = location; + return S_OK; + } + return S_OUT_OF_MEMORY; +} + +size_t free_page_count(struct page_stack_t *stack) +{ + return stack->base_pointer - stack->stack_pointer; +} \ No newline at end of file diff --git a/src/pageallocator.hpp b/src/pageallocator.hpp deleted file mode 100755 index 2e600a7..0000000 --- a/src/pageallocator.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef __MEMORYALLOCATOR_H_ -#define __MEMORYALLOCATOR_H_ - -#include -#include "systypes.hpp" - -namespace kernelns -{ - -/** - * Interface for a physical memory allocator. - */ -class PageAllocator -{ -public: - - /** - * Allocate a single page, returning its physical address. Upon failure, - * returns an error code such that the least signifigant byte is nonzero. - */ - virtual physaddr_t next() = 0; - - /** - * Frees a single page that has previously been allocated. - */ - virtual void free(physaddr_t location) = 0; - - /** - * @returns the total number of free blocks of memory. - */ - virtual size_t freeBlocks() const = 0; - - /** - * @returns the total number of blocks managed by this memory - * allocator. - */ - virtual size_t getMemorySize() const = 0; - -}; - -} - -#endif diff --git a/src/pageallocatorstack.cpp b/src/pageallocatorstack.cpp deleted file mode 100644 index 8425dcd..0000000 --- a/src/pageallocatorstack.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "pageallocatorstack.hpp" -#include "memorytype.hpp" - -using namespace kernelns; - -PageAllocatorStack::PageAllocatorStack(physaddr_t *stackBase, physaddr_t *stackTop, size_t frameSize, const MemoryMap& memoryMap) -{ - m_base = stackBase; - m_top = stackTop; - m_stack = m_base; - for(int i = 0; i < memoryMap.size(); i++) - { - if((MemoryType) memoryMap[i].getType() == MemoryType::Available) - { - for(int j = 0; j < memoryMap[i].getSize() / frameSize; j++) - { - free(memoryMap[i].getLocation() + j * frameSize); - } - } - } -} - -physaddr_t PageAllocatorStack::next() -{ - if(m_stack < m_base) - { - physaddr_t frame = *m_stack; - *m_stack = (physaddr_t) 0; - m_stack++; - return frame; - } - return (physaddr_t) -1; -} - -void PageAllocatorStack::free(physaddr_t location) -{ - if(m_stack > m_top) - { - m_stack--; - *m_stack = location; - } -} - -size_t PageAllocatorStack::freeBlocks() const -{ - return m_base - m_stack; -} - -size_t PageAllocatorStack::getMemorySize() const -{ - return m_totalSize; -} \ No newline at end of file diff --git a/src/pageallocatorstack.hpp b/src/pageallocatorstack.hpp deleted file mode 100644 index f5710ed..0000000 --- a/src/pageallocatorstack.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef PAGEALLOCATORSTACK_H -#define PAGEALLOCATORSTACK_H - -#include "pageallocator.hpp" -#include "memorymap.hpp" - -namespace kernelns -{ - -class PageAllocatorStack : public PageAllocator -{ -public: - - PageAllocatorStack(physaddr_t *stackBase, physaddr_t *stackTop, size_t frameSize, const MemoryMap& memoryMap); - - virtual physaddr_t next(); - - virtual void free(physaddr_t location); - - virtual size_t freeBlocks() const; - - virtual size_t getMemorySize() const; - -private: - - size_t m_totalSize; - - physaddr_t *m_stack, *m_base, *m_top; - -}; - -} - -#endif \ No newline at end of file