aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
Diffstat (limited to 'arch')
-rw-r--r--arch/x86_64/include/arch/memory/heap/bump_allocator.hpp4
-rw-r--r--arch/x86_64/include/arch/memory/heap/concept.hpp22
-rw-r--r--arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp8
-rw-r--r--arch/x86_64/include/arch/memory/heap/heap_allocator.hpp5
-rw-r--r--arch/x86_64/src/kernel/main.cpp1
-rw-r--r--arch/x86_64/src/memory/cpu/control_register.cpp8
-rw-r--r--arch/x86_64/src/memory/heap/global_heap_allocator.cpp75
-rw-r--r--arch/x86_64/src/memory/main.cpp2
8 files changed, 46 insertions, 79 deletions
diff --git a/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp b/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp
index 209f8b3..d5f4561 100644
--- a/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp
@@ -34,7 +34,7 @@ namespace teachos::arch::memory::heap
* @param size Amount of memory to allocate.
* @return Address of the first byte to the allocated area
*/
- auto allocate(std::size_t size) -> void *;
+ auto allocate(std::size_t size) -> void * override;
/**
* @brief Deallocates heap memory at the specified location.
@@ -44,7 +44,7 @@ namespace teachos::arch::memory::heap
* @param pointer Pointer to the location which should be deallocated.
* @param size Size of the underlying memory area we want to deallocate.
*/
- auto deallocate(void * pointer, std::size_t size) -> void;
+ auto deallocate(void * pointer, std::size_t size) -> void override;
private:
std::size_t heap_start; ///< Start of the allocatable heap area
diff --git a/arch/x86_64/include/arch/memory/heap/concept.hpp b/arch/x86_64/include/arch/memory/heap/concept.hpp
deleted file mode 100644
index e22e35f..0000000
--- a/arch/x86_64/include/arch/memory/heap/concept.hpp
+++ /dev/null
@@ -1,22 +0,0 @@
-#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP
-#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP
-
-#include <concepts>
-
-namespace teachos::arch::memory::heap
-{
- std::size_t constexpr HEAP_START = 0x100000000;
- std::size_t constexpr HEAP_SIZE = 100 * 1024;
-
- /**
- * @brief Heap allocator concept required for allocating and managing free space on the heap.
- */
- template<typename T>
- concept HeapAllocator = requires(T t, uint8_t * pointer, std::size_t size) {
- { t.allocate(size) } -> std::same_as<uint8_t *>;
- { t.deallocate(pointer, size) } -> std::same_as<void>;
- };
-
-} // namespace teachos::arch::memory::heap
-
-#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP
diff --git a/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp
index e8555df..1b1d964 100644
--- a/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp
@@ -1,7 +1,7 @@
#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP
#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP
-#include "arch/memory/heap/linked_list_allocator.hpp"
+#include "arch/memory/heap/heap_allocator.hpp"
namespace teachos::arch::memory::heap
{
@@ -14,16 +14,16 @@ namespace teachos::arch::memory::heap
struct global_heap_allocator
{
- static auto register_heap_allocator_type(heap_allocator_type new_type) -> void;
+ static auto register_heap_allocator(heap_allocator_type new_type) -> void;
static auto allocate(std::size_t size) -> void *;
static auto deallocate(void * pointer, std::size_t size) -> void;
private:
- static heap_allocator_type allocator_type;
+ static heap_allocator * allocator_instance;
- static auto create_or_get() -> heap_allocator &;
+ static auto get() -> heap_allocator &;
};
} // namespace teachos::arch::memory::heap
diff --git a/arch/x86_64/include/arch/memory/heap/heap_allocator.hpp b/arch/x86_64/include/arch/memory/heap/heap_allocator.hpp
index 9e37bd4..62ba963 100644
--- a/arch/x86_64/include/arch/memory/heap/heap_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/heap/heap_allocator.hpp
@@ -5,8 +5,13 @@
namespace teachos::arch::memory::heap
{
+ std::size_t constexpr HEAP_START = 0x100000000;
+ std::size_t constexpr HEAP_SIZE = 100 * 1024;
+
struct heap_allocator
{
+ virtual ~heap_allocator() {}
+
virtual auto allocate(std::size_t size) -> void * = 0;
virtual auto deallocate(void * pointer, std::size_t size) -> void = 0;
diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp
index 7c03644..f102a3a 100644
--- a/arch/x86_64/src/kernel/main.cpp
+++ b/arch/x86_64/src/kernel/main.cpp
@@ -1,7 +1,6 @@
#include "arch/kernel/main.hpp"
#include "arch/memory/heap/bump_allocator.hpp"
-#include "arch/memory/heap/concept.hpp"
#include "arch/memory/heap/global_heap_allocator.hpp"
#include "arch/memory/main.hpp"
#include "arch/memory/multiboot/reader.hpp"
diff --git a/arch/x86_64/src/memory/cpu/control_register.cpp b/arch/x86_64/src/memory/cpu/control_register.cpp
index 298874f..7ee88b5 100644
--- a/arch/x86_64/src/memory/cpu/control_register.cpp
+++ b/arch/x86_64/src/memory/cpu/control_register.cpp
@@ -1,6 +1,6 @@
#include "arch/memory/cpu/control_register.hpp"
-#include "arch/exception_handling/assert.hpp"
+#include "arch/exception_handling/panic.hpp"
#include <type_traits>
@@ -24,8 +24,7 @@ namespace teachos::arch::memory::cpu
asm volatile("mov %%cr4, %[output]" : [output] "=r"(current_value));
break;
default:
- exception_handling::assert(false,
- "[Control Register] Attempted to read non-existent or reserved control register");
+ exception_handling::panic("[Control Register] Attempted to read non-existent or reserved control register");
break;
}
return current_value;
@@ -60,8 +59,7 @@ namespace teachos::arch::memory::cpu
: "memory");
break;
default:
- exception_handling::assert(false,
- "[Control Register] Attempted to write non-existent or reserved control register");
+ exception_handling::panic("[Control Register] Attempted to write non-existent or reserved control register");
break;
}
}
diff --git a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp
index 243f5d8..71fe775 100644
--- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp
+++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp
@@ -1,56 +1,47 @@
#include "arch/memory/heap/global_heap_allocator.hpp"
#include "arch/exception_handling/assert.hpp"
-#include "arch/exception_handling/panic.hpp"
#include "arch/memory/heap/bump_allocator.hpp"
-#include "arch/memory/heap/concept.hpp"
-#include "arch/memory/heap/heap_allocator.hpp"
#include "arch/memory/heap/linked_list_allocator.hpp"
namespace teachos::arch::memory::heap
{
- heap_allocator_type global_heap_allocator::allocator_type = heap_allocator_type::NONE;
+ heap_allocator * global_heap_allocator::allocator_instance = nullptr;
- auto global_heap_allocator::allocate(std::size_t size) -> void * { return create_or_get().allocate(size); }
+ auto global_heap_allocator::allocate(std::size_t size) -> void * { return get().allocate(size); }
- auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void
- {
- create_or_get().deallocate(pointer, size);
- }
+ auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void { get().deallocate(pointer, size); }
- auto global_heap_allocator::register_heap_allocator_type(heap_allocator_type new_type) -> void
+ auto global_heap_allocator::register_heap_allocator(heap_allocator_type new_type) -> void
{
- allocator_type = new_type;
- }
+ exception_handling::assert(allocator_instance != nullptr,
+ "Calling register_heap_allocator_type can only be done once.");
- auto global_heap_allocator::create_or_get() -> heap_allocator &
- {
- static heap_allocator * allocator_ptr = nullptr;
-
- if (allocator_ptr == nullptr)
+ switch (new_type)
{
- switch (allocator_type)
- {
- case heap_allocator_type::NONE:
- // Nothing to do.
- break;
- case heap_allocator_type::BUMP: {
- static bump_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE};
- allocator_ptr = &allocator;
- break;
- }
- case heap_allocator_type::LINKED_LIST: {
- static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE};
- allocator_ptr = &allocator;
- break;
- }
+ case heap_allocator_type::NONE:
+ // Nothing to do
+ break;
+ case heap_allocator_type::BUMP: {
+ static bump_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE};
+ allocator_instance = &allocator;
+ break;
+ }
+ case heap_allocator_type::LINKED_LIST: {
+ static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE};
+ allocator_instance = &allocator;
+ break;
}
- exception_handling::assert(allocator_ptr != nullptr,
- "Attempted to allocate or deallocate using the global_heap_allocator before "
- "register_heap_allocation_type was called.");
}
+ }
- return *allocator_ptr;
+ auto global_heap_allocator::get() -> heap_allocator &
+ {
+ exception_handling::assert(allocator_instance != nullptr,
+ "Attempted to allocate or deallocate using the global_heap_allocator before "
+ "register_heap_allocation_type was called.");
+
+ return *allocator_instance;
}
} // namespace teachos::arch::memory::heap
@@ -61,10 +52,8 @@ auto operator new(std::size_t size) -> void *
auto operator delete(void * pointer) noexcept -> void
{
- if (pointer == nullptr)
- {
- }
- teachos::arch::exception_handling::panic("Called delete operator without passing required size attribute");
+ teachos::arch::exception_handling::assert(false && pointer == nullptr,
+ "Called delete operator without passing required size attribute");
}
auto operator delete(void * pointer, std::size_t size) noexcept -> void
@@ -79,10 +68,8 @@ auto operator new[](std::size_t size) -> void *
auto operator delete[](void * pointer) noexcept -> void
{
- if (pointer == nullptr)
- {
- }
- teachos::arch::exception_handling::panic("Called delete[] operator without passing required size attribute");
+ teachos::arch::exception_handling::assert(false && pointer == nullptr,
+ "Called delete[] operator without passing required size attribute");
}
auto operator delete[](void * pointer, std::size_t size) noexcept -> void
diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp
index b978319..a6f91d9 100644
--- a/arch/x86_64/src/memory/main.cpp
+++ b/arch/x86_64/src/memory/main.cpp
@@ -4,7 +4,7 @@
#include "arch/memory/allocator/area_frame_allocator.hpp"
#include "arch/memory/cpu/control_register.hpp"
#include "arch/memory/cpu/msr.hpp"
-#include "arch/memory/heap/concept.hpp"
+#include "arch/memory/heap/heap_allocator.hpp"
#include "arch/memory/paging/active_page_table.hpp"
#include "arch/memory/paging/kernel_mapper.hpp"