aboutsummaryrefslogtreecommitdiff
path: root/mirai/memory/zone/create/create.zig
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-03-30 15:02:42 +0530
committerBobby <[email protected]>2026-03-30 15:02:42 +0530
commit2324951126b542aeecfd8dd12b381265cce1566c (patch)
treea580fe97a13788fbe3b104e3a9553f551c2bff11 /mirai/memory/zone/create/create.zig
parent3c2c5c419cae1b7f2d60e8a3dc6e2e8c157b5a2f (diff)
downloadakiba-main.tar.xz
akiba-main.zip
refactor: reorganize kernel structure and implement memory management zonesHEADmain
Diffstat (limited to 'mirai/memory/zone/create/create.zig')
-rw-r--r--mirai/memory/zone/create/create.zig39
1 files changed, 39 insertions, 0 deletions
diff --git a/mirai/memory/zone/create/create.zig b/mirai/memory/zone/create/create.zig
new file mode 100644
index 0000000..d115df0
--- /dev/null
+++ b/mirai/memory/zone/create/create.zig
@@ -0,0 +1,39 @@
+//! Zone Creation
+
+const types = @import("../types/types.zig");
+const Zone = types.Zone;
+const page_size = types.page_size;
+const min_elem_size = types.min_elem_size;
+
+const bootstrap = @import("../bootstrap/bootstrap.zig");
+const alloc_mod = @import("../alloc/alloc.zig");
+
+pub const CreateError = error{
+ NotInitialized,
+ OutOfMemory,
+};
+
+pub fn create(name: []const u8, elem_size: usize) CreateError!*Zone {
+ if (!bootstrap.is_initialized()) return CreateError.NotInitialized;
+
+ const zone_zone = bootstrap.get_zone_zone();
+ const ptr = alloc_mod.zalloc(zone_zone) catch return CreateError.OutOfMemory;
+ const zone: *Zone = @ptrCast(@alignCast(ptr));
+
+ const actual_size = if (elem_size < min_elem_size) min_elem_size else elem_size;
+ const aligned_size = (actual_size + 7) & ~@as(usize, 7);
+
+ zone.elem_size = aligned_size;
+ zone.elems_per_page = page_size / aligned_size;
+ zone.partial_pages = null;
+ zone.full_pages = null;
+ zone.alloc_count = 0;
+ zone.free_count = 0;
+ zone.page_count = 0;
+
+ const copy_len = @min(name.len, 31);
+ @memcpy(zone.name[0..copy_len], name[0..copy_len]);
+ zone.name_len = @truncate(copy_len);
+
+ return zone;
+}