authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2023-07-16 21:20:55-04:00
committergravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2023-08-02 17:39:52-04:00
log841b54f5e3447ee94d89007bf670b3924f78b0d2
treec8f2442e41f06b684e43f0f48f361936b814f57f
parent4d711e8edd1234e727ba7e801b71090dc885249e

std: add SbrkAllocator and use it for Plan 9

Implements issue #6451. This was needed to support allocation on Plan 9 and now other operating systems like DOS can also use it. It is a modified version of the WasmAllocator since wasm also uses a sbrk-esque allocation system. This commit also adds the necessary system bits for sbrk to work on plan 9.

3 files changed, 201 insertions(+), 0 deletions(-)

lib/std/heap.zig+6
...@@ -21,6 +21,7 @@ pub const WasmAllocator = @import("heap/WasmAllocator.zig");...@@ -21,6 +21,7 @@ pub const WasmAllocator = @import("heap/WasmAllocator.zig");
21pub const WasmPageAllocator = @import("heap/WasmPageAllocator.zig");21pub const WasmPageAllocator = @import("heap/WasmPageAllocator.zig");
22pub const PageAllocator = @import("heap/PageAllocator.zig");22pub const PageAllocator = @import("heap/PageAllocator.zig");
23pub const ThreadSafeAllocator = @import("heap/ThreadSafeAllocator.zig");23pub const ThreadSafeAllocator = @import("heap/ThreadSafeAllocator.zig");
24pub const SbrkAllocator = @import("heap/sbrk_allocator.zig").SbrkAllocator;
2425
25const memory_pool = @import("heap/memory_pool.zig");26const memory_pool = @import("heap/memory_pool.zig");
26pub const MemoryPool = memory_pool.MemoryPool;27pub const MemoryPool = memory_pool.MemoryPool;
...@@ -228,6 +229,11 @@ pub const page_allocator = if (builtin.target.isWasm())...@@ -228,6 +229,11 @@ pub const page_allocator = if (builtin.target.isWasm())
228 .ptr = undefined,229 .ptr = undefined,
229 .vtable = &WasmPageAllocator.vtable,230 .vtable = &WasmPageAllocator.vtable,
230 }231 }
232else if (builtin.target.os.tag == .plan9)
233 Allocator{
234 .ptr = undefined,
235 .vtable = &SbrkAllocator(std.os.plan9.sbrk).vtable,
236 }
231else if (builtin.target.os.tag == .freestanding)237else if (builtin.target.os.tag == .freestanding)
232 root.os.heap.page_allocator238 root.os.heap.page_allocator
233else239else
lib/std/heap/sbrk_allocator.zig created+161
...@@ -0,0 +1,161 @@
1const std = @import("../std.zig");
2const builtin = @import("builtin");
3const math = std.math;
4const Allocator = std.mem.Allocator;
5const mem = std.mem;
6const assert = std.debug.assert;
7
8pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
9 return struct {
10 pub const vtable = Allocator.VTable{
11 .alloc = alloc,
12 .resize = resize,
13 .free = free,
14 };
15
16 pub const Error = Allocator.Error;
17
18 lock: std.Thread.Mutex = .{},
19
20 const max_usize = math.maxInt(usize);
21 const ushift = math.Log2Int(usize);
22 const bigpage_size = 64 * 1024;
23 const pages_per_bigpage = bigpage_size / mem.page_size;
24 const bigpage_count = max_usize / bigpage_size;
25
26 /// Because of storing free list pointers, the minimum size class is 3.
27 const min_class = math.log2(math.ceilPowerOfTwoAssert(usize, 1 + @sizeOf(usize)));
28 const size_class_count = math.log2(bigpage_size) - min_class;
29 /// 0 - 1 bigpage
30 /// 1 - 2 bigpages
31 /// 2 - 4 bigpages
32 /// etc.
33 const big_size_class_count = math.log2(bigpage_count);
34
35 var next_addrs = [1]usize{0} ** size_class_count;
36 /// For each size class, points to the freed pointer.
37 var frees = [1]usize{0} ** size_class_count;
38 /// For each big size class, points to the freed pointer.
39 var big_frees = [1]usize{0} ** big_size_class_count;
40
41 // TODO don't do the naive locking strategy
42 var lock: std.Thread.Mutex = .{};
43 fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*]u8 {
44 _ = ctx;
45 _ = return_address;
46 lock.lock();
47 defer lock.unlock();
48 // Make room for the freelist next pointer.
49 const alignment = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_align));
50 const actual_len = @max(len +| @sizeOf(usize), alignment);
51 const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;
52 const class = math.log2(slot_size) - min_class;
53 if (class < size_class_count) {
54 const addr = a: {
55 const top_free_ptr = frees[class];
56 if (top_free_ptr != 0) {
57 const node = @as(*usize, @ptrFromInt(top_free_ptr + (slot_size - @sizeOf(usize))));
58 frees[class] = node.*;
59 break :a top_free_ptr;
60 }
61
62 const next_addr = next_addrs[class];
63 if (next_addr % mem.page_size == 0) {
64 const addr = allocBigPages(1);
65 if (addr == 0) return null;
66 //std.debug.print("allocated fresh slot_size={d} class={d} addr=0x{x}\n", .{
67 // slot_size, class, addr,
68 //});
69 next_addrs[class] = addr + slot_size;
70 break :a addr;
71 } else {
72 next_addrs[class] = next_addr + slot_size;
73 break :a next_addr;
74 }
75 };
76 return @as([*]u8, @ptrFromInt(addr));
77 }
78 const bigpages_needed = bigPagesNeeded(actual_len);
79 const addr = allocBigPages(bigpages_needed);
80 return @as([*]u8, @ptrFromInt(addr));
81 }
82
83 fn resize(
84 ctx: *anyopaque,
85 buf: []u8,
86 log2_buf_align: u8,
87 new_len: usize,
88 return_address: usize,
89 ) bool {
90 _ = ctx;
91 _ = return_address;
92 lock.lock();
93 defer lock.unlock();
94 // We don't want to move anything from one size class to another, but we
95 // can recover bytes in between powers of two.
96 const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
97 const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);
98 const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);
99 const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);
100 const old_small_class = math.log2(old_small_slot_size) - min_class;
101 if (old_small_class < size_class_count) {
102 const new_small_slot_size = math.ceilPowerOfTwo(usize, new_actual_len) catch return false;
103 return old_small_slot_size == new_small_slot_size;
104 } else {
105 const old_bigpages_needed = bigPagesNeeded(old_actual_len);
106 const old_big_slot_pages = math.ceilPowerOfTwoAssert(usize, old_bigpages_needed);
107 const new_bigpages_needed = bigPagesNeeded(new_actual_len);
108 const new_big_slot_pages = math.ceilPowerOfTwo(usize, new_bigpages_needed) catch return false;
109 return old_big_slot_pages == new_big_slot_pages;
110 }
111 }
112
113 fn free(
114 ctx: *anyopaque,
115 buf: []u8,
116 log2_buf_align: u8,
117 return_address: usize,
118 ) void {
119 _ = ctx;
120 _ = return_address;
121 lock.lock();
122 defer lock.unlock();
123 const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
124 const actual_len = @max(buf.len + @sizeOf(usize), buf_align);
125 const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);
126 const class = math.log2(slot_size) - min_class;
127 const addr = @intFromPtr(buf.ptr);
128 if (class < size_class_count) {
129 const node = @as(*usize, @ptrFromInt(addr + (slot_size - @sizeOf(usize))));
130 node.* = frees[class];
131 frees[class] = addr;
132 } else {
133 const bigpages_needed = bigPagesNeeded(actual_len);
134 const pow2_pages = math.ceilPowerOfTwoAssert(usize, bigpages_needed);
135 const big_slot_size_bytes = pow2_pages * bigpage_size;
136 const node = @as(*usize, @ptrFromInt(addr + (big_slot_size_bytes - @sizeOf(usize))));
137 const big_class = math.log2(pow2_pages);
138 node.* = big_frees[big_class];
139 big_frees[big_class] = addr;
140 }
141 }
142
143 inline fn bigPagesNeeded(byte_count: usize) usize {
144 return (byte_count + (bigpage_size + (@sizeOf(usize) - 1))) / bigpage_size;
145 }
146
147 fn allocBigPages(n: usize) usize {
148 const pow2_pages = math.ceilPowerOfTwoAssert(usize, n);
149 const slot_size_bytes = pow2_pages * bigpage_size;
150 const class = math.log2(pow2_pages);
151
152 const top_free_ptr = big_frees[class];
153 if (top_free_ptr != 0) {
154 const node = @as(*usize, @ptrFromInt(top_free_ptr + (slot_size_bytes - @sizeOf(usize))));
155 big_frees[class] = node.*;
156 return top_free_ptr;
157 }
158 return sbrk(pow2_pages * pages_per_bigpage * mem.page_size);
159 }
160 };
161}
lib/std/os/plan9.zig+34
...@@ -216,3 +216,37 @@ pub const O = struct {...@@ -216,3 +216,37 @@ pub const O = struct {
216 pub const RCLOSE = 64; // or'ed in, remove on close216 pub const RCLOSE = 64; // or'ed in, remove on close
217 pub const EXCL = 0x1000; // or'ed in, exclusive create217 pub const EXCL = 0x1000; // or'ed in, exclusive create
218};218};
219
220pub const ExecData = struct {
221 pub extern const etext: anyopaque;
222 pub extern const edata: anyopaque;
223 pub extern const end: anyopaque;
224};
225
226/// Brk sets the system's idea of the lowest bss location not
227/// used by the program (called the break) to addr rounded up to
228/// the next multiple of 8 bytes. Locations not less than addr
229/// and below the stack pointer may cause a memory violation if
230/// accessed. -9front brk(2)
231pub fn brk_(addr: usize) i32 {
232 return @intCast(syscall_bits.syscall1(.BRK_, addr));
233}
234var bloc: usize = 0;
235var bloc_max: usize = 0;
236
237pub fn sbrk(n: usize) usize {
238 if (bloc == 0) {
239 // we are at the start
240 bloc = @intFromPtr(&ExecData.end);
241 bloc_max = @intFromPtr(&ExecData.end);
242 }
243 var bl = std.mem.alignForward(usize, bloc, std.mem.page_size);
244 const n_aligned = std.mem.alignForward(usize, n, std.mem.page_size);
245 if (bl + n_aligned > bloc_max) {
246 // we need to allocate
247 if (brk_(bl + n_aligned) < 0) return 0;
248 bloc_max = bl + n_aligned;
249 }
250 bloc = bloc + n_aligned;
251 return bl;
252}