update to zig 0.11.0-dev.3886+0c1bfe271

This commit is contained in:
Emil Lerch 2023-07-03 15:27:21 -07:00
parent abc26837a0
commit d32beac025
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
6 changed files with 18 additions and 24 deletions

View File

@ -31,7 +31,7 @@ tied to Linux, the file watcher module uses inotify and friends and will not
work outside that OS. PRs are welcome. work outside that OS. PRs are welcome.
The system is built with a pre-release version of zig, currently zig version The system is built with a pre-release version of zig, currently zig version
[0.11.0-dev.3312+ab37ab33c](https://github.com/marler8997/zig-unofficial-releases#0110-dev3312ab37ab33c-summary). [0.11.0-dev.3886+0c1bfe271](https://github.com/marler8997/zig-unofficial-releases#0110-dev38860c1bfe271-summary).
This version has web server in the standard library, so it is useful. This version has web server in the standard library, so it is useful.
To achieve the lowest latency possible, this server loads dynamic libraries To achieve the lowest latency possible, this server loads dynamic libraries

View File

@ -145,17 +145,11 @@ pub fn startWatch(self: *Self) void {
var ptr: [*]u8 = &event_buf; var ptr: [*]u8 = &event_buf;
const end_ptr = ptr + bytes_read; const end_ptr = ptr + bytes_read;
while (@ptrToInt(ptr) < @ptrToInt(end_ptr)) { while (@intFromPtr(ptr) < @intFromPtr(end_ptr)) {
const ev = @ptrCast( const ev = @as(*const std.os.linux.inotify_event, @ptrCast(@alignCast(ptr)));
*const std.os.linux.inotify_event,
@alignCast(@alignOf(*const std.os.linux.inotify_event), ptr),
);
// Read next event from inotify // Read next event from inotify
ptr = @alignCast( ptr = ptr + @sizeOf(std.os.linux.inotify_event) + ev.len;
@alignOf(std.os.linux.inotify_event),
ptr + @sizeOf(std.os.linux.inotify_event) + ev.len,
);
self.processInotifyEvent(ev, ptr - ev.len); self.processInotifyEvent(ev, ptr - ev.len);
} }
} }
@ -213,7 +207,7 @@ fn processInotifyEvent(self: Self, ev: *const std.os.linux.inotify_event, name_p
// TODO: This should be a std.mem.sliceTo(@ptrCast([*:0]u8, name_ptr), ev.len); // TODO: This should be a std.mem.sliceTo(@ptrCast([*:0]u8, name_ptr), ev.len);
// and returning from C without a sentinal, we can use the same call, like this: // and returning from C without a sentinal, we can use the same call, like this:
// std.mem.sliceTo(@ptrCast([*]u8, name_ptr), len); // std.mem.sliceTo(@ptrCast([*]u8, name_ptr), len);
const name = std.mem.span(@ptrCast([*:0]u8, name_ptr)); const name = std.mem.span(@as([*:0]u8, @ptrCast(name_ptr)));
log.debug("MOVED_TO({d}/{d}): {s}", .{ name.len, ev.len, name }); log.debug("MOVED_TO({d}/{d}): {s}", .{ name.len, ev.len, name });
for (self.dir_wds) |dir| { for (self.dir_wds) |dir| {
if (ev.wd == dir.wd) { if (ev.wd == dir.wd) {

View File

@ -26,7 +26,7 @@ pub const ParsedConfig = struct {
pub fn deinit(self: *SelfConfig) void { pub fn deinit(self: *SelfConfig) void {
for (self.key_value_map.keys(), self.key_value_map.values()) |k, v| { for (self.key_value_map.keys(), self.key_value_map.values()) |k, v| {
// StringArrayHashMap assumes []const u8, but what we've allocated is null terminated // StringArrayHashMap assumes []const u8, but what we've allocated is null terminated
self.config_allocator.free(@ptrCast([:0]const u8, k)); // this is also the key in value_key_map self.config_allocator.free(@as([:0]const u8, @ptrCast(k))); // this is also the key in value_key_map
self.config_allocator.free(v); self.config_allocator.free(v);
} }

View File

@ -57,7 +57,7 @@ pub const ZigRequestHandler = *const fn (std.mem.Allocator, ZigRequest, ZigRespo
/// the handleRequest helper below, you must use zigInit or otherwise /// the handleRequest helper below, you must use zigInit or otherwise
/// set the interface allocator in your own version of zigInit /// set the interface allocator in your own version of zigInit
pub fn zigInit(parent_allocator: *anyopaque) callconv(.C) void { pub fn zigInit(parent_allocator: *anyopaque) callconv(.C) void {
allocator = @ptrCast(*std.mem.Allocator, @alignCast(@alignOf(*std.mem.Allocator), parent_allocator)); allocator = @ptrCast(@alignCast(parent_allocator));
} }
pub fn toZigHeader(header: Header) ZigHeader { pub fn toZigHeader(header: Header) ZigHeader {

View File

@ -86,15 +86,15 @@ test "handle_request" {
var aa = arena.allocator(); var aa = arena.allocator();
interface.zigInit(&aa); interface.zigInit(&aa);
var headers: []interface.Header = @constCast(&[_]interface.Header{.{ var headers: []interface.Header = @constCast(&[_]interface.Header{.{
.name_ptr = @ptrCast([*:0]u8, @constCast("GET".ptr)), .name_ptr = @ptrCast(@constCast("GET".ptr)),
.name_len = 3, .name_len = 3,
.value_ptr = @ptrCast([*:0]u8, @constCast("GET".ptr)), .value_ptr = @ptrCast(@constCast("GET".ptr)),
.value_len = 3, .value_len = 3,
}}); }});
var req = interface.Request{ var req = interface.Request{
.method = @ptrCast([*:0]u8, @constCast("GET".ptr)), .method = @ptrCast(@constCast("GET".ptr)),
.method_len = 3, .method_len = 3,
.content = @ptrCast([*:0]u8, @constCast("GET".ptr)), .content = @ptrCast(@constCast("GET".ptr)),
.content_len = 3, .content_len = 3,
.headers = headers.ptr, .headers = headers.ptr,
.headers_len = 1, .headers_len = 1,

View File

@ -171,7 +171,7 @@ fn getExecutor(requested_path: []const u8, headers: std.http.Headers) !*Executor
const serve_fn = std.c.dlsym(executor.library.?, SERVE_FN_NAME); const serve_fn = std.c.dlsym(executor.library.?, SERVE_FN_NAME);
if (serve_fn == null) return error.CouldNotLoadSymbolServe; if (serve_fn == null) return error.CouldNotLoadSymbolServe;
executor.serveFn = @ptrCast(serveFn, serve_fn.?); executor.serveFn = @ptrCast(serve_fn.?);
loadOptionalSymbols(executor); loadOptionalSymbols(executor);
return executor; return executor;
} }
@ -216,10 +216,10 @@ fn executorIsMatch(match_data: []const u8, requested_path: []const u8, headers:
/// different in each case so we can't combine those two. /// different in each case so we can't combine those two.
fn loadOptionalSymbols(executor: *Executor) void { fn loadOptionalSymbols(executor: *Executor) void {
if (std.c.dlsym(executor.library.?, "request_deinit")) |s| { if (std.c.dlsym(executor.library.?, "request_deinit")) |s| {
executor.requestDeinitFn = @ptrCast(requestDeinitFn, s); executor.requestDeinitFn = @ptrCast(s);
} }
if (std.c.dlsym(executor.library.?, "zigInit")) |s| { if (std.c.dlsym(executor.library.?, "zigInit")) |s| {
executor.zigInitFn = @ptrCast(zigInitFn, s); executor.zigInitFn = @ptrCast(s);
} }
} }
@ -255,7 +255,7 @@ fn executorChanged(watch: usize) void {
@panic("System unstable: Error after library open and cannot close"); @panic("System unstable: Error after library open and cannot close");
return; return;
} }
executor.serveFn = @ptrCast(serveFn, symbol); executor.serveFn = @ptrCast(symbol);
loadOptionalSymbols(executor); loadOptionalSymbols(executor);
} }
} }
@ -380,7 +380,7 @@ pub fn main() !void {
var server_thread_count = if (std.os.getenv("SERVER_THREAD_COUNT")) |count| var server_thread_count = if (std.os.getenv("SERVER_THREAD_COUNT")) |count|
try std.fmt.parseInt(usize, count, 10) try std.fmt.parseInt(usize, count, 10)
else switch (builtin.mode) { else switch (builtin.mode) {
.Debug => std.math.min(4, try std.Thread.getCpuCount()), .Debug => @min(4, try std.Thread.getCpuCount()),
else => try std.Thread.getCpuCount(), else => try std.Thread.getCpuCount(),
}; };
switch (builtin.mode) { switch (builtin.mode) {
@ -505,14 +505,14 @@ fn processRequest(allocator: *std.mem.Allocator, server: *std.http.Server, write
response_bytes = f.response; response_bytes = f.response;
res.transfer_encoding = .{ .content_length = response_bytes.len }; res.transfer_encoding = .{ .content_length = response_bytes.len };
try res.headers.append("connection", "close"); try res.headers.append("connection", "close");
try writer.print(" {d} ttfb {d:.3}ms", .{ @enumToInt(res.status), @intToFloat(f64, tm.read()) / std.time.ns_per_ms }); try writer.print(" {d} ttfb {d:.3}ms", .{ @intFromEnum(res.status), @as(f64, @floatFromInt(tm.read())) / std.time.ns_per_ms });
if (builtin.is_test) writeToTestBuffers(response_bytes, &res); if (builtin.is_test) writeToTestBuffers(response_bytes, &res);
try res.do(); try res.do();
_ = try res.writer().writeAll(response_bytes); _ = try res.writer().writeAll(response_bytes);
try res.finish(); try res.finish();
try writer.print(" {d} ttlb {d:.3}ms", .{ try writer.print(" {d} ttlb {d:.3}ms", .{
response_bytes.len, response_bytes.len,
@intToFloat(f64, tm.read()) / std.time.ns_per_ms, @as(f64, @floatFromInt(tm.read())) / std.time.ns_per_ms,
}); });
} }