Add Leak Detection to Ona Heap Allocator #15

Merged
kayomn merged 12 commits from ona-allocator-safety-tracker into main 2023-06-04 16:07:47 +02:00
2 changed files with 21 additions and 11 deletions
Showing only changes of commit 5500de299b - Show all commits

View File

@ -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;
} }

View File

@ -183,6 +183,7 @@ pub fn deinit(self: *Self) void {
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());
kayomn marked this conversation as resolved Outdated

May be worth adding an assert to check that the VM environment heap is empty by now before calling deinit.

May be worth adding an assert to check that the VM environment heap is empty by now before calling deinit.
self.heap.deinit(self.allocator); self.heap.deinit(self.allocator);
} }