mirror of
https://github.com/rdmsr/zag.git
synced 2026-10-31 07:18:22 +00:00
all: refactor coding style
This commit is contained in:
+59
-56
@@ -16,7 +16,11 @@ const Entry = struct {
|
||||
},
|
||||
};
|
||||
|
||||
pub fn Tunable(comptime T: type, comptime default: T, comptime name: []const u8) type {
|
||||
pub fn Tunable(
|
||||
comptime T: type,
|
||||
comptime default: T,
|
||||
comptime name: []const u8,
|
||||
) type {
|
||||
return struct {
|
||||
var storage: T = default;
|
||||
|
||||
@@ -42,66 +46,65 @@ pub fn Tunable(comptime T: type, comptime default: T, comptime name: []const u8)
|
||||
};
|
||||
}
|
||||
|
||||
fn store_int(comptime T: type, addr: *anyopaque, value: anytype) bool {
|
||||
const ptr: *T = @ptrCast(@alignCast(addr));
|
||||
ptr.* = std.math.cast(T, value) orelse return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
fn apply_num(
|
||||
comptime ValueT: type,
|
||||
cmdline: []const u8,
|
||||
entry: *const Entry,
|
||||
info: std.builtin.Type.Int,
|
||||
) void {
|
||||
const value = rtl.cmdline.get_number(ValueT, cmdline, entry.name) catch |e| {
|
||||
if (e == error.Format) {
|
||||
std.log.warn(
|
||||
"Invalid number value for '{s}', falling back.",
|
||||
.{entry.name},
|
||||
);
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
const sign = @typeInfo(ValueT).int.signedness;
|
||||
|
||||
_ = switch (info.bits) {
|
||||
8 => store_int(std.meta.Int(sign, 8), entry.address, value),
|
||||
16 => store_int(std.meta.Int(sign, 16), entry.address, value),
|
||||
32 => store_int(std.meta.Int(sign, 32), entry.address, value),
|
||||
64 => store_int(std.meta.Int(sign, 64), entry.address, value),
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
fn apply_bool(cmdline: []const u8, entry: *const Entry) void {
|
||||
const value = rtl.cmdline.get_string(cmdline, entry.name) orelse return;
|
||||
const ptr: *bool = @ptrCast(@alignCast(entry.address));
|
||||
|
||||
if (std.mem.eql(u8, value, "true")) {
|
||||
ptr.* = true;
|
||||
} else if (std.mem.eql(u8, value, "false")) {
|
||||
ptr.* = false;
|
||||
} else {
|
||||
std.log.warn(
|
||||
"Invalid cmdline boolean value for tunable '{s}'",
|
||||
.{entry.name},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init() void {
|
||||
const cmdline = r.boot_info.cmdline orelse return;
|
||||
|
||||
const elems = set.elems();
|
||||
|
||||
for (elems) |entry| {
|
||||
for (set.elems()) |entry| {
|
||||
switch (entry.type) {
|
||||
.num => |info| {
|
||||
const a = entry.address;
|
||||
|
||||
switch (info.signedness) {
|
||||
.unsigned => {
|
||||
const value = rtl.cmdline.get_number(u64, cmdline, entry.name) catch |e| {
|
||||
if (e == error.Format) {
|
||||
std.log.warn("Invalid number value for '{s}', falling back.", .{entry.name});
|
||||
}
|
||||
continue;
|
||||
};
|
||||
|
||||
switch (info.bits) {
|
||||
8 => (@as(*u8, @ptrCast(@alignCast(a)))).* = std.math.cast(u8, value) orelse continue,
|
||||
16 => (@as(*u16, @ptrCast(@alignCast(a)))).* = std.math.cast(u16, value) orelse continue,
|
||||
32 => (@as(*u32, @ptrCast(@alignCast(a)))).* = std.math.cast(u32, value) orelse continue,
|
||||
64 => (@as(*u64, @ptrCast(@alignCast(a)))).* = value,
|
||||
else => unreachable,
|
||||
}
|
||||
},
|
||||
|
||||
.signed => {
|
||||
const value = rtl.cmdline.get_number(i64, cmdline, entry.name) catch |e| {
|
||||
if (e == error.Format) {
|
||||
std.log.warn("Invalid number value for '{s}', falling back.", .{entry.name});
|
||||
}
|
||||
|
||||
continue;
|
||||
};
|
||||
|
||||
switch (info.bits) {
|
||||
8 => (@as(*i8, @ptrCast(@alignCast(a)))).* = std.math.cast(i8, value) orelse continue,
|
||||
16 => (@as(*i16, @ptrCast(@alignCast(a)))).* = std.math.cast(i16, value) orelse continue,
|
||||
32 => (@as(*i32, @ptrCast(@alignCast(a)))).* = std.math.cast(i32, value) orelse continue,
|
||||
64 => (@as(*i64, @ptrCast(@alignCast(a)))).* = value,
|
||||
else => unreachable,
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
.bool => {
|
||||
const value = rtl.cmdline.get_string(cmdline, entry.name) orelse continue;
|
||||
const ptr: *bool = @ptrCast(@alignCast(entry.address));
|
||||
|
||||
if (std.mem.eql(u8, value, "true")) {
|
||||
ptr.* = true;
|
||||
} else if (std.mem.eql(u8, value, "false")) {
|
||||
ptr.* = false;
|
||||
} else {
|
||||
std.log.warn("Invalid cmdline boolean value for tunable '{s}'", .{entry.name});
|
||||
}
|
||||
.num => |info| switch (info.signedness) {
|
||||
.unsigned => apply_num(u64, cmdline, entry, info),
|
||||
.signed => apply_num(i64, cmdline, entry, info),
|
||||
},
|
||||
.bool => apply_bool(cmdline, entry),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user