chore: remove magic numbers

This commit is contained in:
2022-04-13 21:39:35 -03:00
parent ffbb31c767
commit dfe94fb931
6 changed files with 17 additions and 29 deletions

View File

@@ -12,9 +12,7 @@ pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
if (maybe_path) |path| {
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
const len = try file.getEndPos();
buf = try file.readToEndAlloc(alloc, len);
buf = try file.readToEndAlloc(alloc, try file.getEndPos());
}
return Self{

View File

@@ -1,13 +1,14 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ewram_size = 0x40000;
const Self = @This();
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, 0x40000);
const buf = try alloc.alloc(u8, ewram_size);
std.mem.set(u8, buf, 0);
return Self{

View File

@@ -1,13 +1,14 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const iwram_size = 0x8000;
const Self = @This();
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, 0x8000);
const buf = try alloc.alloc(u8, iwram_size);
std.mem.set(u8, buf, 0);
return Self{

View File

@@ -27,7 +27,7 @@ pub const Backup = struct {
flash: Flash,
pub fn init(alloc: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self {
const buf_len: usize = switch (kind) {
const buf_size: usize = switch (kind) {
.Sram => 0x8000, // 32K
.Flash => 0x10000, // 64K
.Flash1M => 0x20000, // 128K
@@ -35,7 +35,7 @@ pub const Backup = struct {
.None => 0,
};
const buf = try alloc.alloc(u8, buf_len);
const buf = try alloc.alloc(u8, buf_size);
std.mem.set(u8, buf, 0xFF);
var backup = Self{
@@ -74,17 +74,17 @@ pub const Backup = struct {
defer self.alloc.free(file_path);
const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{});
const len = try file.getEndPos();
const file_buf = try file.readToEndAlloc(self.alloc, len);
const file_buf = try file.readToEndAlloc(self.alloc, try file.getEndPos());
defer self.alloc.free(file_buf);
switch (self.kind) {
.Sram, .Flash, .Flash1M => {
if (self.buf.len == file_buf.len) {
std.mem.copy(u8, self.buf, file_buf);
log.info("Loaded Save from {s}", .{file_path});
return log.info("Loaded Save from {s}", .{file_path});
}
log.err("{s} is {} bytes, but we expected {} bytes", .{ file_path, file_buf.len, self.buf.len });
},
else => return SaveError.UnsupportedBackupKind,
}