Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 34 additions & 42 deletions runtime/src/allocator.zig
Original file line number Diff line number Diff line change
@@ -1,70 +1,62 @@
const Config = @import("config.zig");
const AllocatorConfig = @import("allocator_config.zig");
const Arena = @import("arena.zig");
const RBTree = @import("rbtree.zig").rbtree(Arena);
const Array = @import("dynamic_array.zig");
const RBTree = @import("rbtree.zig").rbtree;
const Arrays = @import("arrays.zig");
const Utils = @import("utils.zig");
const LinkedList = @import("linked_list.zig").linked_list;

const c = @cImport({
@cInclude("stdlib.h");
});

const Self = @This();
const IndexOfArena = u32;

arena_size: usize,
capacity_of_heap: usize,
arenas: []Arena,
treeped_arenas: RBTree,

archive_of_arenas: Array.dynamic_array(IndexOfArena),
active_arenas: Array.dynamic_array(IndexOfArena),
current_arenas: Array.dynamic_array(IndexOfArena),
managed_arenas: LinkedList(*Arena, *Arena),
treeped_managed_arenas: RBTree(*Arena),
arenas_for_rebuild: LinkedList(*Arena, *Arena),
native_arenas: LinkedList(*Arena, *Arena),
treeped_native_arenas: RBTree(*Arena),
boot_arena: Arena,
boot_allocator: *Self = null,

// pub fn alloc(self: *Self, size: usize) ?[*]u8 {
//
// }

pub const AllocatorError = error{InvalidConfig, OOMInInit, EmptyArchive, OOM};
pub const AllocatorError = error{InvalidConfig, OOMInInit, EmptyArchive, OOM, FreeOnManagedArena, PageSizeIsNotSupport, ArenaSizeIsLarge};

pub fn calloc(count: usize, T: type) ?[]T {
const raw: [*]T = @ptrCast(@alignCast(c.malloc(@sizeOf(T) * count) orelse return null));
return raw[0..count];
}

pub fn init(cfg: Config) AllocatorError!*Self {
if (cfg.log_arena_size > cfg.log_capacity_of_heap)
pub fn init(cfg: AllocatorConfig) AllocatorError!*Self {
const page_size = Utils.get_page_size();
if (page_size > Arena.MAX_SUPPORT_PAGE_SIZE) return AllocatorError.PageSizeIsNotSupport;

if (1 << cfg.log_arena_size > cfg.capacity_of_heap)
return AllocatorError.InvalidConfig;
if (cfg.log_arena_size < 8) return AllocatorError.InvalidConfig;
if (cfg.log_arena_size > 26) return AllocatorError.InvalidConfig;
if (cfg.log_arena_size > 16) return AllocatorError.InvalidConfig;
if ((1 << cfg.log_arena_size) < cfg.max_size_of_object_on_arena)
return AllocatorError.InvalidConfig;

const count_of_arenas = 1 << (cfg.log_capacity_of_heap - cfg.log_arena_size);
var this = Self{};
this.arena_size = 1 << cfg.log_arena_size;
this.capacity_of_heap = 1 << cfg.log_capacity_of_heap;

this.arenas = calloc(count_of_arenas, Arena)
orelse return AllocatorError.OOMInInit;
this.boot_arena = Arena{};
const count_of_arenas = cfg.capacity_of_heap / (1 << cfg.log_arena_size);
const predict = Utils.roundUpToN(count_of_arenas * 256, page_size);
this.boot_arena.init(predict, false, true) catch |err| switch (err) {

this.archive_of_arenas = Array.dynamic_array(IndexOfArena).init(count_of_arenas)
orelse return AllocatorError.OOMInInit;
error.ArenaSizeIsLarge => {
try this.boot_arena.init(page_size, false, true);
this.boot_allocator = try this.boot_arena.new(Self);
this.boot_allocator.* = Self.init(AllocatorConfig{
.capacity_of_heap = predict,
.log_arena_size = 16,
.max_size_of_object_on_arena = predict / 4
});
},

this.active_arenas = Array.dynamic_array(IndexOfArena).init(count_of_arenas)
orelse return AllocatorError.OOMInInit;
else => return err
};

this.current_arenas = Array.dynamic_array(IndexOfArena).init(count_of_arenas)
orelse return AllocatorError.OOMInInit;

for (0..count_of_arenas) |i| {
this.archive_of_arenas.pop(count_of_arenas - i - 1);
}
}

fn unarchive(self: *Self) AllocatorError!void {
const new_arena = self.archive_of_arenas.pop()
orelse return AllocatorError.EmptyArchive;
self.active_arenas.push(new_arena);
self.current_arenas.push(new_arena);

self.arenas[new_arena].
}
4 changes: 4 additions & 0 deletions runtime/src/allocator_config.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

capacity_of_heap: usize,
log_arena_size: usize,
max_size_of_object_on_arena: usize,
Loading