refactoring: cleanup free/deinit/destroy
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user