示例#1
0
        public AbstractBlittableJsonTextWriter(JsonOperationContext context, Stream stream)
        {
            _context = context;
            _stream  = stream;

            _returnBuffer = context.GetManagedBuffer(out _pinnedBuffer);
            _buffer       = _pinnedBuffer.Pointer;

            _parserAuxiliarMemory = context.GetMemory(32);
        }
示例#2
0
        public void Return(AllocatedMemoryData allocation)
        {
            if (_isDisposed)
            {
                return;
            }

            var address = allocation.Address;

#if DEBUG
            Debug.Assert(address != _ptrCurrent);
            Debug.Assert(allocation.IsReturned == false);
            allocation.IsReturned = true;
#endif

#if MEM_GUARD
#if MEM_GUARD_STACK
            if (allocation.FreedBy == null)
            {
                allocation.FreedBy = Environment.StackTrace;
            }
#endif
            ElectricFencedMemory.Free(address);
#else
            if (address != _ptrCurrent - allocation.SizeInBytes ||
                address < _ptrStart)
            {
                // we have fragmentation, so'll just store the values that we need here
                // in the memory we just freed :-)

                // note that this fragmentation will be healed by the call to ResetArena
                // trying to do this on the fly is too expensive.

                Debug.Assert(Bits.NextPowerOf2(allocation.SizeInBytes) == allocation.SizeInBytes,
                             "Allocation size must always be a power of two"
                             );
                Debug.Assert(allocation.SizeInBytes >= sizeof(FreeSection));


                var index   = Bits.MostSignificantBit(allocation.SizeInBytes) - 1;
                var section = (FreeSection *)address;
                section->SizeInBytes = allocation.SizeInBytes;
                section->Previous    = _freed[index];
                _freed[index]        = section;
                return;
            }
            // since the returned allocation is at the end of the arena, we can just move
            // the pointer back
            _used       -= allocation.SizeInBytes;
            TotalUsed   -= allocation.SizeInBytes;
            _ptrCurrent -= allocation.SizeInBytes;
#endif
        }
示例#3
0
 public void Reset()
 {
     _unmanagedWriteBuffer.Dispose();
     if (_compressionBuffer != null)
     {
         _context.ReturnMemory(_compressionBuffer);
         _compressionBuffer = null;
     }
     if (_innerBuffer != null)
     {
         _context.ReturnMemory(_innerBuffer);
         _innerBuffer = null;
     }
 }
示例#4
0
        private unsafe byte *GetCompressionBuffer(int minSize)
        {
            // enlarge buffer if needed
            if (_compressionBuffer == null ||
                minSize > _compressionBuffer.SizeInBytes)
            {
                if (_compressionBuffer != null)
                {
                    _context.ReturnMemory(_compressionBuffer);
                }
                _compressionBuffer = _context.GetMemory(minSize);
            }

            return(_compressionBuffer.Address);
        }
        public void Return(AllocatedMemoryData returned)
        {
#if MEM_GUARD
            ElectricFencedMemory.Free(returned.Address);
            GC.SuppressFinalize(returned);
#else
            if (returned == null)
            {
                throw new ArgumentNullException(nameof(returned));
            }
            var index = GetIndexFromSize(returned.SizeInBytes);
            if (index == -1)
            {
                NativeMemory.Free(returned.Address, returned.SizeInBytes, returned.AllocatingThread);

                return; // strange size, just free it
            }
            _freeSegments[index].Push(returned);
#endif
        }
示例#6
0
        public UnmanagedWriteBuffer(JsonOperationContext context, AllocatedMemoryData allocatedMemoryData)
        {
            Debug.Assert(context != null);
            Debug.Assert(allocatedMemoryData != null);

            _context = context;
            _head    = new Segment
            {
                Previous = null,
                DeallocationPendingPrevious = null,
                Allocation             = allocatedMemoryData,
                Address                = allocatedMemoryData.Address,
                Used                   = 0,
                AccumulatedSizeInBytes = 0
            };

#if MEM_GUARD
            AllocatedBy = Environment.StackTrace;
            FreedBy     = null;
#endif
        }
示例#7
0
        public bool GrowAllocation(AllocatedMemoryData allocation, int sizeIncrease)
        {
            byte *end      = allocation.Address + allocation.SizeInBytes;
            var   distance = end - _ptrCurrent;

            if (distance != 0)
            {
                return(false);
            }

            // we need to keep the total allocation size as power of 2
            sizeIncrease = Bits.NextPowerOf2(allocation.SizeInBytes + sizeIncrease) - allocation.SizeInBytes;

            if (_used + sizeIncrease > _allocated)
            {
                return(false);
            }

            _ptrCurrent            += sizeIncrease;
            _used                  += sizeIncrease;
            TotalUsed              += sizeIncrease;
            allocation.SizeInBytes += sizeIncrease;
            return(true);
        }
示例#8
0
 public BlittableWriter(JsonOperationContext context)
 {
     _context     = context;
     _innerBuffer = _context.GetMemory(32);
 }
示例#9
0
 public BlittableWriter(JsonOperationContext context, TWriter writer)
 {
     _context = context;
     _unmanagedWriteBuffer = writer;
     _innerBuffer          = _context.GetMemory(32);
 }