Something went wrong. Try again.
[WIP] A toy kernel written in Zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153const std = @import("std");
const kernel = @import("../root.zig");const mem = kernel.mem;
const Allocator = @This();
page_allocator: std.mem.Allocator,
next_addrs: [size_class_count]?[]u8 = @splat(null),frees: [size_class_count]?*FreeBlock = @splat(null),
const FreeBlock = extern struct { next: ?*FreeBlock,
pub fn asPtr(self: *FreeBlock) [*]u8 { return @ptrCast(self); }};
pub fn init(page_allocator: std.mem.Allocator) Allocator { return .{ .page_allocator = page_allocator };}
const max_class: usize = std.math.log2(mem.pageSize) - 1;const min_class: usize = std.math.log2(@sizeOf(usize));const size_class_count: usize = max_class - min_class + 1;
pub fn allocator(self: *Allocator) std.mem.Allocator { return .{ .ptr = self, .vtable = &vtable, };}
const vtable: std.mem.Allocator.VTable = .{ .alloc = &alloc, .resize = &resize, .remap = &remap, .free = &free,};
fn alloc( ptr: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize,) ?[*]u8 { var self: *Allocator = @ptrCast(@alignCast(ptr));
const class = sizeClassIndex(len, alignment); const slot_size = slotSize(class);
if (class >= size_class_count or slot_size >= mem.pageSize) { @branchHint(.unlikely); return self.page_allocator.rawAlloc(len, alignment, ret_addr); }
if (self.frees[class]) |free_block| { @branchHint(.likely); self.frees[class] = free_block.next; return free_block.asPtr(); }
var next_addr = self.next_addrs[class] orelse self.page_allocator.alloc(u8, mem.pageSize) catch return null;
const allocation = next_addr.ptr;
next_addr = next_addr[slot_size..]; self.next_addrs[class] = if (next_addr.len == 0) null else next_addr;
return allocation;}
pub fn resize( ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize,) bool { var self: *Allocator = @ptrCast(@alignCast(ptr));
const class = sizeClassIndex(memory.len, alignment); const new_class = sizeClassIndex(new_len, alignment);
if (class >= size_class_count) { if (new_class < size_class_count) return false; return self.page_allocator.rawResize(memory, alignment, new_len, ret_addr); }
return new_class == class;}
pub fn remap( ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize,) ?[*]u8 { var self: *Allocator = @ptrCast(@alignCast(ptr));
const class = sizeClassIndex(memory.len, alignment); const new_class = sizeClassIndex(new_len, alignment);
if (class >= size_class_count) { if (new_class < size_class_count) return null; return self.page_allocator.rawRemap(memory, alignment, new_len, ret_addr); }
return if (new_class == class) memory.ptr else null;}
fn free( ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret_addr: usize,) void { var self: *Allocator = @ptrCast(@alignCast(ptr));
const class = sizeClassIndex(memory.len, alignment); const slot_size = slotSize(class);
if (class >= size_class_count or slot_size >= mem.pageSize) { @branchHint(.unlikely); return self.page_allocator.rawFree(memory, alignment, ret_addr); }
const free_block: *FreeBlock = @ptrCast(@alignCast(memory.ptr)); free_block.next = self.frees[class]; self.frees[class] = free_block;}
fn sizeClassIndex(len: usize, alignment: std.mem.Alignment) usize { return @max( @bitSizeOf(usize) - @clz(len - 1), @intFromEnum(alignment), min_class, ) - min_class;}
fn slotSize(class: usize) usize { return @as(usize, 1) << @intCast(class + min_class);}