aboutsummaryrefslogtreecommitdiff
path: root/kernel/l0/frame.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/l0/frame.c')
-rw-r--r--kernel/l0/frame.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/kernel/l0/frame.c b/kernel/l0/frame.c
index c646a48..489d010 100644
--- a/kernel/l0/frame.c
+++ b/kernel/l0/frame.c
@@ -1,6 +1,8 @@
#include <frame.h>
#include <dbglog.h>
+#include <mutex.h>
+
// TODO: buddy allocator
// this is a simple bitmap allocator
@@ -32,9 +34,12 @@ void frame_init_allocator(size_t total_ram, void** kernel_data_end) {
begin_search_at = INDEX_FROM_BIT(kernel_pages);
}
+STATIC_MUTEX(frame_allocator_mutex);
+
uint32_t frame_alloc(size_t n) {
if (n > 32) return 0;
+ mutex_lock(&frame_allocator_mutex);
for (uint32_t i = begin_search_at; i < INDEX_FROM_BIT(nframes); i++) {
if (frame_bitset[i] == 0xFFFFFFFF) {
if (i == begin_search_at) begin_search_at++;
@@ -46,14 +51,19 @@ uint32_t frame_alloc(size_t n) {
if (!(frame_bitset[i]&to_test)) {
frame_bitset[i] |= to_test;
nused_frames += n;
+
+ mutex_unlock(&frame_allocator_mutex);
return i * 32 + j;
}
}
}
+ mutex_unlock(&frame_allocator_mutex);
return 0;
}
void frame_free(uint32_t base, size_t n) {
+ mutex_lock(&frame_allocator_mutex);
+
for (size_t i = 0; i < n; i++) {
uint32_t idx = INDEX_FROM_BIT(base + i);
uint32_t ofs = OFFSET_FROM_BIT(base + i);
@@ -64,6 +74,8 @@ void frame_free(uint32_t base, size_t n) {
}
if (INDEX_FROM_BIT(base) < begin_search_at)
begin_search_at = INDEX_FROM_BIT(base);
+
+ mutex_unlock(&frame_allocator_mutex);
}
void dbg_print_frame_stats() {