add variable to enable atomic refcounts for testing

This commit is contained in:
Brendan Hansknecht 2022-03-31 22:18:25 -07:00
parent 51b360b578
commit a75ecb2883

View file

@ -120,11 +120,17 @@ pub const IntWidth = enum(u8) {
I128 = 9,
};
const USE_ATOMICS = false;
pub fn increfC(ptr_to_refcount: *isize, amount: isize) callconv(.C) void {
var refcount = ptr_to_refcount.*;
var masked_amount = if (refcount == REFCOUNT_MAX_ISIZE) 0 else amount;
var masked_amount = if (refcount < REFCOUNT_MAX_ISIZE) amount else 0;
if (USE_ATOMICS) {
var last = @atomicRmw(isize, ptr_to_refcount, std.builtin.AtomicRmwOp.Add, masked_amount, std.builtin.AtomicOrder.Monotonic);
} else {
ptr_to_refcount.* = refcount + masked_amount;
}
}
pub fn decrefC(
bytes_or_null: ?[*]isize,
@ -169,15 +175,22 @@ inline fn decref_ptr_to_refcount(
refcount_ptr: [*]isize,
alignment: u32,
) void {
const refcount: isize = refcount_ptr[0];
const extra_bytes = std.math.max(alignment, @sizeOf(usize));
if (USE_ATOMICS) {
var amount : isize = if (refcount_ptr[0] < REFCOUNT_MAX_ISIZE) 1 else 0;
var last = @atomicRmw(isize, &refcount_ptr[0], std.builtin.AtomicRmwOp.Sub, amount, std.builtin.AtomicOrder.Monotonic);
if (last == REFCOUNT_ONE_ISIZE) {
dealloc(@ptrCast([*]u8, refcount_ptr) - (extra_bytes - @sizeOf(usize)), alignment);
}
} else {
const refcount: isize = refcount_ptr[0];
if (refcount == REFCOUNT_ONE_ISIZE) {
dealloc(@ptrCast([*]u8, refcount_ptr) - (extra_bytes - @sizeOf(usize)), alignment);
} else if (refcount < 0) {
} else if (refcount < REFCOUNT_MAX_ISIZE) {
refcount_ptr[0] = refcount - 1;
}
}
}
pub fn allocateWithRefcount(
data_bytes: usize,