public void WriteString(LazyCompressedStringValue str)
        {
            var strBuffer = str.DecompressToTempBuffer(out AllocatedMemoryData allocated, _context);

            try
            {
                var strSrcBuffer = str.Buffer;

                var size = str.UncompressedSize;
                var escapeSequencePos       = str.CompressedSize;
                var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos);

                // We ensure our buffer will have enough space to deal with the whole string.
                int bufferSize = 2 * numberOfEscapeSequences + size + 2;
                if (bufferSize >= JsonOperationContext.ManagedPinnedBuffer.Size)
                {
                    goto WriteLargeCompressedString; // OK, do it the slow way instead.
                }
                EnsureBuffer(bufferSize);

                _buffer[_pos++] = Quote;
                while (numberOfEscapeSequences > 0)
                {
                    numberOfEscapeSequences--;
                    var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos);
                    WriteRawString(strBuffer, bytesToSkip);
                    strBuffer += bytesToSkip;
                    size      -= bytesToSkip + 1 /*for the escaped char we skip*/;
                    var b = *(strBuffer++);

                    var auxPos = _pos;
                    _buffer[auxPos++] = (byte)'\\';
                    _buffer[auxPos++] = GetEscapeCharacter(b);
                    _pos = auxPos;
                }

                // write remaining (or full string) to the buffer in one shot
                WriteRawString(strBuffer, size);

                _buffer[_pos++] = Quote;

                return;

WriteLargeCompressedString:
                UnlikelyWriteLargeString(numberOfEscapeSequences, strSrcBuffer, escapeSequencePos, strBuffer, size);
            }
            finally
            {
                if (allocated != null) //precaution
                {
                    _context.ReturnMemory(allocated);
                }
            }
        }
Exemple #2
0
 public void Reset()
 {
     _unmanagedWriteBuffer.Dispose();
     if (_compressionBuffer != null)
     {
         _context.ReturnMemory(_compressionBuffer);
         _compressionBuffer = null;
     }
     if (_innerBuffer != null)
     {
         _context.ReturnMemory(_innerBuffer);
         _innerBuffer = null;
     }
 }
        public void Dispose()
        {
            if (_buffer != null)
            {
                _context.ReturnMemory(_buffer);
                _buffer = null;
            }
            if (_compressionBuffer != null)
            {
                _context.ReturnMemory(_compressionBuffer);
                _compressionBuffer = null;
            }

            _stream.Dispose();
        }
        public void Clear()
        {
            _current.Used = 0;
            _sizeInBytes  = 0;

            // this releases everything but the current item
            var prev = _current.Previous;

            _current.Previous = null;
            while (prev != null)
            {
                _context.ReturnMemory(prev.Allocation);
                prev = prev.Previous;
            }
        }
Exemple #5
0
        public void Dispose()
        {
            //    _unmanagedWriteBuffer.Dispose();
            if (_compressionBuffer != null)
            {
                _context.ReturnMemory(_compressionBuffer);
            }

            _compressionBuffer = null;

            if (_innerBuffer != null)
            {
                _context.ReturnMemory(_innerBuffer);
            }
            _innerBuffer = null;
        }
        public bool Equals(LazyCompressedStringValue other)
        {
            if (other.UncompressedSize != UncompressedSize)
            {
                return(false);
            }

            if (other.CompressedSize == CompressedSize)
            {
                return(Memory.Compare(Buffer, other.Buffer, CompressedSize) == 0);
            }

            AllocatedMemoryData otherAllocated = null;
            AllocatedMemoryData allocated      = null;

            try
            {
                var otherTempBuffer = other.DecompressToTempBuffer(out otherAllocated);
                var tempBuffer      = DecompressToTempBuffer(out allocated);

                return(Memory.Compare(tempBuffer, otherTempBuffer, UncompressedSize) == 0);
            }
            finally
            {
                if (otherAllocated != null)
                {
                    other._context.ReturnMemory(otherAllocated);
                }

                if (allocated != null)
                {
                    _context.ReturnMemory(allocated);
                }
            }
        }
