chore: remove all memory leaks

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-01-03 20:08:55 -06:00
parent 8257a3899a
commit 3aa680ab8c
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 { pub fn read32(self: *const @This(), addr: u32) u32 {
return switch (addr) { return switch (addr) {
// General Internal Memory // General Internal Memory

View File

@ -4,6 +4,7 @@ const Allocator = std.mem.Allocator;
pub const Bios = struct { pub const Bios = struct {
buf: []u8, buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !@This() { pub fn init(alloc: Allocator, path: []const u8) !@This() {
const file = try std.fs.cwd().openFile(path, .{ .read = true }); const file = try std.fs.cwd().openFile(path, .{ .read = true });
@ -13,9 +14,14 @@ pub const Bios = struct {
return @This(){ return @This(){
.buf = try file.readToEndAlloc(alloc, len), .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 { pub inline fn get32(self: *const @This(), idx: usize) u32 {
std.debug.panic("[BIOS] TODO: BIOS is not implemented", .{}); 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])); 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 { pub const GamePak = struct {
buf: []u8, buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, path: []const u8) !@This() { pub fn init(alloc: Allocator, path: []const u8) !@This() {
const file = try std.fs.cwd().openFile(path, .{ .read = true }); const file = try std.fs.cwd().openFile(path, .{ .read = true });
@ -13,9 +14,14 @@ pub const GamePak = struct {
return @This(){ return @This(){
.buf = try file.readToEndAlloc(alloc, len), .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 { 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])); 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 { pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator(); const alloc = gpa.allocator();
defer _ = gpa.deinit(); defer std.debug.assert(!gpa.deinit());
const args = try std.process.argsAlloc(alloc); const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args); defer std.process.argsFree(alloc, args);
@ -24,7 +24,11 @@ pub fn main() anyerror!void {
} }
var bus = try Bus.init(alloc, zba_args[0]); var bus = try Bus.init(alloc, zba_args[0]);
defer bus.deinit();
var scheduler = Scheduler.init(alloc); var scheduler = Scheduler.init(alloc);
defer scheduler.deinit();
var cpu = Arm7tdmi.init(&scheduler, &bus); var cpu = Arm7tdmi.init(&scheduler, &bus);
cpu.skipBios(); cpu.skipBios();

View File

@ -10,17 +10,27 @@ pub const Ppu = struct {
.vram = try Vram.init(alloc), .vram = try Vram.init(alloc),
}; };
} }
pub fn deinit(self: *@This()) void {
self.vram.deinit();
}
}; };
const Vram = struct { const Vram = struct {
buf: []u8, buf: []u8,
alloc: Allocator,
fn init(alloc: Allocator) !@This() { fn init(alloc: Allocator) !@This() {
return @This(){ return @This(){
.buf = try alloc.alloc(u8, 0x18000), .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 { 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])); 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; return scheduler;
} }
pub fn deinit(self: *@This()) void {
self.queue.deinit();
}
pub fn handleEvent(self: *@This(), _: *Arm7tdmi, _: *Bus) void { pub fn handleEvent(self: *@This(), _: *Arm7tdmi, _: *Bus) void {
const should_handle = if (self.queue.peek()) |e| self.tick >= e.tick else false; const should_handle = if (self.queue.peek()) |e| self.tick >= e.tick else false;