refactoring: cleanup free/deinit/destroy

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

View File

@ -141,32 +141,34 @@ pub const Value = struct {
};
pub const ValueArray = struct {
allocator: Allocator,
capacity: usize,
count: usize,
values: []Value,
pub fn new() ValueArray {
pub fn new(allocator: Allocator) ValueArray {
return ValueArray{
.allocator = allocator,
.capacity = 0,
.count = 0,
.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) {
const old_capacity = self.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.count += 1;
}
pub fn free(self: *ValueArray, allocator: Allocator) void {
pub fn destroy(self: *ValueArray) void {
if (self.capacity > 0) {
allocator.free(self.values);
self.allocator.free(self.values);
}
}
};