Exemple #7
0
 public void Dispose()
 {
     if (AllocatedMemoryData != null)
     {
         _context.ReturnMemory(AllocatedMemoryData);
         AllocatedMemoryData = null;
     }
 }
 public void Dispose()
 {
     while (_current != null)
     {
         _context.ReturnMemory(_current.Allocation);
         _current = _current.Previous;
     }
 }
Exemple #9
0
        public void Dispose()
        {
            if (IsDisposed)
            {
                ThrowAlreadyDisposed();
            }

            if (AllocatedMemoryData != null)
            {
                _context.ReturnMemory(AllocatedMemoryData);
            }
            IsDisposed = true;
        }
Exemple #10
0
        public void WriteString(LazyCompressedStringValue str)
        {
            AllocatedMemoryData allocated;
            var strBuffer = str.DecompressToTempBuffer(out allocated);

            try
            {
                var size = str.UncompressedSize;

                EnsureBuffer(1);
                _buffer[_pos++] = Quote;
                var escapeSequencePos       = str.CompressedSize;
                var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(str.Buffer, ref escapeSequencePos);
                while (numberOfEscapeSequences > 0)
                {
                    numberOfEscapeSequences--;
                    var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(str.Buffer, ref escapeSequencePos);
                    WriteRawString(strBuffer, bytesToSkip);
                    strBuffer += bytesToSkip;
                    size      -= bytesToSkip + 1 /*for the escaped char we skip*/;
                    var b = *(strBuffer++);
                    EnsureBuffer(2);
                    _buffer[_pos++] = (byte)'\\';
                    _buffer[_pos++] = GetEscapeCharacter(b);
                }
                // write remaining (or full string) to the buffer in one shot
                WriteRawString(strBuffer, size);

                EnsureBuffer(1);
                _buffer[_pos++] = Quote;
            }
            finally
            {
                if (allocated != null) //precaution
                {
                    _context.ReturnMemory(allocated);
                }
            }
        }
Exemple #11
0
        private void ReturnAllocatedMemory()
        {
            if (AllocatedMemoryData == null)
            {
                return;
            }

            if (_context.Generation == AllocatedMemoryData.ContextGeneration)
            {
                _context.ReturnMemory(AllocatedMemoryData);
            }

            AllocatedMemoryData = null;
        }
        public void Dispose()
        {
            if (IsDisposed)
            {
                return;
            }

#if MEM_GUARD
            FreedBy = Environment.StackTrace;
#endif

            // The actual lifetime of the _head Segment is unbounded: it will
            // be released by the GC when we no longer have any references to
            // it (i.e. no more copies of this struct)
            //
            // We can, however, force the deallocation of all the previous
            // Segments by ensuring we don't keep any references after the
            // Dispose is run.

            var head = _head;
            _head = null; // Further references are NREs.
            for (Segment next; head != null; head = next)
            {
                _context.ReturnMemory(head.Allocation);

                // This is used to signal that Dispose has run to other copies
                head.Address = null;

#if DEBUG
                // Helps to avoid program errors, albeit unnecessary
                head.Allocation             = null;
                head.AccumulatedSizeInBytes = -1;
                head.Used = -1;
#endif

                // `next` is used to keep a reference to the previous Segment.
                // Since `next` lives only within this for loop and we clear up
                // all other references, non-head Segments should be GC'd.
                next          = head.DeallocationPendingPrevious;
                head.Previous = null;
                head.DeallocationPendingPrevious = null;
            }
        }
Exemple #13
0
        public void Dispose()
        {
#if MEM_GUARD
            if (FreedBy == null) //if already disposed, keep the "FreedBy"
            {
                FreedBy = Environment.StackTrace;
            }
#endif

            var start = _current;
            while (_current != null &&
                   _current.Address != null) //prevent double dispose
            {
                _context.ReturnMemory(_current.Allocation);
                _current.Address = null; //precaution, to make memory issues more visible
                _current         = _current.PreviousAllocated;
            }
            GC.KeepAlive(start);
        }