diff --git a/src/libraries/kutil/heap_allocator.cpp b/src/libraries/kutil/heap_allocator.cpp index a80e8f8..01b9506 100644 --- a/src/libraries/kutil/heap_allocator.cpp +++ b/src/libraries/kutil/heap_allocator.cpp @@ -89,6 +89,8 @@ heap_allocator::allocate(size_t length) if (order > max_order) return nullptr; + scoped_lock lock {m_lock}; + mem_header *header = pop_free(order); header->set_used(true); m_allocated_size += (1 << order); @@ -104,6 +106,8 @@ heap_allocator::free(void *p) kassert(addr >= m_start && addr < m_end, "Attempt to free non-heap pointer"); + scoped_lock lock {m_lock}; + mem_header *header = reinterpret_cast(p); header -= 1; // p points after the header header->set_used(false); diff --git a/src/libraries/kutil/include/kutil/heap_allocator.h b/src/libraries/kutil/include/kutil/heap_allocator.h index 43b2dab..0424af4 100644 --- a/src/libraries/kutil/include/kutil/heap_allocator.h +++ b/src/libraries/kutil/include/kutil/heap_allocator.h @@ -3,6 +3,7 @@ /// A buddy allocator for a memory heap #include +#include "kutil/spinlock.h" namespace kutil { @@ -57,6 +58,8 @@ protected: mem_header *m_free[max_order - min_order + 1]; size_t m_allocated_size; + spinlock m_lock; + heap_allocator(const heap_allocator &) = delete; };