Moved PageAllocator implementation to pageallocator.cpp

This commit is contained in:
2020-08-01 18:10:43 -05:00
parent 80ec7afa13
commit 09edf0cedc
7 changed files with 91 additions and 102 deletions

View File

@@ -1,5 +1,5 @@
noinst_PROGRAMS = quark-kernel noinst_PROGRAMS = quark-kernel
quark_kernel_SOURCES = quarkkernel.cpp elf.cpp tty.cpp systeminfo.cpp util.cpp memorymap.cpp buddyallocator.cpp quark_kernel_SOURCES = quarkkernel.cpp elf.cpp tty.cpp systeminfo.cpp util.cpp memorymap.cpp pageallocator.cpp
quark_kernel_LDADD = -lgcc quark_kernel_LDADD = -lgcc
quark_kernel_CPPFLAGS = -ffreestanding -mgeneral-regs-only -O0 -Wall -fno-exceptions -fno-rtti -ggdb quark_kernel_CPPFLAGS = -ffreestanding -mgeneral-regs-only -O0 -Wall -fno-exceptions -fno-rtti -ggdb
quark_kernel_LDFLAGS = -nostdlib quark_kernel_LDFLAGS = -nostdlib

View File

@@ -1,94 +0,0 @@
#ifndef BUDDYALLOCATOR_H_
#define BUDDYALLOCATOR_H_
#include "pageallocator.hpp"
#include "memorymap.hpp"
namespace kernel
{
class BuddyAllocator : public MemoryAllocator
{
public:
BuddyAllocator();
BuddyAllocator(MemoryMap& memmap, char* bitmap, size_t blockCount,
size_t treeHeight);
BuddyAllocator(char* bitmap, size_t blockSize, size_t blockCount,
size_t treeHeight);
/**
* Allocate a block of memory containing at least 'size' bytes.
* Rounds up to the nearest power of 2 times the size of a block.
*/
virtual physaddr_t allocate(size_t size);
/**
* Free the region of memory starting at 'location' and containing
* 'size' bytes.
*/
virtual void free(physaddr_t location, size_t size);
/**
* @returns the total number of free blocks of memory.
*/
virtual size_t freeBlocks() const;
/**
* @returns the size in blocks of the largest possible allocation that
* will not fail due to lack of memory.
*/
virtual size_t maxAllocationSize() const;
/**
* @returns the size in bytes of a single block.
*/
virtual size_t getBlockSize() const;
/**
* @returns the total number of blocks managed by this memory
* allocator.
*/
virtual size_t getMemorySize() const;
private:
static const size_t INVALID = (size_t) -1;
char* bitmap;
size_t blockSize;
size_t blockCount;
size_t treeHeight;
size_t findFreeBlock(size_t height);
size_t split(size_t height, size_t index);
size_t merge(size_t height, size_t index);
size_t getBuddy(size_t index);
size_t getParent(size_t index);
size_t getChild(size_t index);
physaddr_t nodeToAddress(size_t height, size_t index) const;
size_t addressToNode(size_t height, physaddr_t location) const;
void reserveNode(size_t height, size_t index);
void freeNode(size_t height, size_t index);
bool isFree(size_t height, size_t index) const;
};
}
#endif

View File

@@ -10,9 +10,9 @@
namespace kernel namespace kernel
{ {
int mmap(MemoryAllocator& allocator, void* start, size_t length, int flags); int mmap(PageAllocator& allocator, void* start, size_t length, int flags);
int munmap(MemoryAllocator& allocator, void* start, size_t length); int munmap(PageAllocator& allocator, void* start, size_t length);
bool isMapped(void* addr); bool isMapped(void* addr);

View File

@@ -2,6 +2,6 @@
#define MMGR_H #define MMGR_H
#include "mmap.hpp" #include "mmap.hpp"
#include "buddyallocator.hpp" #include "pageallocator.hpp"
#endif #endif

View File

@@ -1,4 +1,4 @@
#include "buddyallocator.hpp" #include "pageallocator.hpp"
#include "systypes.hpp" #include "systypes.hpp"
#include "memorymap.hpp" #include "memorymap.hpp"

View File

@@ -3,6 +3,7 @@
#include <stddef.h> #include <stddef.h>
#include "systypes.hpp" #include "systypes.hpp"
#include "memorymap.hpp"
namespace kernel namespace kernel
{ {
@@ -10,7 +11,7 @@ namespace kernel
/** /**
* Interface for a dymanic memory allocator. * Interface for a dymanic memory allocator.
*/ */
class MemoryAllocator class PageAllocator
{ {
public: public:
@@ -50,6 +51,88 @@ public:
}; };
class BuddyAllocator : public PageAllocator
{
public:
BuddyAllocator();
BuddyAllocator(MemoryMap& memmap, char* bitmap, size_t blockCount,
size_t treeHeight);
BuddyAllocator(char* bitmap, size_t blockSize, size_t blockCount,
size_t treeHeight);
/**
* Allocate a block of memory containing at least 'size' bytes.
* Rounds up to the nearest power of 2 times the size of a block.
*/
virtual physaddr_t allocate(size_t size);
/**
* Free the region of memory starting at 'location' and containing
* 'size' bytes.
*/
virtual void free(physaddr_t location, size_t size);
/**
* @returns the total number of free blocks of memory.
*/
virtual size_t freeBlocks() const;
/**
* @returns the size in blocks of the largest possible allocation that
* will not fail due to lack of memory.
*/
virtual size_t maxAllocationSize() const;
/**
* @returns the size in bytes of a single block.
*/
virtual size_t getBlockSize() const;
/**
* @returns the total number of blocks managed by this memory
* allocator.
*/
virtual size_t getMemorySize() const;
private:
static const size_t INVALID = (size_t) -1;
char* bitmap;
size_t blockSize;
size_t blockCount;
size_t treeHeight;
size_t findFreeBlock(size_t height);
size_t split(size_t height, size_t index);
size_t merge(size_t height, size_t index);
size_t getBuddy(size_t index);
size_t getParent(size_t index);
size_t getChild(size_t index);
physaddr_t nodeToAddress(size_t height, size_t index) const;
size_t addressToNode(size_t height, physaddr_t location) const;
void reserveNode(size_t height, size_t index);
void freeNode(size_t height, size_t index);
bool isFree(size_t height, size_t index) const;
};
} }
#endif #endif

View File

@@ -1,7 +1,7 @@
#include "../mmap.hpp" #include "../mmap.hpp"
#include "pagetableentry.hpp" #include "pagetableentry.hpp"
int kernel::mmap(MemoryAllocator& allocator, void* start, size_t length, int flags) int kernel::mmap(PageAllocator& allocator, void* start, size_t length, int flags)
{ {
if((size_t) start % 4096 != 0) if((size_t) start % 4096 != 0)
return -1; return -1;
@@ -36,7 +36,7 @@ int kernel::mmap(MemoryAllocator& allocator, void* start, size_t length, int fla
return 0; return 0;
} }
int kernel::munmap(MemoryAllocator& allocator, void* start, size_t length) int kernel::munmap(PageAllocator& allocator, void* start, size_t length)
{ {
if((size_t) start % 4096 != 0) if((size_t) start % 4096 != 0)
return -1; return -1;