chore: remove all memory leaks

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:11:47 -03:00
parent 5c5179a553
commit da7300a78c
6 changed files with 37 additions and 1 deletions

View File

@ -22,6 +22,12 @@ pub const Bus = struct {
};
}
pub fn deinit(self: *@This()) void {
self.pak.deinit();
self.bios.deinit();
self.ppu.deinit();
}
pub fn read32(self: *const @This(), addr: u32) u32 {
return switch (addr) {
// General Internal Memory

View File

@ -4,6 +4,7 @@ const Allocator = std.mem.Allocator;
pub const Bios = struct {
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !@This() {
const file = try std.fs.cwd().openFile(path, .{ .read = true });
@ -13,9 +14,14 @@ pub const Bios = struct {
return @This(){
.buf = try file.readToEndAlloc(alloc, len),
.alloc = alloc,
};
}
pub fn deinit(self: *@This()) void {
self.alloc.free(self.buf);
}
pub inline fn get32(self: *const @This(), idx: usize) u32 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{});
return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx]));

View File

@ -4,6 +4,7 @@ const Allocator = std.mem.Allocator;
pub const GamePak = struct {
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !@This() {
const file = try std.fs.cwd().openFile(path, .{ .read = true });
@ -13,9 +14,14 @@ pub const GamePak = struct {
return @This(){
.buf = try file.readToEndAlloc(alloc, len),
.alloc = alloc,
};
}
pub fn deinit(self: *@This()) void {
self.alloc.free(self.buf);
}
pub inline fn get32(self: *const @This(), idx: usize) u32 {
return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx]));
}

View File

@ -8,7 +8,7 @@ const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
defer _ = gpa.deinit();
defer std.debug.assert(!gpa.deinit());
const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args);
@ -24,7 +24,11 @@ pub fn main() anyerror!void {
}
var bus = try Bus.init(alloc, zba_args[0]);
defer bus.deinit();
var scheduler = Scheduler.init(alloc);
defer scheduler.deinit();
var cpu = Arm7tdmi.init(&scheduler, &bus);
cpu.skipBios();

View File

@ -10,17 +10,27 @@ pub const Ppu = struct {
.vram = try Vram.init(alloc),
};
}
pub fn deinit(self: *@This()) void {
self.vram.deinit();
}
};
const Vram = struct {
buf: []u8,
alloc: Allocator,
fn init(alloc: Allocator) !@This() {
return @This(){
.buf = try alloc.alloc(u8, 0x18000),
.alloc = alloc,
};
}
fn deinit(self: *@This()) void {
self.alloc.free(self.buf);
}
pub inline fn get32(self: *const @This(), idx: usize) u32 {
return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx]));
}

View File

@ -21,6 +21,10 @@ pub const Scheduler = struct {
return scheduler;
}
pub fn deinit(self: *@This()) void {
self.queue.deinit();
}
pub fn handleEvent(self: *@This(), _: *Arm7tdmi, _: *Bus) void {
const should_handle = if (self.queue.peek()) |e| self.tick >= e.tick else false;