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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
const std = @import("std");
const paging = @import("paging.zig");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const maxInt = std.math.maxInt;
const mem = std.mem;
const Chunk = struct {
flags: Flags,
len: usize,
const Flags = packed struct(u8) {
active: u1,
reserved: u7,
};
pub fn next(self: *align(1) Chunk) *align(1) Chunk {
const byte_ptr: [*]u8 = @ptrCast(self);
return @ptrCast(byte_ptr + @sizeOf(Chunk) + self.len);
}
pub fn take(self: *align(1) Chunk) void {
self.flags.active = 1;
}
pub fn clear(self: *align(1) Chunk) void {
self.flags = mem.zeroInit(Flags, .{});
}
pub fn data(self: *align(1) Chunk) []u8 {
const byte_ptr: [*]u8 = @ptrCast(self);
return byte_ptr[@sizeOf(Chunk)..self.len];
}
};
pub const ChunkAllocatorConfig = struct {
auto_merge_free: bool = true,
};
pub fn ChunkAllocator(comptime config: ChunkAllocatorConfig) type {
return struct {
head: ?*align(1) Chunk,
pages: usize,
const Self = @This();
pub fn init(pages: usize) !Self {
const head: *align(1) Chunk = @ptrCast(try paging.zeroedAlloc(pages));
head.len = (pages * paging.page_size) - @sizeOf(Chunk);
return .{ .head = head, .pages = pages };
}
pub fn deinit(self: *Self) void {
if (self.head) |head| {
paging.free(head);
self.head = null;
}
}
pub fn allocator(self: *Self) Allocator {
return .{
.ptr = self,
.vtable = &.{
.alloc = alloc,
.resize = resize,
.free = free,
},
};
}
pub fn alloc(ctx: *anyopaque, len: usize, log2_ptr_align: u8, ret_addr: usize) ?[*]u8 {
_ = ret_addr;
const self: *Self = @ptrCast(@alignCast(ctx));
const ptr_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_ptr_align));
var chunk = self.head orelse return null;
const bound = @intFromPtr(chunk) + (self.pages * paging.page_size);
var predecessor: ?*align(1) Chunk = null;
while (@intFromPtr(chunk) < bound) : (chunk = chunk.next()) {
const adjust_off = mem.alignPointerOffset(chunk.data().ptr, ptr_align) orelse return null;
const aligned_len = len + adjust_off;
// Is this chunk free and large enough to hold the requested allocation?
if (!@bitCast(chunk.flags.active) and chunk.len >= aligned_len) {
const remaining = chunk.len - aligned_len;
if (predecessor) |*pred| {
pred.*.len += adjust_off;
} else if (adjust_off != 0) return null;
chunk = @ptrFromInt(@intFromPtr(chunk) + adjust_off);
chunk.clear();
chunk.take();
if (remaining > @sizeOf(Chunk)) {
chunk.len = len;
const new_successor = chunk.next();
new_successor.clear();
new_successor.len = remaining - @sizeOf(Chunk);
}
return chunk.data().ptr;
}
predecessor = chunk;
}
return null;
}
// Only expands into the next free chunk (if there is one).
// You may want to call mergeFree first if auto_merge_free was configured to false.
pub fn resize(ctx: *anyopaque, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
_ = ret_addr;
const self: *Self = @ptrCast(@alignCast(ctx));
const ptr_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
const head = self.head orelse return false;
const bound = @intFromPtr(head) + (self.pages * paging.page_size);
const chunk = @as(*align(1) Chunk, @ptrCast(buf.ptr - @sizeOf(Chunk)));
const adjust_off = mem.alignPointerOffset(buf.ptr, ptr_align) orelse return false;
const aligned_new_len = new_len + adjust_off;
if (aligned_new_len < chunk.len) {
const regained = chunk.len - aligned_new_len;
if (regained > @sizeOf(Chunk)) {
chunk.len = aligned_new_len;
const new_successor = chunk.next();
new_successor.clear();
new_successor.len = regained - @sizeOf(Chunk);
}
return true;
} else if (aligned_new_len > chunk.len) {
const successor = chunk.next();
if (@intFromPtr(successor) >= bound) return false;
const total_len = chunk.len + @sizeOf(Chunk) + successor.len;
if (!@bitCast(successor.flags.active) and aligned_new_len <= total_len) {
const remaining = total_len - aligned_new_len;
if (remaining > @sizeOf(Chunk)) {
chunk.len = aligned_new_len;
const new_successor = chunk.next();
new_successor.clear();
new_successor.len = remaining - @sizeOf(Chunk);
} else {
chunk.len = total_len;
}
return true;
}
return false;
} else return true;
}
pub fn free(ctx: *anyopaque, old_mem: []u8, log2_old_align: u8, ret_addr: usize) void {
_ = log2_old_align;
_ = ret_addr;
const self: *Self = @ptrCast(@alignCast(ctx));
// Safety check. Do not free memory in uninitialized / undefined pages.
if (self.head == null) return;
const chunk = @as([*]Chunk, @ptrCast(@alignCast(old_mem.ptr))) - 1;
chunk[0].clear();
if (config.auto_merge_free) {
self.mergeFree();
}
}
pub fn mergeFree(self: *Self) void {
var chunk = self.head orelse return;
const bound = @intFromPtr(chunk) + (self.pages * paging.page_size);
while (@intFromPtr(chunk) < bound) : (chunk = chunk.next()) {
const successor = chunk.next();
if (@intFromPtr(successor) >= bound) {
// Safety check.
// Should never run if the implementation is working correctly.
//
// Ensure that there is a successor within bounds.
// The loop condition is not sufficient here, it only detects
// non-erroneous list ends (i.e. chunk == bound).
break;
} else if (!@bitCast(chunk.flags.active) and !@bitCast(successor.flags.active)) {
chunk.len += @sizeOf(Chunk) + successor.len;
}
}
}
};
}
pub const PageAllocator = struct {
pub const vtable = Allocator.VTable{
.alloc = alloc,
.resize = resize,
.free = free,
};
fn alloc(_: *anyopaque, n: usize, log2_align: u8, ra: usize) ?[*]u8 {
_ = ra;
_ = log2_align;
assert(n > 0);
if (n > maxInt(usize) - (paging.page_size - 1)) return null;
const aligned_len = mem.alignForward(usize, n, paging.page_size);
const num_pages = @divExact(aligned_len, paging.page_size);
const slice = paging.zeroedAlloc(num_pages) catch return null;
assert(mem.isAligned(@intFromPtr(slice.ptr), paging.page_size));
return slice.ptr;
}
fn resize(_: *anyopaque, buf_unaligned: []u8, log2_buf_align: u8, new_size: usize, return_address: usize) bool {
_ = log2_buf_align;
_ = return_address;
const new_size_aligned = mem.alignForward(usize, new_size, paging.page_size);
const buf_aligned_len = mem.alignForward(usize, buf_unaligned.len, paging.page_size);
if (new_size_aligned == buf_aligned_len) {
return true;
}
return false;
}
fn free(_: *anyopaque, slice: []u8, log2_buf_align: u8, return_address: usize) void {
_ = log2_buf_align;
_ = return_address;
const buf_aligned_len = mem.alignForward(usize, slice.len, paging.page_size);
paging.free(slice.ptr[0..buf_aligned_len]);
}
};
pub const page_allocator = Allocator{
.ptr = undefined,
.vtable = &PageAllocator.vtable,
};
|