Implemented page allocator in C; removed C++.

This commit is contained in:
2021-04-10 19:16:31 -05:00
parent 7c3a40bc47
commit 327fbc70c6
4 changed files with 30 additions and 129 deletions

30
src/pageallocator.c Normal file
View File

@@ -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;
}

View File

@@ -1,43 +0,0 @@
#ifndef __MEMORYALLOCATOR_H_
#define __MEMORYALLOCATOR_H_
#include <stddef.h>
#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

View File

@@ -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;
}

View File

@@ -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