refactoring: cleanup free/deinit/destroy

This commit is contained in:
Patrick MARIE 2024-08-28 12:32:37 +02:00
parent 16640b6d07
commit 732e0861b5
7 changed files with 34 additions and 44 deletions

View File

@ -23,19 +23,18 @@ pub const Chunk = struct {
.capacity = 0, .capacity = 0,
.code = &.{}, .code = &.{},
.lines = &.{}, .lines = &.{},
.constants = ValueArray.new(), .constants = ValueArray.new(allocator),
.allocator = allocator, .allocator = allocator,
}; };
} }
pub fn init(self: *Chunk) !void { pub fn destroy(self: *Chunk) void {
self.deinit(self.allocator); self.constants.destroy();
self.count = 0; if (self.capacity > 0) {
self.capacity = 0; self.allocator.free(self.code);
self.code = &.{}; self.allocator.free(self.lines);
self.lines = &.{}; }
self.constants = ValueArray.new();
} }
pub fn write(self: *Chunk, byte: u8, line: usize) !void { pub fn write(self: *Chunk, byte: u8, line: usize) !void {
@ -109,17 +108,8 @@ pub const Chunk = struct {
} }
} }
pub fn deinit(self: *Chunk) void {
self.constants.free(self.allocator);
if (self.capacity > 0) {
self.allocator.free(self.code);
self.allocator.free(self.lines);
}
}
pub fn add_constant(self: *Chunk, value: Value) !usize { pub fn add_constant(self: *Chunk, value: Value) !usize {
try self.constants.write(self.allocator, value); try self.constants.write(value);
return self.constants.count - 1; return self.constants.count - 1;
} }
}; };

View File

@ -746,8 +746,8 @@ const Local = struct {
depth: ?usize, depth: ?usize,
}; };
pub fn compile(allocator: Allocator, vm: *VM, contents: []const u8) !?*Obj.Function { pub fn compile(vm: *VM, contents: []const u8) !?*Obj.Function {
var compiler = Compiler.new(allocator, FunctionType.Script); var compiler = Compiler.new(vm.allocator, FunctionType.Script);
var scanner = Scanner.init(contents); var scanner = Scanner.init(contents);
var parser = Parser.new(vm, &compiler, &scanner); var parser = Parser.new(vm, &compiler, &scanner);

View File

@ -63,7 +63,7 @@ pub fn main() !void {
defer std.process.argsFree(allocator, args); defer std.process.argsFree(allocator, args);
var vm = VM.new(allocator); var vm = VM.new(allocator);
defer vm.free(); defer vm.destroy();
if (args.len == 1) { if (args.len == 1) {
try repl(allocator, &vm); try repl(allocator, &vm);

View File

@ -36,9 +36,8 @@ pub const Obj = struct {
} }
pub fn destroy(self: *String) void { pub fn destroy(self: *String) void {
const allocator = self.obj.allocator; self.obj.allocator.free(self.chars);
allocator.free(self.chars); self.obj.allocator.destroy(self);
allocator.destroy(self);
} }
}; };
@ -64,9 +63,8 @@ pub const Obj = struct {
} }
pub fn destroy(self: *Function) void { pub fn destroy(self: *Function) void {
const allocator = self.obj.allocator; self.chunk.destroy();
self.chunk.deinit(); self.obj.allocator.destroy(self);
allocator.destroy(self);
} }
}; };

View File

