Example #1
0
        private ByteVault GetVault(int sizeHint)
        {
            int bufferSizeToAllocate = 0;

            if (sizeHint == 0)
            {
                if (this.lastVault == null || this.lastVault.Remaining == 0)
                {
                    bufferSizeToAllocate = DefaultVaultSize;
                }
            }
            else
            {
                if (this.lastVault == null || this.lastVault.Remaining < sizeHint)
                {
                    bufferSizeToAllocate = Math.Max(sizeHint, DefaultVaultSize);
                }
            }

            if (bufferSizeToAllocate > 0)
            {
                var vault = new ByteVault(arrayPool.Rent(bufferSizeToAllocate));
                this.AddVault(vault);
            }

            return(this.lastVault !);
        }
Example #2
0
        private void AddVault(ByteVault vault)
        {
            if (this.lastVault == null)
            {
                this.firstVault = this.lastVault = vault;
            }
            else
            {
                if (this.lastVault.Size > 0)
                {// Add a new block.
                    this.lastVault.SetNext(vault);
                }
                else
                {// The last block is completely unused. Replace it instead of appending to it.
                    var current = this.firstVault !;
                    if (this.firstVault == this.lastVault)
                    { // Only one vault.
                        this.firstVault = vault;
                    }
                    else
                    {
                        while (current.Next != this.lastVault)
                        {
                            current = (ByteVault)current.Next !;
                        }
                    }

                    arrayPool.Return(this.lastVault.Array);
                    this.lastVault.Clear();

                    current.SetNext(vault);
                }

                this.lastVault = vault;
            }
        }
Example #3
0
 internal void SetNext(ByteVault next)
 {
     this.Next         = next;
     next.RunningIndex = this.RunningIndex + this.Size;
     this.Memory       = this.Memory.Slice(0, this.Size);
 }