Compare commits
2 Commits
155645a308
...
3794259949
Author | SHA1 | Date | |
---|---|---|---|
3794259949 | |||
5500de299b |
@ -52,17 +52,6 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
|
|||||||
entry.value = value;
|
entry.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
|
||||||
/// Fetches the value referenced by `index` in `self`, returning it.
|
|
||||||
///
|
|
||||||
pub fn fetch(self: *Self, index: Index) Value {
|
|
||||||
const entry = &self.table[index];
|
|
||||||
|
|
||||||
debug.assert(entry.* == .value);
|
|
||||||
|
|
||||||
return entry.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Deinitializes `self` and sets it to an invalid state, freeing all memory allocated by `allocator`.
|
/// Deinitializes `self` and sets it to an invalid state, freeing all memory allocated by `allocator`.
|
||||||
///
|
///
|
||||||
@ -81,6 +70,17 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
|
|||||||
self.free_index = 0;
|
self.free_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Fetches the value referenced by `index` in `self`, returning it.
|
||||||
|
///
|
||||||
|
pub fn fetch(self: *Self, index: Index) Value {
|
||||||
|
const entry = &self.table[index];
|
||||||
|
|
||||||
|
debug.assert(entry.* == .value);
|
||||||
|
|
||||||
|
return entry.value;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Attempts to grow the internal buffer of `self` by `growth_amount` using `allocator`.
|
/// Attempts to grow the internal buffer of `self` by `growth_amount` using `allocator`.
|
||||||
///
|
///
|
||||||
@ -147,12 +147,20 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
|
|||||||
|
|
||||||
debug.assert(entry.* == .free_index);
|
debug.assert(entry.* == .free_index);
|
||||||
|
|
||||||
|
self.count += 1;
|
||||||
self.free_index = entry.free_index;
|
self.free_index = entry.free_index;
|
||||||
entry.* = .{.value = value};
|
entry.* = .{.value = value};
|
||||||
|
|
||||||
return entry_index;
|
return entry_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Returns `true` if `self` contains no values, otherwise `false`.
|
||||||
|
///
|
||||||
|
pub fn is_empty(self: Self) bool {
|
||||||
|
return self.count == 0;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Removes the value referenced by `index` from `self`.
|
/// Removes the value referenced by `index` from `self`.
|
||||||
///
|
///
|
||||||
@ -161,6 +169,7 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
|
|||||||
|
|
||||||
debug.assert(entry.* == .value);
|
debug.assert(entry.* == .value);
|
||||||
|
|
||||||
|
self.count -= 1;
|
||||||
entry.* = .{.free_index = self.free_index};
|
entry.* = .{.free_index = self.free_index};
|
||||||
self.free_index = index;
|
self.free_index = index;
|
||||||
}
|
}
|
||||||
|
@ -170,46 +170,26 @@ pub fn check(self: *Self, condition: bool, failure_message: []const u8) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
self.discard(.{.object = self.global_object});
|
self.object_release(self.global_object);
|
||||||
|
|
||||||
{
|
{
|
||||||
var interned_iterable = InternTable.Iterable{.hashed_map = &self.interned};
|
var interned_iterable = InternTable.Iterable{.hashed_map = &self.interned};
|
||||||
|
|
||||||
while (interned_iterable.next()) |entry| {
|
while (interned_iterable.next()) |entry| {
|
||||||
self.discard(.{.object = entry.value});
|
self.object_release(entry.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.interned.deinit(self.allocator);
|
self.interned.deinit(self.allocator);
|
||||||
self.values.deinit(self.allocator);
|
self.values.deinit(self.allocator);
|
||||||
self.calls.deinit(self.allocator);
|
self.calls.deinit(self.allocator);
|
||||||
|
coral.debug.assert(self.heap.is_empty());
|
||||||
self.heap.deinit(self.allocator);
|
self.heap.deinit(self.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn discard(self: *Self, val: types.Val) void {
|
pub fn discard(self: *Self, val: types.Val) void {
|
||||||
switch (val) {
|
switch (val) {
|
||||||
.object => |object| {
|
.object => |object| self.object_release(object),
|
||||||
var data = self.heap.fetch(object);
|
|
||||||
|
|
||||||
coral.debug.assert(data.ref_count != 0);
|
|
||||||
|
|
||||||
data.ref_count -= 1;
|
|
||||||
|
|
||||||
if (data.ref_count == 0) {
|
|
||||||
data.state.info.deinitializer(.{
|
|
||||||
.env = self,
|
|
||||||
.obj = val.as_ref(),
|
|
||||||
});
|
|
||||||
|
|
||||||
// TODO: Free individual key-value pairs of fields
|
|
||||||
data.state.fields.deinit(self.allocator);
|
|
||||||
coral.io.deallocate(self.allocator, data.state.userdata);
|
|
||||||
self.heap.remove(object);
|
|
||||||
} else {
|
|
||||||
self.heap.assign(object, data);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -416,6 +396,28 @@ pub fn new_string(self: *Self, data: []const u8) coral.io.AllocationError!types.
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn object_release(self: *Self, object: types.Object) void {
|
||||||
|
var data = self.heap.fetch(object);
|
||||||
|
|
||||||
|
coral.debug.assert(data.ref_count != 0);
|
||||||
|
|
||||||
|
data.ref_count -= 1;
|
||||||
|
|
||||||
|
if (data.ref_count == 0) {
|
||||||
|
data.state.info.deinitializer(.{
|
||||||
|
.env = self,
|
||||||
|
.obj = .{.object = object},
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Free individual key-value pairs of fields
|
||||||
|
data.state.fields.deinit(self.allocator);
|
||||||
|
coral.io.deallocate(self.allocator, data.state.userdata);
|
||||||
|
self.heap.remove(object);
|
||||||
|
} else {
|
||||||
|
self.heap.assign(object, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_global(self: *Self, global_name: []const u8, value: types.Ref) coral.io.AllocationError!void {
|
pub fn set_global(self: *Self, global_name: []const u8, value: types.Ref) coral.io.AllocationError!void {
|
||||||
try self.globals.assign(self.allocator, global_name, value);
|
try self.globals.assign(self.allocator, global_name, value);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user