Example #1
0
        /// <summary>
        /// Get a object from the buffer manager.  If no buffers exist, allocate a new one.
        /// </summary>
        internal object Allocate()
        {
#if ENABLE
            // Check to see whether or not the cache is disabled.
            if (m_CacheName == null)
            {
                return(m_factory());
            }
#endif
            // Fast path, get it from our Gen2 aged m_FreeList.
            object returnBuffer;
            if (!m_FreeList.TryPop(out returnBuffer))
            {
                Restock(out returnBuffer);
            }

#if LOGGING
            // Computing free count is expensive enough that we don't want to compute it unless logging is on.
            if (PinnableBufferCacheEventSource.Log.IsEnabled())
            {
                int numAllocCalls = Interlocked.Increment(ref m_numAllocCalls);
                if (numAllocCalls >= 1024)
                {
                    lock (this)
                    {
                        int previousNumAllocCalls = Interlocked.Exchange(ref m_numAllocCalls, 0);
                        if (previousNumAllocCalls >= 1024)
                        {
                            int nonGen2Count = 0;
                            foreach (object o in m_FreeList)
                            {
                                if (GC.GetGeneration(o) < GC.MaxGeneration)
                                {
                                    nonGen2Count++;
                                }
                            }

                            PinnableBufferCacheEventSource.Log.WalkFreeListResult(m_CacheName, m_FreeList.Count, nonGen2Count);
                        }
                    }
                }

                PinnableBufferCacheEventSource.Log.AllocateBuffer(m_CacheName, PinnableBufferCacheEventSource.AddressOf(returnBuffer), returnBuffer.GetHashCode(), GC.GetGeneration(returnBuffer), m_FreeList.Count);
            }
#endif
            return(returnBuffer);
        }
        internal object Allocate()
        {
            if (this.m_CacheName == null)
            {
                return(this.m_factory());
            }
            object obj;

            if (!this.m_FreeList.TryPop(out obj))
            {
                this.Restock(out obj);
            }
            if (PinnableBufferCacheEventSource.Log.IsEnabled())
            {
                int num = Interlocked.Increment(ref this.m_numAllocCalls);
                if (num >= 1024)
                {
                    lock (this)
                    {
                        int num2 = Interlocked.Exchange(ref this.m_numAllocCalls, 0);
                        if (num2 >= 1024)
                        {
                            int num3 = 0;
                            foreach (object obj2 in this.m_FreeList)
                            {
                                if (GC.GetGeneration(obj2) < GC.MaxGeneration)
                                {
                                    num3++;
                                }
                            }
                            PinnableBufferCacheEventSource.Log.WalkFreeListResult(this.m_CacheName, this.m_FreeList.Count, num3);
                        }
                    }
                }
                PinnableBufferCacheEventSource.Log.AllocateBuffer(this.m_CacheName, PinnableBufferCacheEventSource.AddressOf(obj), obj.GetHashCode(), GC.GetGeneration(obj), this.m_FreeList.Count);
            }
            return(obj);
        }