public void TraceStats()
        {
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < this.m_bucketCount; i++)
            {
                SortedBucket sortedBucket = this.m_buckets[i];
                stringBuilder.AppendFormat("\r\n\t\tBucket: Limit: {0} Count: {1}", sortedBucket.Limit, sortedBucket.Count);
            }
            Global.Tracer.Trace(TraceLevel.Verbose, "DynamicBucketedHeapSpaceManager Stats. StreamSize: {0} MB. UnusableSpace: {1} KB. \r\n\tBucketInfo: {2}", this.m_end / 1048576, this.m_unuseableBytes / 1024, stringBuilder.ToString());
        }
        public DynamicBucketedHeapSpaceManager(int splitThreshold, int maxBucketCount, int maxSpacesPerBucket, int minTrackedSizeBytes)
        {
            this.m_bucketSplitThreshold = splitThreshold;
            this.m_maxBucketCount       = maxBucketCount;
            this.m_maxSpacesPerBucket   = maxSpacesPerBucket;
            this.m_minimumTrackedSize   = minTrackedSizeBytes;
            this.m_buckets = new SortedBucket[this.m_maxBucketCount];
            SortedBucket sortedBucket = new SortedBucket(this.m_maxSpacesPerBucket)
            {
                Limit = 0
            };

            this.m_buckets[0] = sortedBucket;
            this.m_bucketCount++;
        }
 private void InsertSpace(Space space)
 {
     if (space.Size < this.m_minimumTrackedSize)
     {
         this.m_unuseableBytes += space.Size;
     }
     else
     {
         int          bucketIndex  = this.GetBucketIndex(space.Size);
         SortedBucket sortedBucket = this.m_buckets[bucketIndex];
         if (sortedBucket.Count == this.m_maxSpacesPerBucket)
         {
             if (this.m_bucketCount < this.m_maxBucketCount && sortedBucket.Maximum - sortedBucket.Minimum > this.m_bucketSplitThreshold)
             {
                 SortedBucket sortedBucket2 = sortedBucket.Split(this.m_maxSpacesPerBucket);
                 for (int num = this.m_bucketCount; num > bucketIndex + 1; num--)
                 {
                     this.m_buckets[num] = this.m_buckets[num - 1];
                 }
                 this.m_buckets[bucketIndex + 1] = sortedBucket2;
                 this.m_bucketCount++;
                 this.InsertSpace(space);
             }
             else if (sortedBucket.Peek().Size < space.Size)
             {
                 Space space2 = sortedBucket.ExtractMax();
                 this.m_unuseableBytes += space2.Size;
                 sortedBucket.Insert(space);
             }
             else
             {
                 this.m_unuseableBytes += space.Size;
             }
         }
         else
         {
             sortedBucket.Insert(space);
         }
     }
 }
        public long AllocateSpace(long size)
        {
            long num = -1L;

            for (int i = this.GetBucketIndex(size); i < this.m_bucketCount; i++)
            {
                if (num != -1)
                {
                    break;
                }
                SortedBucket sortedBucket = this.m_buckets[i];
                if (sortedBucket.Count > 0)
                {
                    Space space = sortedBucket.Peek();
                    if (space.Size >= size)
                    {
                        sortedBucket.ExtractMax();
                        num           = space.Offset;
                        space.Offset += size;
                        space.Size   -= size;
                        if (space.Size > 0)
                        {
                            this.InsertSpace(space);
                        }
                        if (sortedBucket.Count == 0 && i != 0)
                        {
                            Array.Copy(this.m_buckets, i + 1, this.m_buckets, i, this.m_bucketCount - i - 1);
                            this.m_bucketCount--;
                        }
                    }
                }
            }
            if (num == -1 && this.m_allowEndAllocation)
            {
                num         = this.m_end;
                this.m_end += size;
            }
            return(num);
        }