blob: d115df008cace36108afa949771172e971c1f346 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
}
|