better debug info if inc on invalid pointer

This commit is contained in:
Folkert 2023-05-14 12:59:43 +02:00
parent c19ad4ecfd
commit dd8739b847
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C

View file

@ -152,22 +152,28 @@ const RC_TYPE = Refcount.normal;
pub fn increfRcPtrC(ptr_to_refcount: *isize, amount: isize) callconv(.C) void {
if (RC_TYPE == Refcount.none) return;
if (DEBUG_INCDEC and builtin.target.cpu.arch != .wasm32) {
std.debug.print("| increment {*}: ", .{ptr_to_refcount});
}
// Ensure that the refcount is not whole program lifetime.
if (ptr_to_refcount.* != REFCOUNT_MAX_ISIZE) {
// Note: we assume that a refcount will never overflow.
// As such, we do not need to cap incrementing.
switch (RC_TYPE) {
Refcount.normal => {
const old = @bitCast(usize, ptr_to_refcount.*);
ptr_to_refcount.* += amount;
const new = @bitCast(usize, ptr_to_refcount.*);
if (DEBUG_INCDEC and builtin.target.cpu.arch != .wasm32) {
const old = @bitCast(usize, ptr_to_refcount.*);
const new = old + @intCast(usize, amount);
const oldH = old - REFCOUNT_ONE + 1;
const newH = new - REFCOUNT_ONE + 1;
std.debug.print("| increment {*}: {} + {} = {}!\n", .{ ptr_to_refcount, oldH, amount, newH });
std.debug.print("{} + {} = {}!\n", .{ oldH, amount, newH });
}
ptr_to_refcount.* += amount;
},
Refcount.atomic => {
_ = @atomicRmw(isize, ptr_to_refcount, std.builtin.AtomicRmwOp.Add, amount, Monotonic);