core: Add global FrameAllocator with locking
This commit is contained in:
parent
14862ec271
commit
f87426753b
@ -99,6 +99,11 @@ export fn _start(magic: u32, info: MultibootInfo) callconv(.C) noreturn {
|
||||
};
|
||||
thread.arch.setStack(&init.regs, stack);
|
||||
|
||||
pmm.setGlobalAllocator(&allocator) catch |err| {
|
||||
debug.print("Error while setting up global frame allocator: {}\n", .{err});
|
||||
while (true) {}
|
||||
};
|
||||
|
||||
platform.platformEndInit();
|
||||
|
||||
thread.enterTask(init);
|
||||
|
@ -1,8 +1,10 @@
|
||||
const std = @import("std");
|
||||
const easyboot = @cImport(@cInclude("easyboot.h"));
|
||||
const platform = @import("arch/platform.zig").arch;
|
||||
const vmm = @import("arch/vmm.zig").arch;
|
||||
const mmap = @import("mmap.zig");
|
||||
const bmap = @import("lib/bitmap.zig");
|
||||
const locking = @import("lib/spinlock.zig");
|
||||
|
||||
const FrameAllocatorError = error{
|
||||
InvalidMemoryMap,
|
||||
@ -104,3 +106,28 @@ pub fn initializeFrameAllocator(tag: *easyboot.multiboot_tag_mmap_t) !FrameAlloc
|
||||
|
||||
return allocator;
|
||||
}
|
||||
|
||||
var lock: locking.SpinLock = .{};
|
||||
var global_allocator: *FrameAllocator = undefined;
|
||||
|
||||
pub fn setGlobalAllocator(allocator: *FrameAllocator) !void {
|
||||
const frame = try allocFrame(allocator);
|
||||
const virt = frame.virtualAddress(vmm.PHYSICAL_MAPPING_BASE);
|
||||
|
||||
global_allocator = @ptrFromInt(virt);
|
||||
global_allocator.* = allocator.*;
|
||||
}
|
||||
|
||||
pub fn lockGlobalAllocator() *FrameAllocator {
|
||||
lock.lock();
|
||||
return global_allocator;
|
||||
}
|
||||
|
||||
pub fn tryLockGlobalAllocator() ?*FrameAllocator {
|
||||
if (!lock.tryLock()) return null;
|
||||
return global_allocator;
|
||||
}
|
||||
|
||||
pub fn unlockGlobalAllocator() void {
|
||||
lock.unlock();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user