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);
        }