public static OwnedMemory <T> Create(int size) { if (!Pool.TryTake(out OwnedPooledArray <T> ownedPooledArray)) { ownedPooledArray = new OwnedPooledArray <T>(); } ownedPooledArray._array = BufferPool <T> .Rent(size, false); ownedPooledArray._disposed = false; ownedPooledArray._referenceCount = 0; return(ownedPooledArray); }
internal byte[] GetBlock() { var buffer = BufferPool <byte> .Rent(_blockSize); if (buffer.Length == _blockSize) { return(buffer); } BufferPool <byte> .Return(buffer, false); buffer = new byte[_blockSize]; return(buffer); }
internal static PrivateMemory <T> Create(int length, RetainableMemoryPool <T> pool) { var cpuId = Cpu.GetCurrentCoreId(); var alignedSize = (uint)BitUtil.FindNextPositivePowerOfTwo(Unsafe.SizeOf <T>()); if ((ulong)length * alignedSize > int.MaxValue) { ThrowHelper.ThrowArgumentOutOfRangeException(nameof(length)); } length = Math.Max(length, Settings.MIN_POOLED_BUFFER_LEN); var privateMemory = ObjectPool.Rent(cpuId); // Clear counter (with flags, PM does not have any flags). // We cannot tell if ObjectPool allocated a new one or took from pool // other then by checking if the counter is disposed, so we cannot require // that the counter is disposed. We only need that pooled object has the counter // in a disposed state so that no one accidentally uses the object while it is in the pool. privateMemory.CounterRef = 0; if (TypeHelper <T> .IsReferenceOrContainsReferences) { privateMemory._array = BufferPool <T> .Rent(length); privateMemory._offset = 0; ThrowHelper.DebugAssert(privateMemory._pointer == null); } else { privateMemory.AllocateBlittable((uint)(length * alignedSize), alignedSize, cpuId); ThrowHelper.DebugAssert(privateMemory._array == null); } privateMemory.PoolIndex = pool is null ? (byte)1 : pool.PoolIdx; privateMemory.Vec = privateMemory.GetVec().AsVec(); return(privateMemory); }
/// <summary> /// Create a <see cref="PrivateMemory{T}"/> from a <see cref="RetainableMemoryPool{T}"/>. /// </summary> internal static PrivateMemory <T> Create(int length, RetainableMemoryPool <T>?pool) { length = Math.Max(length, Settings.MIN_POOLED_BUFFER_LEN); var cpuId = Cpu.GetCurrentCoreId(); var privateMemory = ObjectPool.Rent(cpuId); ThrowHelper.DebugAssert(privateMemory.IsDisposed); // Clear counter (with flags, PM does not have any flags). // We cannot tell if ObjectPool allocated a new one or took from pool // other then by checking if the counter is disposed, so we cannot require // that the counter is disposed. We only need that pooled object has the counter // in a disposed state so that no one accidentally uses the object while it is in the pool. privateMemory.CounterRef = 0; if (TypeHelper <T> .IsReferenceOrContainsReferences) { var arr = BufferPool <T> .Rent(length); privateMemory._array = arr; privateMemory._length = arr.Length; privateMemory._offset = 0; ThrowHelper.DebugAssert(privateMemory._pointer == default); } else { privateMemory.AllocateBlittable(length, cpuId); ThrowHelper.DebugAssert(privateMemory._array == null); } privateMemory.PoolIndex = pool is null ? (byte)1 : pool.PoolIdx; privateMemory._memory = privateMemory.CreateMemory(); return(privateMemory); }
private RetainableMemory <T> CreateNew(int length) { if (_disposed) { ThrowDisposed <RetainableMemoryPool <T> >(); } if (_factory == null) { var am = ArrayMemory <T> .Create(BufferPool <T> .Rent(length), 0, length, false, _pin, this); // am._pool = this; return(Unsafe.As <RetainableMemory <T> >(am)); } var buffer = _factory.Invoke(this, length); if (IsRentAlwaysClean) { buffer.GetSpan().Clear(); } return(buffer); }
internal byte[] GetBlock() { return(BufferPool <byte> .Rent(_blockSize, true)); }