Пример #1
0
 /**
  * Try to allocate a small buffer out of the cache. Returns {@code true} if successful {@code false} otherwise
  */
 internal bool AllocateSmall(PoolArena <T> area, PooledByteBuffer <T> buf, int reqCapacity, int normCapacity) =>
 this.Allocate(this.CacheForSmall(area, normCapacity), buf, reqCapacity);
Пример #2
0
        // TODO: Test if adding padding helps under contention
        //private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;

        internal PoolThreadCache(PoolArena <T> heapArena, PoolArena <T> directArena,
                                 int tinyCacheSize, int smallCacheSize, int normalCacheSize,
                                 int maxCachedBufferCapacity, int freeSweepAllocationThreshold)
        {
            if ((uint)maxCachedBufferCapacity > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(maxCachedBufferCapacity, ExceptionArgument.maxCachedBufferCapacity);
            }

            _freeSweepAllocationThreshold = freeSweepAllocationThreshold;
            HeapArena   = heapArena;
            DirectArena = directArena;
            if (directArena is object)
            {
                tinySubPageDirectCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                smallSubPageDirectCaches = CreateSubPageCaches(
                    smallCacheSize, directArena.NumSmallSubpagePools, SizeClass.Small);

                _numShiftsNormalDirect = Log2(directArena.PageSize);
                normalDirectCaches     = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, directArena);

                directArena.IncrementNumThreadCaches();
            }
            else
            {
                // No directArea is configured so just null out all caches
                tinySubPageDirectCaches  = null;
                smallSubPageDirectCaches = null;
                normalDirectCaches       = null;
                _numShiftsNormalDirect   = -1;
            }
            if (heapArena is object)
            {
                // Create the caches for the heap allocations
                tinySubPageHeapCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                smallSubPageHeapCaches = CreateSubPageCaches(
                    smallCacheSize, heapArena.NumSmallSubpagePools, SizeClass.Small);

                _numShiftsNormalHeap = Log2(heapArena.PageSize);
                normalHeapCaches     = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, heapArena);

                heapArena.IncrementNumThreadCaches();
            }
            else
            {
                // No heapArea is configured so just null out all caches
                tinySubPageHeapCaches  = null;
                smallSubPageHeapCaches = null;
                normalHeapCaches       = null;
                _numShiftsNormalHeap   = -1;
            }

            // We only need to watch the thread when any cache is used.
            if (tinySubPageDirectCaches is object || smallSubPageDirectCaches is object || normalDirectCaches is object ||
                tinySubPageHeapCaches is object || smallSubPageHeapCaches is object || normalHeapCaches is object)
            {
                if (freeSweepAllocationThreshold < 1)
                {
                    ThrowHelper.ThrowArgumentException_Positive(freeSweepAllocationThreshold, ExceptionArgument.freeSweepAllocationThreshold);
                }
                _freeTask         = Free0;
                _deathWatchThread = Thread.CurrentThread;

                // The thread-local cache will keep a list of pooled buffers which must be returned to
                // the pool when the thread is not alive anymore.
                ThreadDeathWatcher.Watch(_deathWatchThread, _freeTask);
            }
            else
            {
                _freeTask         = null;
                _deathWatchThread = null;
            }
        }
Пример #3
0
        MemoryRegionCache CacheForSmall(PoolArena <T> area, int normCapacity)
        {
            int idx = PoolArena <T> .SmallIdx(normCapacity);

            return(Cache(area.IsDirect ? smallSubPageDirectCaches : smallSubPageHeapCaches, idx));
        }
Пример #4
0
        MemoryRegionCache CacheForTiny(PoolArena <T> area, int normCapacity)
        {
            int idx = PoolArena <T> .TinyIdx(normCapacity);

            return(Cache(area.IsDirect ? tinySubPageDirectCaches : tinySubPageHeapCaches, idx));
        }
Пример #5
0
 /// <summary>
 /// Try to allocate a small buffer out of the cache
 /// </summary>
 /// <returns><c>true</c> if successful <c>false</c> otherwise</returns>
 internal bool AllocateNormal(PoolArena <T> area, PooledByteBuffer <T> buf, int reqCapacity, int normCapacity) =>
 Allocate(CacheForNormal(area, normCapacity), buf, reqCapacity);
Пример #6
0
        // TODO: Test if adding padding helps under contention
        //private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;

        internal PoolThreadCache(PoolArena <T> heapArena, PoolArena <T> directArena,
                                 int tinyCacheSize, int smallCacheSize, int normalCacheSize,
                                 int maxCachedBufferCapacity, int freeSweepAllocationThreshold)
        {
            Contract.Requires(maxCachedBufferCapacity >= 0);
            Contract.Requires(freeSweepAllocationThreshold > 0);

            this.freeSweepAllocationThreshold = freeSweepAllocationThreshold;
            this.HeapArena   = heapArena;
            this.DirectArena = directArena;
            if (directArena != null)
            {
                this.tinySubPageDirectCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                this.smallSubPageDirectCaches = CreateSubPageCaches(
                    smallCacheSize, directArena.NumSmallSubpagePools, SizeClass.Small);

                this.numShiftsNormalDirect = Log2(directArena.PageSize);
                this.normalDirectCaches    = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, directArena);

                directArena.IncrementNumThreadCaches();
            }
            else
            {
                // No directArea is configured so just null out all caches
                this.tinySubPageDirectCaches  = null;
                this.smallSubPageDirectCaches = null;
                this.normalDirectCaches       = null;
                this.numShiftsNormalDirect    = -1;
            }
            if (heapArena != null)
            {
                // Create the caches for the heap allocations
                this.tinySubPageHeapCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                this.smallSubPageHeapCaches = CreateSubPageCaches(
                    smallCacheSize, heapArena.NumSmallSubpagePools, SizeClass.Small);

                this.numShiftsNormalHeap = Log2(heapArena.PageSize);
                this.normalHeapCaches    = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, heapArena);

                heapArena.IncrementNumThreadCaches();
            }
            else
            {
                // No heapArea is configured so just null out all caches
                this.tinySubPageHeapCaches  = null;
                this.smallSubPageHeapCaches = null;
                this.normalHeapCaches       = null;
                this.numShiftsNormalHeap    = -1;
            }

            // We only need to watch the thread when any cache is used.
            if (this.tinySubPageDirectCaches != null || this.smallSubPageDirectCaches != null || this.normalDirectCaches != null ||
                this.tinySubPageHeapCaches != null || this.smallSubPageHeapCaches != null || this.normalHeapCaches != null)
            {
                this.freeTask         = this.Free0;
                this.deathWatchThread = Thread.CurrentThread;

                // The thread-local cache will keep a list of pooled buffers which must be returned to
                // the pool when the thread is not alive anymore.
                ThreadDeathWatcher.Watch(this.deathWatchThread, this.freeTask);
            }
            else
            {
                this.freeTask         = null;
                this.deathWatchThread = null;
            }
        }