From 09edf0cedc644dc69fbcd035c28ee6d6a21aa129 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Sat, 1 Aug 2020 18:10:43 -0500 Subject: [PATCH] Moved PageAllocator implementation to pageallocator.cpp --- src/Makefile.am | 2 +- src/buddyallocator.hpp | 94 ------------------- src/mmap.hpp | 4 +- src/mmgr.hpp | 2 +- src/{buddyallocator.cpp => pageallocator.cpp} | 2 +- src/pageallocator.hpp | 85 ++++++++++++++++- src/x86/mmap.cpp | 4 +- 7 files changed, 91 insertions(+), 102 deletions(-) delete mode 100755 src/buddyallocator.hpp rename src/{buddyallocator.cpp => pageallocator.cpp} (99%) diff --git a/src/Makefile.am b/src/Makefile.am index 4062180..a5b0db4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ 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_CPPFLAGS = -ffreestanding -mgeneral-regs-only -O0 -Wall -fno-exceptions -fno-rtti -ggdb quark_kernel_LDFLAGS = -nostdlib diff --git a/src/buddyallocator.hpp b/src/buddyallocator.hpp deleted file mode 100755 index d30351f..0000000 --- a/src/buddyallocator.hpp +++ /dev/null @@ -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 diff --git a/src/mmap.hpp b/src/mmap.hpp index 8f354d3..3169629 100644 --- a/src/mmap.hpp +++ b/src/mmap.hpp @@ -10,9 +10,9 @@ 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); diff --git a/src/mmgr.hpp b/src/mmgr.hpp index b34b018..e39cb6e 100644 --- a/src/mmgr.hpp +++ b/src/mmgr.hpp @@ -2,6 +2,6 @@ #define MMGR_H #include "mmap.hpp" -#include "buddyallocator.hpp" +#include "pageallocator.hpp" #endif \ No newline at end of file diff --git a/src/buddyallocator.cpp b/src/pageallocator.cpp similarity index 99% rename from src/buddyallocator.cpp rename to src/pageallocator.cpp index 93a33c9..761a52d 100755 --- a/src/buddyallocator.cpp +++ b/src/pageallocator.cpp @@ -1,4 +1,4 @@ -#include "buddyallocator.hpp" +#include "pageallocator.hpp" #include "systypes.hpp" #include "memorymap.hpp" diff --git a/src/pageallocator.hpp b/src/pageallocator.hpp index 48ea87c..d3034ec 100755 --- a/src/pageallocator.hpp +++ b/src/pageallocator.hpp @@ -3,6 +3,7 @@ #include #include "systypes.hpp" +#include "memorymap.hpp" namespace kernel { @@ -10,7 +11,7 @@ namespace kernel /** * Interface for a dymanic memory allocator. */ -class MemoryAllocator +class PageAllocator { public: @@ -49,6 +50,88 @@ public: virtual size_t getMemorySize() const = 0; }; + +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; + +}; } diff --git a/src/x86/mmap.cpp b/src/x86/mmap.cpp index 9bb61a1..4638203 100644 --- a/src/x86/mmap.cpp +++ b/src/x86/mmap.cpp @@ -1,7 +1,7 @@ #include "../mmap.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) return -1; @@ -36,7 +36,7 @@ int kernel::mmap(MemoryAllocator& allocator, void* start, size_t length, int fla 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) return -1;