Пример #1
0
 public void RentEmpty()
 {
     using (var memory = new RefTypeRentMemory <string?>(0)) {
         Assert.True(memory.AsSpan().IsEmpty);
         Assert.Equal(0, memory.Length);
     }
 }
Пример #2
0
 public void LargeRent(int length)
 {
     using (var memory = new RefTypeRentMemory <string?>(length)) {
         var span = memory.AsSpan();
         Assert.Equal(length, span.Length);
     }
 }
Пример #3
0
 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);
     });
 }
Пример #4
0
 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);
         }
     }
 }
Пример #5
0
        public void RentMulti()
        {
            const int Count = 100;

            using var list = new Disposables <RefTypeRentMemory <string?> >();
            for (int i = 0; i < Count; i++)
            {
                var mem = new RefTypeRentMemory <string?>(1000);
                list.Add(mem);
            }
            for (int i = 0; i < Count; i++)
            {
                Assert.Equal(1000, list[i].Length);
                Assert.Equal(1000, list[i].AsSpan().Length);
            }
        }
Пример #6
0
        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;
            }
        }
Пример #7
0
 internal RefTypeRentMemoryDebuggerTypeProxy(RefTypeRentMemory <T> entity)
 {
     _entity = entity;
 }