aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2024-12-03 09:13:53 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2024-12-03 09:13:53 +0000
commit05fe50cefb12a7333a320a3d101dccdd13b8034a (patch)
treed8857629506a93536eee255cbc8cd303473a81f5
parent6dff0ff5bcdd63de4a68f9c361acd0bace39b5ca (diff)
downloadteachos-05fe50cefb12a7333a320a3d101dccdd13b8034a.tar.xz
teachos-05fe50cefb12a7333a320a3d101dccdd13b8034a.zip
Clear old memory in contructor
-rw-r--r--arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp10
-rw-r--r--arch/x86_64/include/arch/memory/heap/memory_block.hpp15
-rw-r--r--arch/x86_64/src/memory/heap/linked_list_allocator.cpp14
-rw-r--r--arch/x86_64/src/memory/heap/memory_block.cpp10
4 files changed, 21 insertions, 28 deletions
diff --git a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp
index 5ff13ca..03b8828 100644
--- a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp
@@ -121,16 +121,6 @@ namespace teachos::arch::memory::heap
auto coalesce_free_memory_block(memory_block * previous_block, memory_block * current_block, void * pointer,
std::size_t size) -> void;
- /**
- * @brief Clears the memory of the previous memory block header.
- *
- * @note Done so the given pointer can be reused to construct other classes into, without having the old values.
- * Required because we cannot call delete, because it causes "undefined reference to `sbrk`".
- *
- * @param pointer Address we want to clear the memory block header at (16 bytes).
- */
- auto clear_memory_block_header(void * pointer) -> void;
-
std::size_t heap_start; ///< Start of the allocatable heap area.
std::size_t heap_end; ///< End of the allocatable heap area.
memory_block * first; ///< First free entry in our memory.
diff --git a/arch/x86_64/include/arch/memory/heap/memory_block.hpp b/arch/x86_64/include/arch/memory/heap/memory_block.hpp
index c48d0cd..c62dd57 100644
--- a/arch/x86_64/include/arch/memory/heap/memory_block.hpp
+++ b/arch/x86_64/include/arch/memory/heap/memory_block.hpp
@@ -12,15 +12,24 @@ namespace teachos::arch::memory::heap
struct memory_block
{
/**
- * @brief Constructor,
+ * @brief Constructor. Clears all memory from the place it was allocated until the end (address +
+ * size).
*
* @param size Amount of free memory of this specific hole.
* @param next Optional pointer to the next free memory.
*/
memory_block(std::size_t size, memory_block * next);
- std::size_t size; ///< Amount of free memory this hole contains, has to always be atleast 16 bytes to hold the size
- ///< variable and the pointer to the next hole.
+ /**
+ * @brief Destructor. Clears all internal memory.
+ *
+ * @note Used so the memory can be reused to construct other classes into, without having the old values.
+ * Required because we cannot call delete, because it causes "undefined reference to `sbrk`".
+ */
+ ~memory_block();
+
+ std::size_t size; ///< Amount of free memory this hole contains, has to always be atleast 16 bytes to hold the
+ ///< size variable and the pointer to the next hole.
memory_block * next; ///< Optional pointer to the next free memory, holds nullptr if there is none.
};
} // namespace teachos::arch::memory::heap
diff --git a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
index d0bf8e6..e5bae21 100644
--- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
+++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
@@ -3,10 +3,6 @@
#include "arch/exception_handling/assert.hpp"
#include "arch/exception_handling/panic.hpp"
-#include <string.h>
-
-#include <algorithm>
-
namespace teachos::arch::memory::heap
{
linked_list_allocator::linked_list_allocator(std::size_t heap_start, std::size_t heap_end)
@@ -112,7 +108,7 @@ namespace teachos::arch::memory::heap
{
previous_block->next = new_block;
}
- clear_memory_block_header(current_block);
+ current_block->~memory_block();
return reinterpret_cast<void *>(start_address);
}
@@ -134,8 +130,7 @@ namespace teachos::arch::memory::heap
{
block_size += current_block->size;
next_block = current_block->next;
-
- clear_memory_block_header(current_block);
+ current_block->~memory_block();
}
// If the block we want to deallocate is behind another free block and we can therefore combine both into one.
@@ -170,9 +165,4 @@ namespace teachos::arch::memory::heap
previous_block->next = new_block;
}
- auto linked_list_allocator::clear_memory_block_header(void * pointer) -> void
- {
- memset(pointer, 0, min_allocatable_size());
- }
-
} // namespace teachos::arch::memory::heap
diff --git a/arch/x86_64/src/memory/heap/memory_block.cpp b/arch/x86_64/src/memory/heap/memory_block.cpp
index b68dd6d..446cd96 100644
--- a/arch/x86_64/src/memory/heap/memory_block.cpp
+++ b/arch/x86_64/src/memory/heap/memory_block.cpp
@@ -1,11 +1,15 @@
#include "arch/memory/heap/memory_block.hpp"
+#include <string.h>
+
namespace teachos::arch::memory::heap
{
memory_block::memory_block(std::size_t size, memory_block * next)
- : size(size)
- , next(next)
{
- // Nothing to do
+ memset(static_cast<void *>(this), 0, size);
+ this->size = size;
+ this->next = next;
}
+
+ memory_block::~memory_block() { memset(static_cast<void *>(this), 0, sizeof(memory_block)); }
} // namespace teachos::arch::memory::heap