Exemplo n.º 1
0
        public bool TryFastAppend(Pointer ip, int available, int len)
        {
            int spaceLeft = _length - _index;

            if (len > 16 || available < 16 || spaceLeft < 16)
            {
                return(false);
            }

            var op = new Pointer(_buffer, _index);

            op.Copy64(ip);
            op.Copy64(ip + 8, 8);

            _index += len;
            return(true);
        }
Exemplo n.º 2
0
        private void IncrementalCopyFastPath(Pointer src, Pointer op, int len)
        {
            while (op - src < 8)
            {
                op.Copy64(src);
                len -= op - src;
                op  += op - src;
            }

            while (len > 0)
            {
                op.Copy64(src);
                src += 8;
                op  += 8;
                len -= 8;
            }
        }
Exemplo n.º 3
0
        private static Pointer EmitLiteral(Pointer dest, Pointer literal, int length, bool allowFastPath)
        {
            int n = length - 1;

            if (n < 60)
            {
                var value = CompressorTag.Literal | (n << 2);
                dest[0] = (byte)value;
                dest   += 1;

                if (allowFastPath && length <= 16)
                {
                    dest.Copy64(literal);
                    dest.Copy64(literal + 8, offset: 8);
                    return(dest + length);
                }
            }
            else
            {
                var tmp = new Pointer(dest);
                dest += 1;

                int count = 0;
                while (n > 0)
                {
                    dest[count] = (byte)(n & 0xff);
                    n         >>= 8;
                    count++;
                }

                Debug.Assert(count >= 1);
                Debug.Assert(count <= 4);

                tmp[0] = (byte)(CompressorTag.Literal | (59 + count) << 2);
                dest  += count;
            }

            dest.Copy(literal, length);
            return(dest + length);
        }
Exemplo n.º 4
0
        public bool AppendFromSelf(int offset, int len)
        {
            int spaceLeft = _length - _index;

            if (_index <= offset - 1u)
            {  // -1u catches offset==0
                return(false);
            }

            var op = new Pointer(_buffer, _index);

            if (len <= 16 && offset >= 8 && spaceLeft >= 16)
            {
                var src = new Pointer(_buffer, _index - offset);
                op.Copy64(src);
                op.Copy64(src + 8, offset: 8);
            }
            else
            {
                if (spaceLeft >= len + CompressorConstants.MaxIncrementCopyOverflow)
                {
                    IncrementalCopyFastPath(op - offset, op, len);
                }
                else
                {
                    if (spaceLeft < len)
                    {
                        return(false);
                    }

                    IncrementalCopy(op - offset, op, len);
                }
            }

            _index += len;
            return(true);
        }