@ -30,7 +30,7 @@ pub const Table = struct {
}; };
} }
pub fn deinit(self: *Table) void { pub fn destroy(self: *Table) void {
if (self.capacity == 0) { if (self.capacity == 0) {
return; return;
} }
@ -202,7 +202,7 @@ test "initialize an hash table" {
const allocator = std.testing.allocator; const allocator = std.testing.allocator;
var table = Table.new(allocator); var table = Table.new(allocator);
defer table.deinit(); defer table.destroy();
try std.testing.expectEqual(0, table.count); try std.testing.expectEqual(0, table.count);
try std.testing.expectEqual(0, table.capacity); try std.testing.expectEqual(0, table.capacity);
} }
@ -214,7 +214,7 @@ test "adding values" {
defer key.destroy(); defer key.destroy();
var table = Table.new(allocator); var table = Table.new(allocator);
defer table.deinit(); defer table.destroy();
var res = table.set(key, Value.nil_val()); var res = table.set(key, Value.nil_val());
try std.testing.expectEqual(true, res); try std.testing.expectEqual(true, res);
@ -234,7 +234,7 @@ test "adding tables" {
defer key.destroy(); defer key.destroy();
var table = Table.new(allocator); var table = Table.new(allocator);
defer table.deinit(); defer table.destroy();
const res = table.set(key, Value.nil_val()); const res = table.set(key, Value.nil_val());
try std.testing.expectEqual(true, res); try std.testing.expectEqual(true, res);
@ -242,7 +242,7 @@ test "adding tables" {
try std.testing.expectEqual(1, table.count); try std.testing.expectEqual(1, table.count);
var table2 = Table.new(allocator); var table2 = Table.new(allocator);
defer table2.deinit(); defer table2.destroy();
try std.testing.expectEqual(0, table2.capacity); try std.testing.expectEqual(0, table2.capacity);
try std.testing.expectEqual(0, table2.count); try std.testing.expectEqual(0, table2.count);
@ -259,7 +259,7 @@ test "deleting from table" {
defer key.destroy(); defer key.destroy();
var table = Table.new(allocator); var table = Table.new(allocator);
defer table.deinit(); defer table.destroy();
var res = table.set(key, Value.nil_val()); var res = table.set(key, Value.nil_val());
try std.testing.expectEqual(true, res); try std.testing.expectEqual(true, res);
@ -279,7 +279,7 @@ test "find" {
defer key.destroy(); defer key.destroy();
var table = Table.new(allocator); var table = Table.new(allocator);
defer table.deinit(); defer table.destroy();
const value = Value.number_val(42.0); const value = Value.number_val(42.0);

View File

@ -141,32 +141,34 @@ pub const Value = struct {
}; };
pub const ValueArray = struct { pub const ValueArray = struct {
allocator: Allocator,
capacity: usize, capacity: usize,
count: usize, count: usize,
values: []Value, values: []Value,
pub fn new() ValueArray { pub fn new(allocator: Allocator) ValueArray {
return ValueArray{ return ValueArray{
.allocator = allocator,
.capacity = 0, .capacity = 0,
.count = 0, .count = 0,
.values = &.{}, .values = &.{},
}; };
} }
pub fn write(self: *ValueArray, allocator: Allocator, value: Value) !void { pub fn write(self: *ValueArray, value: Value) !void {
if (self.capacity < self.count + 1) { if (self.capacity < self.count + 1) {
const old_capacity = self.capacity; const old_capacity = self.capacity;
self.capacity = utils.grow_capacity(old_capacity); self.capacity = utils.grow_capacity(old_capacity);
self.values = try allocator.realloc(self.values, self.capacity); self.values = try self.allocator.realloc(self.values, self.capacity);
} }
self.values[self.count] = value; self.values[self.count] = value;
self.count += 1; self.count += 1;
} }
pub fn free(self: *ValueArray, allocator: Allocator) void { pub fn destroy(self: *ValueArray) void {
if (self.capacity > 0) { if (self.capacity > 0) {
allocator.free(self.values); self.allocator.free(self.values);
} }
} }
}; };

View File

@ -53,13 +53,13 @@ pub const VM = struct {
}; };
} }
pub fn free(self: *VM) void { pub fn destroy(self: *VM) void {
if (constants.DEBUG_PRINT_INTERNAL_STRINGS) { if (constants.DEBUG_PRINT_INTERNAL_STRINGS) {
self.strings.dump(); self.strings.dump();
} }
self.strings.deinit(); self.strings.destroy();
self.globals.deinit(); self.globals.destroy();
self.clean_references(); self.clean_references();
self.references.deinit(); self.references.deinit();
} }
@ -74,9 +74,9 @@ pub const VM = struct {
pub fn interpret(self: *VM, allocator: Allocator, content: []const u8) !InterpretResult { pub fn interpret(self: *VM, allocator: Allocator, content: []const u8) !InterpretResult {
var chunk = Chunk.new(allocator); var chunk = Chunk.new(allocator);
defer chunk.deinit(); defer chunk.destroy();
const function = try compile(allocator, self, content); const function = try compile(self, content);
if (function == null) { if (function == null) {
return InterpretResult.COMPILE_ERROR; return InterpretResult.COMPILE_ERROR;
} }