public void LargeRent(int length) { using (var memory = new RefTypeRentMemory <string?>(length)) { var span = memory.AsSpan(); Assert.Equal(length, span.Length); } }
public void RentEmpty() { using (var memory = new RefTypeRentMemory <string?>(0)) { Assert.True(memory.AsSpan().IsEmpty); Assert.Equal(0, memory.Length); } }
public void RentMultiParallel() { Parallel.For(0, 100, i => { using var mem = new RefTypeRentMemory <string?>(1000); Assert.Equal(1000, mem.Length); Assert.Equal(1000, mem.AsSpan().Length); }); }
public void Rent(int length) { using (var memory = new RefTypeRentMemory <string?>(length)) { var span = memory.AsSpan(); Assert.Equal(length, span.Length); foreach (var item in span) { Assert.Null(item); } } }
public void Raise(T arg) { // When _count == 0, there is no need to perform exclusive locking. if (_count == 0) { return; } _lock.Enter(); // ---- enter // Get _count after exclusive locking. var count = _count; if (count == 1) { var action = SafeCast.NotNullAs <Action <T> >(_actions); _lock.Exit(); // ---- exit action.Invoke(arg); return; } else { using var mem = new RefTypeRentMemory <object?>(count); Span <object?> memSpan; try { var actions = SafeCast.NotNullAs <object?[]>(_actions).AsSpan(0, count); memSpan = mem.AsSpan(); actions.CopyTo(memSpan); } finally { _lock.Exit(); // ---- exit } foreach (var action in memSpan) { SafeCast.NotNullAs <Action <T> >(action).Invoke(arg); } return; } }