fix infra: saturating memory tracker, check build imports, buffer growth
test
This commit is contained in:
@@ -39,6 +39,17 @@ pub const TrackingAllocator = struct {
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Atomically subtract from allocated_bytes, saturating at 0 to prevent underflow.
|
||||
/// Uses fetchSub (single atomic op) in the common path for maximum throughput.
|
||||
/// Only corrects on underflow, which is rare (forced shutdown freeing untracked memory).
|
||||
fn saturatingSub(self: *Self, amount: u64) void {
|
||||
const prev = self.allocated_bytes.fetchSub(amount, .release);
|
||||
if (prev < amount) {
|
||||
// Underflowed — correct back to 0
|
||||
_ = self.allocated_bytes.fetchAdd(amount - prev, .release);
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize a tracking allocator with a backing allocator and memory limit
|
||||
pub fn init(backing_allocator: Allocator, max_bytes: u64) Self {
|
||||
return .{
|
||||
@@ -99,7 +110,7 @@ pub const TrackingAllocator = struct {
|
||||
const result = self.backing_allocator.rawRemap(buf, buf_align, new_len, ret_addr);
|
||||
if (result == null) {
|
||||
// Failed - release reserved memory
|
||||
_ = self.allocated_bytes.fetchSub(delta, .release);
|
||||
self.saturatingSub(delta);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -121,7 +132,7 @@ pub const TrackingAllocator = struct {
|
||||
const result = self.backing_allocator.rawRemap(buf, buf_align, new_len, ret_addr);
|
||||
if (result != null) {
|
||||
const delta = old_size - new_len;
|
||||
_ = self.allocated_bytes.fetchSub(delta, .release);
|
||||
self.saturatingSub(delta);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
@@ -217,7 +228,7 @@ pub const TrackingAllocator = struct {
|
||||
|
||||
if (result == null) {
|
||||
// Allocation failed - release the reserved memory
|
||||
_ = self.allocated_bytes.fetchSub(size, .release);
|
||||
self.saturatingSub(size);
|
||||
std.log.debug("CRIT-8: Backing allocator failed for {d} bytes", .{size});
|
||||
return null;
|
||||
}
|
||||
@@ -258,7 +269,7 @@ pub const TrackingAllocator = struct {
|
||||
// Try the actual resize
|
||||
if (!self.backing_allocator.rawResize(buf, buf_align, new_len, ret_addr)) {
|
||||
// Failed - release reserved memory
|
||||
_ = self.allocated_bytes.fetchSub(delta, .release);
|
||||
self.saturatingSub(delta);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -280,7 +291,7 @@ pub const TrackingAllocator = struct {
|
||||
const delta = old_size - new_len;
|
||||
|
||||
if (self.backing_allocator.rawResize(buf, buf_align, new_len, ret_addr)) {
|
||||
_ = self.allocated_bytes.fetchSub(delta, .release);
|
||||
self.saturatingSub(delta);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -298,7 +309,7 @@ pub const TrackingAllocator = struct {
|
||||
self.backing_allocator.rawFree(buf, buf_align, ret_addr);
|
||||
|
||||
// Release tracked memory
|
||||
_ = self.allocated_bytes.fetchSub(size, .release);
|
||||
self.saturatingSub(size);
|
||||
_ = self.free_count.fetchAdd(1, .monotonic);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user