Exemple #1
0
        public static void OpCodes(MemoryChunk actualChunk, MemoryChunk expectedChunk)
        {
            string message = null;

            if (expectedChunk.Length != actualChunk.Length)
            {
                message = string.Format("expected and actual image lenght doesn't match, expected {0}, actual is {1}",
                    expectedChunk.Length, actualChunk.Length);
            }

            if (string.IsNullOrEmpty(message))
            {
                ushort i = 0;

                foreach (var expectedByte in expectedChunk)
                {
                    if (expectedByte != actualChunk[i])
                    {
                        message = string.Format("Mismatch on position {0}. Expected {1}, actual is {2}", i, expectedByte,
                            actualChunk[i]);
                        break;
                    }
                    i++;
                }
            }

            if (!string.IsNullOrEmpty(message))
            {
                var expectedText = expectedChunk.Select(x => x.ToString("X2")).Aggregate((l, r) => l + ", " + r);
                var actualText = actualChunk.Select(x => x.ToString("X2")).Aggregate((l, r) => l + ", " + r);
                Assert.Fail("{0}\nexpected:\n{1}\nactual:\n{2}", message, expectedText, actualText);
            }
        }
 internal ScatterGatherBuffers(long totalSize)
 {
     this.nextChunkLength = 0x400;
     if (totalSize > 0L)
     {
         this.currentChunk = this.AllocateMemoryChunk((totalSize > 0x7fffffffL) ? 0x7fffffff : ((int) totalSize));
     }
 }
 internal ScatterGatherBuffers(long totalSize)
 {
     // We know up front how much data is to be written.
     if (totalSize > 0)
     {
         currentChunk = AllocateMemoryChunk(totalSize > Int32.MaxValue ? Int32.MaxValue : (int) totalSize);
     }
 }
Exemple #4
0
 private static void Write(MemoryChunk chunk, FileStream writeStream)
 {
     writeStream.WriteByte(0xFF);
     writeStream.WriteByte(0xFF);
     writeStream.WriteByte((byte)(chunk.StartAddress & 0xFF));
     writeStream.WriteByte((byte)((chunk.StartAddress & 0xFF00) >> 8));
     writeStream.WriteByte((byte)(chunk.Length & 0xFF));
     writeStream.WriteByte((byte)((chunk.Length & 0xFF00) >> 8));
     writeStream.Write(chunk.Buffer, 0, chunk.Length);
 }
Exemple #5
0
		public void Add (byte[] buffer, int offset, int size)
		{
			var chunk = new MemoryChunk (buffer, offset, size);
			if (last == null)
				first = last = chunk;
			else {
				last.next = chunk;
				last = chunk;
			}
		}
 private long GetLengthInternal(out MemoryChunk endChunk){
     long length = currentChunkStartPos;
     MemoryChunk chunk = currentChunk;
     while (chunk.Next != null) {
         length += chunk.Buffer.Length;
         chunk = chunk.Next;
     }
     length += endOffset;
     endChunk = chunk;
     return length;
 }
 private MemoryChunk AllocateMemoryChunk(int newSize)
 {
     if (newSize > this.nextChunkLength)
     {
         this.nextChunkLength = newSize;
     }
     MemoryChunk chunk = new MemoryChunk(this.nextChunkLength);
     if (this.Empty)
     {
         this.headChunk = chunk;
     }
     this.nextChunkLength *= 2;
     this.chunkCount++;
     return chunk;
 }
 public override int Read(byte[] buffer, int offset, int count)
 {
     if (this._bClosed)
     {
         throw new RemotingException(CoreChannel.GetResourceString("Remoting_Stream_StreamIsClosed"));
     }
     if (this._readChunk == null)
     {
         if (this._chunks == null)
         {
             return 0;
         }
         this._readChunk = this._chunks;
         this._readOffset = 0;
     }
     byte[] src = this._readChunk.Buffer;
     int length = src.Length;
     if (this._readChunk.Next == null)
     {
         length = this._writeOffset;
     }
     int num2 = 0;
     while (count > 0)
     {
         if (this._readOffset == length)
         {
             if (this._readChunk.Next == null)
             {
                 return num2;
             }
             this._readChunk = this._readChunk.Next;
             this._readOffset = 0;
             src = this._readChunk.Buffer;
             length = src.Length;
             if (this._readChunk.Next == null)
             {
                 length = this._writeOffset;
             }
         }
         int num3 = Math.Min(count, length - this._readOffset);
         Buffer.BlockCopy(src, this._readOffset, buffer, offset, num3);
         offset += num3;
         count -= num3;
         this._readOffset += num3;
         num2 += num3;
     }
     return num2;
 }
 protected override void Dispose(bool disposing)
 {
     try
     {
         this._bClosed = true;
         if (disposing)
         {
             this.ReleaseMemoryChunks(this._chunks);
         }
         this._chunks = null;
         this._writeChunk = null;
         this._readChunk = null;
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
        public void Write(byte[] buffer, int offset, int count) {
            GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::Add() count:" + count.ToString());
            while (count > 0) {
                //
                // compute available space in current allocated buffer (0 if there's no buffer)
                //
                int available = Empty ? 0 : currentChunk.Buffer.Length - currentChunk.FreeOffset;
                GlobalLog.Assert(available>=0, "ScatterGatherBuffers::Add() available<0", "");
                //
                // if the current chunk is is full, allocate a new one
                //
                if (available==0) {
                    // ask for at least count bytes so that we need at most one allocation
                    MemoryChunk newChunk = AllocateMemoryChunk(count);
                    if (currentChunk!=null) {
                        currentChunk.Next = newChunk;
                    }
                    //
                    // move ahead in the linked list (or at the beginning if this is the fist buffer)
                    //
                    currentChunk = newChunk;
                }
                int copyCount = count < available ? count : available;

                Buffer.BlockCopy(
                    buffer,                     // src
                    offset,                     // src index
                    currentChunk.Buffer,        // dest
                    currentChunk.FreeOffset,    // dest index
                    copyCount );                // total size to copy

                //
                // update offsets and counts
                //
                offset += copyCount;
                count -= copyCount;
                totalLength += copyCount;
                currentChunk.FreeOffset += copyCount;
            }
            GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::Add() totalLength:" + totalLength.ToString());
        }
        private void AppendChunk(long count)
        {
            int nextChunkLength = _currentChunk != null ? _currentChunk._buffer.Length * 2 : InitialChunkDefaultSize;
            if (count > nextChunkLength)
            {
                nextChunkLength = (int)Math.Min(count, MaxChunkSize);
            }

            MemoryChunk newChunk = new MemoryChunk(nextChunkLength);

            if (_currentChunk == null)
            {
                Debug.Assert(_headChunk == null);
                _headChunk = _currentChunk = newChunk;
            }
            else
            {
                Debug.Assert(_headChunk != null);
                _currentChunk._next = newChunk;
                _currentChunk = newChunk;
            }
        }
 internal void Write(byte[] buffer, int offset, int count)
 {
     while (count > 0)
     {
         int num = this.Empty ? 0 : (this.currentChunk.Buffer.Length - this.currentChunk.FreeOffset);
         if (num == 0)
         {
             MemoryChunk chunk = this.AllocateMemoryChunk(count);
             if (this.currentChunk != null)
             {
                 this.currentChunk.Next = chunk;
             }
             this.currentChunk = chunk;
         }
         int num2 = (count < num) ? count : num;
         Buffer.BlockCopy(buffer, offset, this.currentChunk.Buffer, this.currentChunk.FreeOffset, num2);
         offset += num2;
         count -= num2;
         this.totalLength += num2;
         this.currentChunk.FreeOffset += num2;
     }
 }
Exemple #13
0
        internal BufferOffsetSize[] GetBuffers()
        {
            if (Empty)
            {
                return(null);
            }
            GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::ToArray() chunkCount:" + chunkCount.ToString());
            BufferOffsetSize[] array    = new BufferOffsetSize[chunkCount];
            int         index           = 0;
            MemoryChunk thisMemoryChunk = headChunk;

            while (thisMemoryChunk != null)
            {
                GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::ToArray() index:" + index.ToString() + " size:" + thisMemoryChunk.FreeOffset);
                //
                // buffer itself is referenced by the BufferOffsetSize struct, data is not copied
                //
                array[index] = new BufferOffsetSize(thisMemoryChunk.Buffer, 0, thisMemoryChunk.FreeOffset, false);
                index++;
                thisMemoryChunk = thisMemoryChunk.Next;
            }
            return(array);
        }
Exemple #14
0
        internal void Write(byte[] buffer, int offset, int count)
        {
            while (count > 0)
            {
                int num = this.Empty ? 0 : (currentChunk.Buffer.Length - currentChunk.FreeOffset);
                if (num == 0)
                {
                    MemoryChunk chunk = AllocateMemoryChunk(count);
                    if (currentChunk != null)
                    {
                        currentChunk.Next = chunk;
                    }
                    currentChunk = chunk;
                }
                int num2 = (count < num) ? count : num;
                Buffer.BlockCopy(buffer, offset, currentChunk.Buffer, currentChunk.FreeOffset, num2);
                offset += num2;
                count  -= num2;

                totalLength             += num2;
                currentChunk.FreeOffset += num2;
            }
        }
Exemple #15
0
        private MemoryChunk AllocateMemoryChunk(int newSize)
        {
            if (newSize > nextChunkLength)
            {
                nextChunkLength = newSize;
            }
            MemoryChunk newChunk = new MemoryChunk(nextChunkLength);

            if (Empty)
            {
                headChunk = newChunk;
            }
            //
            // next time allocate twice as much. check fot possible overflows
            //
            nextChunkLength *= 2;
            //
            // update number of chunks in the linked list
            //
            chunkCount++;
            GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::AllocateMemoryChunk() chunkCount:" + chunkCount.ToString() + " nextChunkLength:" + nextChunkLength.ToString());
            return(newChunk);
        }
Exemple #16
0
        private void AppendChunk(long count)
        {
            int nextChunkLength = _currentChunk != null ? _currentChunk._buffer.Length * 2 : InitialChunkDefaultSize;

            if (count > nextChunkLength)
            {
                nextChunkLength = (int)Math.Min(count, MaxChunkSize);
            }

            MemoryChunk newChunk = new MemoryChunk(nextChunkLength);

            if (_currentChunk == null)
            {
                Debug.Assert(_headChunk == null);
                _headChunk = _currentChunk = newChunk;
            }
            else
            {
                Debug.Assert(_headChunk != null);
                _currentChunk._next = newChunk;
                _currentChunk       = newChunk;
            }
        }
        } // ReadByte

        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_bClosed)
            {
                throw new RemotingException(
                          CoreChannel.GetResourceString("Remoting_Stream_StreamIsClosed"));
            }

            if (_chunks == null)
            {
                _chunks      = AllocateMemoryChunk();
                _writeChunk  = _chunks;
                _writeOffset = 0;
            }

            byte[] chunkBuffer = _writeChunk.Buffer;
            int    chunkSize   = chunkBuffer.Length;

            while (count > 0)
            {
                if (_writeOffset == chunkSize)
                {
                    // allocate a new chunk if the current one is full
                    _writeChunk.Next = AllocateMemoryChunk();
                    _writeChunk      = _writeChunk.Next;
                    _writeOffset     = 0;
                    chunkBuffer      = _writeChunk.Buffer;
                    chunkSize        = chunkBuffer.Length;
                }

                int copyCount = Math.Min(count, chunkSize - _writeOffset);
                Buffer.BlockCopy(buffer, offset, chunkBuffer, _writeOffset, copyCount);
                offset       += copyCount;
                count        -= copyCount;
                _writeOffset += copyCount;
            }
        } // Write
Exemple #18
0
        /// <inheritdoc/>
        protected override void Dispose(bool disposing)
        {
            if (this.isDisposed)
            {
                return;
            }

            try
            {
                this.isDisposed = true;
                if (disposing)
                {
                    this.ReleaseMemoryChunks(this.memoryChunk);
                }

                this.memoryChunk = null;
                this.writeChunk  = null;
                this.readChunk   = null;
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Exemple #19
0
        private void WriteImpl(ReadOnlySpan <byte> buffer)
        {
            this.EnsureNotDisposed();

            if (this.memoryChunk is null)
            {
                this.memoryChunk = this.AllocateMemoryChunk();
                this.writeChunk  = this.memoryChunk;
                this.writeOffset = 0;
            }

            Span <byte> chunkBuffer = this.writeChunk.Buffer.GetSpan();
            int         chunkSize   = this.writeChunk.Length;
            int         count       = buffer.Length;
            int         offset      = 0;

            while (count > 0)
            {
                if (this.writeOffset == chunkSize)
                {
                    // Allocate a new chunk if the current one is full
                    this.writeChunk.Next = this.AllocateMemoryChunk();
                    this.writeChunk      = this.writeChunk.Next;
                    this.writeOffset     = 0;
                    chunkBuffer          = this.writeChunk.Buffer.GetSpan();
                    chunkSize            = this.writeChunk.Length;
                }

                int copyCount = Math.Min(count, chunkSize - this.writeOffset);
                buffer.Slice(offset, copyCount).CopyTo(chunkBuffer.Slice(this.writeOffset));

                offset           += copyCount;
                count            -= copyCount;
                this.writeOffset += copyCount;
            }
        }
Exemple #20
0
 public NineSliceSpriteBatch(uint maxSprites)
 {
     _sprites = MemoryUtils.AllocateBlock <NineSliceSprite>(maxSprites);
 }
Exemple #21
0
        public static BinaryWriter GetWriter(MemoryChunk chunk)
        {
            var stream = new MemoryStream(chunk.Memory, true);

            return(new BinaryWriter(stream));
        }
Exemple #22
0
 public void MountChunk(MemoryChunk chunk)
 {
     chunks.Add(chunk);
     storage.Add(chunk, new string[chunk.Size]);
 }
Exemple #23
0
 public TextBatch(uint maxTextBlocks, TextManager textManager, FontManager fontManager)
 {
     _textManager = textManager;
     _fontManager = fontManager;
     _textBatches = MemoryUtils.AllocateBlock <TextBatchSprite>(maxTextBlocks);
 }
        } // AllocateMemoryChunk

        private void ReleaseMemoryChunks(MemoryChunk chunk)
        {
            // If the buffer pool always allocates a new buffer,
            //   there's no point to trying to return all of the buffers. 
            if (_bufferPool is ByteBufferAllocator)
                return;

            while (chunk != null)
            {
                _bufferPool.ReturnBuffer(chunk.Buffer);
                chunk = chunk.Next;
            }
                
        } // FreeMemoryChunk
        } // ToArray      


        // write remainder of this stream to another stream
        public virtual void WriteTo(Stream stream)
        {
            if (_bClosed)
            {                
                throw new RemotingException(
                    CoreChannel.GetResourceString("Remoting_Stream_StreamIsClosed"));
            }
            
            if (stream == null)
                throw new ArgumentNullException("stream");

            if (_readChunk == null)
            {
                if (_chunks == null)
                    return;

                _readChunk = _chunks;
                _readOffset = 0;
            }

            byte[] chunkBuffer = _readChunk.Buffer;
            int chunkSize = chunkBuffer.Length;
            if (_readChunk.Next == null)
                chunkSize = _writeOffset;

            // following code mirrors Read() logic (_readChunk/_readOffset should
            //   point just past last byte of last chunk when done)

            for (;;) // loop until end of chunks is found
            {
                if (_readOffset == chunkSize)
                {
                    // exit if no more chunks are currently available
                    if (_readChunk.Next == null)
                        break;
                        
                    _readChunk = _readChunk.Next;
                    _readOffset = 0;
                    chunkBuffer = _readChunk.Buffer;
                    chunkSize = chunkBuffer.Length;
                    if (_readChunk.Next == null)
                        chunkSize = _writeOffset;
                }

                int writeCount = chunkSize - _readOffset;
                stream.Write(chunkBuffer, _readOffset, writeCount);
                _readOffset = chunkSize;
            }
                
        } // WriteTo
Exemple #26
0
 public SpriteBatch(uint maxSprites)
 {
     _sprites = MemoryUtils.AllocateBlock <NormalSprite>(maxSprites);
 }
Exemple #27
0
 /// <summary>
 /// Creates a new <see cref="Text"/>.
 /// </summary>
 public Text()
 {
     MainChunk = new MemoryChunk(8010, 2);
     Chunks    = new MemoryChunk[] { MainChunk };
 }
Exemple #28
0
        /// <inheritdoc/>
        public override int Read(byte[] buffer, int offset, int count)
        {
            Guard.NotNull(buffer, nameof(buffer));
            Guard.MustBeGreaterThanOrEqualTo(offset, 0, nameof(offset));
            Guard.MustBeGreaterThanOrEqualTo(count, 0, nameof(count));
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException($"{offset} subtracted from the buffer length is less than {count}");
            }

            this.EnsureNotDisposed();

            if (this.readChunk == null)
            {
                if (this.memoryChunk == null)
                {
                    return(0);
                }

                this.readChunk  = this.memoryChunk;
                this.readOffset = 0;
            }

            byte[] chunkBuffer = this.readChunk.Buffer;
            int    chunkSize   = this.readChunk.Length;

            if (this.readChunk.Next == null)
            {
                chunkSize = this.writeOffset;
            }

            int bytesRead = 0;

            while (count > 0)
            {
                if (this.readOffset == chunkSize)
                {
                    // Exit if no more chunks are currently available
                    if (this.readChunk.Next == null)
                    {
                        break;
                    }

                    this.readChunk  = this.readChunk.Next;
                    this.readOffset = 0;
                    chunkBuffer     = this.readChunk.Buffer;
                    chunkSize       = this.readChunk.Length;
                    if (this.readChunk.Next == null)
                    {
                        chunkSize = this.writeOffset;
                    }
                }

                int readCount = Math.Min(count, chunkSize - this.readOffset);
                Buffer.BlockCopy(chunkBuffer, this.readOffset, buffer, offset, readCount);
                offset          += readCount;
                count           -= readCount;
                this.readOffset += readCount;
                bytesRead       += readCount;
            }

            return(bytesRead);
        }
		public byte[] ToArray()
		{
			int size = (int)Length;	// will throw if stream is closed
			byte[] copy = new byte[size];

			MemoryChunk backupReadChunk = _readChunk;
			int backupReadOffset = _readOffset;

			_readChunk = _chunks;
			_readOffset = 0;
			Read(copy, 0, size);

			_readChunk = backupReadChunk;
			_readOffset = backupReadOffset;

			return copy;
		}
		public void WriteTo(Stream stream)
		{
			// check object state
			if (_closed)
				throw new IOException("Stream is closed");

			// parameters validation
			if (stream == null)
				throw new ArgumentNullException("stream");

			if (_readChunk == null)
			{
				if (_chunks == null)
					return;	// there is no data available
				_readChunk = _chunks;
				_readOffset = 0;
			}

			byte[] chunkBuffer = _readChunk.Buffer;
			int chunkSize = (_readChunk.Next == null ? _writeOffset : chunkBuffer.Length);

			while (true)
			{
				if (_readOffset == chunkSize)
				{
					if (_readChunk.Next != null)
					{
						// jump to the next chunk
						_readChunk = _readChunk.Next;
						_readOffset = 0;

						chunkBuffer = _readChunk.Buffer;
						chunkSize = (_readChunk.Next == null ? _writeOffset : chunkBuffer.Length);
					}
					else
					{
						// there is no more chunks currently available
						break;
					}
				}

				int writeCount = chunkSize - _readOffset;
				stream.Write(chunkBuffer, _readOffset, writeCount);
				_readOffset += writeCount;
			}
		}
		private void ReleaseMemoryChunks(MemoryChunk chunk)
		{
			if (_bufferPool is FakeByteBufferPool)
				return;	// until we are using buffer pool there is nothing to do

			while (chunk != null)
			{
				_bufferPool.ReturnBuffer(chunk.Buffer);
				chunk = chunk.Next;
			}
		}
		private int GetChunkIndex(MemoryChunk chunkToFind)
		{
			int index = 0;
			for (MemoryChunk chunk = _chunks; chunk != null && chunk != chunkToFind; chunk = chunk.Next, ++index);
			return index;
		}
		public override void WriteByte(byte value)
		{
			// check object state
			if (_closed)
				throw new IOException("Stream is closed");

			if (_chunks == null)
			{
				// allocate first chunk
				_chunks = AllocateMemoryChunk();
				_writeChunk = _chunks;
				_writeOffset = 0;
			}

			byte[] chunkBuffer = _writeChunk.Buffer;
			int chunkSize = chunkBuffer.Length;

			if (_writeOffset == chunkSize)
			{
				// allocate a new chunk if the current one is full
				_writeChunk.Next = AllocateMemoryChunk();
				_writeChunk = _writeChunk.Next;
				_writeOffset = 0;

				chunkBuffer = _writeChunk.Buffer;
			}

			chunkBuffer[_writeOffset++] = value;
		}
        } // WriteByte


        // copy entire buffer into an array
        public virtual byte[] ToArray() 
        {
            int length = (int)Length; // this will throw if stream is closed
            byte[] copy = new byte[Length];

            MemoryChunk backupReadChunk = _readChunk;
            int backupReadOffset = _readOffset;

            _readChunk = _chunks;
            _readOffset = 0;            
            Read(copy, 0, length);

            _readChunk = backupReadChunk;
            _readOffset = backupReadOffset;           
            
            return copy;
        } // ToArray      
Exemple #35
0
        private void free(int partitionId, int addr, int size)
        {
            MemoryChunk memoryChunk = new MemoryChunk(addr, size);

            freeMemoryChunks[partitionId].add(memoryChunk);
        }
        } // WriteTo



        private MemoryChunk AllocateMemoryChunk()
        {
            MemoryChunk chunk = new MemoryChunk();
            chunk.Buffer = _bufferPool.GetBuffer();
            chunk.Next = null;

            return chunk;
        } // AllocateMemoryChunk
Exemple #37
0
 internal ScatterGatherStream()
 {
     currentChunk         = headChunk = null;
     currentOffset        = endOffset = 0;
     currentChunkStartPos = 0;
 }
 private MemoryChunk AllocateMemoryChunk(int newSize) {
     if (newSize > nextChunkLength) {
         nextChunkLength = newSize;
     }
     MemoryChunk newChunk = new MemoryChunk(nextChunkLength);
     if (Empty) {
         headChunk = newChunk;
     }
     //
     // next time allocate twice as much. check fot possible overflows
     //
     nextChunkLength *= 2;
     //
     // update number of chunks in the linked list
     //
     chunkCount++;
     GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::AllocateMemoryChunk() chunkCount:" + chunkCount.ToString() + " nextChunkLength:" + nextChunkLength.ToString());
     return newChunk;
 }
Exemple #39
0
		public void Clear ()
		{
			for (var ptr = first; ptr != null; ptr = ptr.next)
				ptr.Dispose ();
			first = last = null;
		}
 protected override void Dispose(bool disposing) 
 {
     try {
         _bClosed = true;
         if (disposing)
             ReleaseMemoryChunks(_chunks);
         _chunks = null;
         _writeChunk = null;
         _readChunk = null;
     }
     finally {
         base.Dispose(disposing);
     }
 } // Close
        } // Flush

        
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_bClosed)
            {                
                throw new RemotingException(
                CoreChannel.GetResourceString("Remoting_Stream_StreamIsClosed"));
            }        
            
            if (_readChunk == null)
            {
                if (_chunks == null)
                    return 0;
                _readChunk = _chunks;
                _readOffset = 0;
            }
            
            byte[] chunkBuffer = _readChunk.Buffer;
            int chunkSize = chunkBuffer.Length;
            if (_readChunk.Next == null)
                chunkSize = _writeOffset;

            int bytesRead = 0;
        
            while (count > 0)
            {
                if (_readOffset == chunkSize)
                {
                    // exit if no more chunks are currently available
                    if (_readChunk.Next == null)
                        break;
                        
                    _readChunk = _readChunk.Next;
                    _readOffset = 0;
                    chunkBuffer = _readChunk.Buffer;
                    chunkSize = chunkBuffer.Length;
                    if (_readChunk.Next == null)
                        chunkSize = _writeOffset;
                }

                int readCount = Math.Min(count, chunkSize - _readOffset);
                Buffer.BlockCopy(chunkBuffer, _readOffset, buffer, offset, readCount);
                offset += readCount;
                count -= readCount;
                _readOffset += readCount;
                bytesRead += readCount;
            }

            return bytesRead;
        } // Read
Exemple #42
0
 /// <inheritdoc/>
 public void Initialize()
 {
     MainChunk = new MemoryChunk(Configuration.BootSize + (Id * Configuration.CoreMemorySize), Configuration.CoreMemorySize);
     Microcode = CommandProvider.GetCommandsAsync().ToDictionary(m => m.Name);
     MountChunk(MainChunk);
 }
Exemple #43
0
 /// <summary>
 /// Creates a new <see cref="Keyboard"/>.
 /// </summary>
 public Keyboard()
 {
     MainChunk = new MemoryChunk(8003, 1);
     Chunks    = new MemoryChunk[] { MainChunk };
 }
Exemple #44
0
 public void DismountChunk(MemoryChunk chunk)
 {
     chunks.Remove(chunk);
     storage.Remove(chunk);
 }
Exemple #45
0
        public override long Seek(long offset, SeekOrigin loc)
        {
            MemoryChunk chunk          = null;;
            long        relativeOffset = 0;
            long        absoluteOffset = 0;

            if (loc == SeekOrigin.Begin)
            {
                absoluteOffset = offset;
                if (offset >= currentChunkStartPos)
                {
                    chunk          = currentChunk;
                    relativeOffset = offset - currentChunkStartPos;
                }
                else
                {
                    chunk          = headChunk;
                    relativeOffset = absoluteOffset;
                }
            }
            else if (loc == SeekOrigin.Current)
            {
                absoluteOffset = offset + currentOffset + currentChunkStartPos;
                if ((offset + currentOffset) > 0)
                {
                    chunk          = currentChunk;
                    relativeOffset = offset + currentOffset;
                }
                else
                {
                    chunk          = headChunk;
                    relativeOffset = absoluteOffset;
                }
            }
            else if (loc == SeekOrigin.End)
            {
                MemoryChunk endChunk;
                long        length = GetLengthInternal(out endChunk);
                absoluteOffset = offset + length;
                if ((offset + endOffset) > 0)
                {
                    relativeOffset = offset + endOffset;
                    chunk          = endChunk;
                }
                else if (absoluteOffset >= currentChunkStartPos)
                {
                    chunk          = currentChunk;
                    relativeOffset = absoluteOffset - currentChunkStartPos;
                }
                else
                {
                    chunk          = headChunk;
                    relativeOffset = absoluteOffset;
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("loc");
            }

            if (relativeOffset < 0 || relativeOffset > MemStreamMaxLength)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            long remaining = relativeOffset;

            while (chunk.Next != null)
            {
                if (remaining < chunk.Buffer.Length)
                {
                    currentChunk         = chunk;
                    currentOffset        = (int)remaining;
                    currentChunkStartPos = absoluteOffset - currentOffset;
                    remaining            = -1;
                    break;
                }
                remaining -= chunk.Buffer.Length;
                chunk      = chunk.Next;
            }

            if (remaining >= 0)
            {
                if (remaining <= chunk.Buffer.Length)
                {
                    currentChunk = chunk;
                }
                else
                {
                    currentChunk = chunk.Next = AllocateMemoryChunk(2 * remaining);
                    endOffset    = 0;
                }
                currentOffset        = (int)remaining;
                currentChunkStartPos = absoluteOffset - currentOffset;
                SyncEndOffset();
            }

            return(absoluteOffset);
        }
Exemple #46
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="AddMemoryRequest" /> class.
 /// </summary>
 /// <param name="memoryChunk">The memory chunk.</param>
 /// <exception cref="ArgumentNullException">memoryChunk</exception>
 public AddMemoryRequest(MemoryChunk memoryChunk)
 {
     MemoryChunk = memoryChunk ?? throw new ArgumentNullException(nameof(memoryChunk));
 }
Exemple #47
0
 public EntityManager(WorldConfiguration config)
 {
     _worldId      = config.Id;
     _entityIds    = new IdContainer(config.MaxEntities);
     _relationship = MemoryUtils.AllocateBlock <Relationship>(config.MaxEntities, true);
 }
        } // Read

        public override int ReadByte()
        {
            if (_bClosed)
            {                
                throw new RemotingException(
                    CoreChannel.GetResourceString("Remoting_Stream_StreamIsClosed"));
            }        
            
            if (_readChunk == null)
            {
                if (_chunks == null)
                    return 0;
                _readChunk = _chunks;
                _readOffset = 0;
            }
            
            byte[] chunkBuffer = _readChunk.Buffer;
            int chunkSize = chunkBuffer.Length;
            if (_readChunk.Next == null)
                chunkSize = _writeOffset;

            if (_readOffset == chunkSize)
            {
                // exit if no more chunks are currently available
                if (_readChunk.Next == null)
                    return -1;
                        
                _readChunk = _readChunk.Next;
                _readOffset = 0;
                chunkBuffer = _readChunk.Buffer;
                chunkSize = chunkBuffer.Length;
                if (_readChunk.Next == null)
                    chunkSize = _writeOffset;
            }

            return chunkBuffer[_readOffset++];
        } // ReadByte
		public override void Write(byte[] buffer, int offset, int count)
		{
			// check object state
			if (_closed)
				throw new IOException("Stream is closed");

			// parameters validation
			if (buffer == null)
				throw new ArgumentNullException("buffer");
			if (offset < 0 || offset > buffer.Length)
				throw new ArgumentOutOfRangeException("offset");
			if (count < 0 || count > buffer.Length - offset)
				throw new ArgumentOutOfRangeException("count");

			if (_chunks == null)
			{
				// allocate first chunk
				_chunks = AllocateMemoryChunk();
				_writeChunk = _chunks;
				_writeOffset = 0;
			}

			byte[] chunkBuffer = _writeChunk.Buffer;
			int chunkSize = chunkBuffer.Length;

			while (count > 0)
			{
				if (_writeOffset == chunkSize)
				{
					// allocate a new chunk if the current one is full
					_writeChunk.Next = AllocateMemoryChunk();
					_writeChunk = _writeChunk.Next;
					_writeOffset = 0;

					chunkBuffer = _writeChunk.Buffer;
					chunkSize = chunkBuffer.Length;
				}

				// write chunk content
				int writeCount = chunkSize - _writeOffset;
				if (writeCount > count) writeCount = count;
				Buffer.BlockCopy(buffer, offset, chunkBuffer, _writeOffset, writeCount);
				offset += writeCount;
				count -= writeCount;
				_writeOffset += writeCount;
			}
		}
        } // ReadByte
                
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_bClosed)
            {                
                throw new RemotingException(
                    CoreChannel.GetResourceString("Remoting_Stream_StreamIsClosed"));
            }
        
            if (_chunks == null)
            {
                _chunks = AllocateMemoryChunk();
                _writeChunk = _chunks;
                _writeOffset = 0;
            }            

            byte[] chunkBuffer = _writeChunk.Buffer;
            int chunkSize = chunkBuffer.Length;
    
            while (count > 0)
            {
                if (_writeOffset == chunkSize)
                {
                    // allocate a new chunk if the current one is full
                    _writeChunk.Next = AllocateMemoryChunk();
                    _writeChunk = _writeChunk.Next;
                    _writeOffset = 0;
                    chunkBuffer = _writeChunk.Buffer;
                    chunkSize = chunkBuffer.Length;
                }
                             
                int copyCount = Math.Min(count, chunkSize - _writeOffset);
                Buffer.BlockCopy(buffer, offset, chunkBuffer, _writeOffset, copyCount);
                offset += copyCount;
                count -= copyCount;
                _writeOffset += copyCount;
            }
            
        } // Write
        } // Write

        public override void WriteByte(byte value)
        {
            if (_bClosed)
            {                
                throw new RemotingException(
                    CoreChannel.GetResourceString("Remoting_Stream_StreamIsClosed"));
            }
        
            if (_chunks == null)
            {
                _chunks = AllocateMemoryChunk();
                _writeChunk = _chunks;
                _writeOffset = 0;
            }            

            byte[] chunkBuffer = _writeChunk.Buffer;
            int chunkSize = chunkBuffer.Length;
    
            if (_writeOffset == chunkSize)
            {
                // allocate a new chunk if the current one is full
                _writeChunk.Next = AllocateMemoryChunk();
                _writeChunk = _writeChunk.Next;
                _writeOffset = 0;
                chunkBuffer = _writeChunk.Buffer;
                chunkSize = chunkBuffer.Length;
            }
            
            chunkBuffer[_writeOffset++] = value;
        } // WriteByte
Exemple #52
0
		public BufferOffsetSize StealBuffer ()
		{
			if (IsSingle) {
				var retval = first;
				first = last = null;
				return retval;
			}

			return GetBuffer ();
		}
Exemple #53
0
 /// <summary>
 /// Creates a new <see cref="Display"/>.
 /// </summary>
 public Display()
 {
     MainChunk = new MemoryChunk(8004, 6);
     Chunks    = new MemoryChunk[] { MainChunk };
 }
Exemple #54
0
        // note: we're only looking at user memory, so 0x08800000 - 0x0A000000
        // this is mainly to make it fit on one console line
        public virtual void dumpSysMemInfo()
        {
            const int MEMORY_SIZE = 0x1800000;
            const int SLOT_COUNT  = 64;            // 0x60000
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'sealed override':
//ORIGINAL LINE: sealed override int SLOT_SIZE = MEMORY_SIZE / SLOT_COUNT;
            int SLOT_SIZE = MEMORY_SIZE / SLOT_COUNT;             // 0x60000

            bool[] allocated      = new bool[SLOT_COUNT];
            bool[] fragmented     = new bool[SLOT_COUNT];
            int    allocatedSize  = 0;
            int    fragmentedSize = 0;

            for (IEnumerator <SysMemInfo> it = blockList.Values.GetEnumerator(); it.MoveNext();)
            {
                SysMemInfo info = it.Current;
                for (int i = info.addr; i < info.addr + info.size; i += SLOT_SIZE)
                {
                    if (i >= 0x08800000 && i < 0x0A000000)
                    {
                        allocated[(i - 0x08800000) / SLOT_SIZE] = true;
                    }
                }
                allocatedSize += info.size;
            }

            for (MemoryChunk memoryChunk = freeMemoryChunks[USER_PARTITION_ID].LowMemoryChunk; memoryChunk != null; memoryChunk = memoryChunk.next)
            {
                for (int i = memoryChunk.addr; i < memoryChunk.addr + memoryChunk.size; i += SLOT_SIZE)
                {
                    if (i >= 0x08800000 && i < 0x0A000000)
                    {
                        fragmented[(i - 0x08800000) / SLOT_SIZE] = true;
                    }
                }
                fragmentedSize += memoryChunk.size;
            }

            StringBuilder allocatedDiagram = new StringBuilder();

            allocatedDiagram.Append("[");
            for (int i = 0; i < SLOT_COUNT; i++)
            {
                allocatedDiagram.Append(allocated[i] ? "X" : " ");
            }
            allocatedDiagram.Append("]");

            StringBuilder fragmentedDiagram = new StringBuilder();

            fragmentedDiagram.Append("[");
            for (int i = 0; i < SLOT_COUNT; i++)
            {
                fragmentedDiagram.Append(fragmented[i] ? "X" : " ");
            }
            fragmentedDiagram.Append("]");

            DumpDebugState.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            DumpDebugState.log(string.Format("Allocated memory:  {0:X8} {1:D} bytes", allocatedSize, allocatedSize));
            DumpDebugState.log(allocatedDiagram.ToString());
            DumpDebugState.log(string.Format("Fragmented memory: {0:X8} {1:D} bytes", fragmentedSize, fragmentedSize));
            DumpDebugState.log(fragmentedDiagram.ToString());

            DumpDebugState.log("Free list: " + DebugFreeMem);
            DumpDebugState.log("Allocated blocks:\n" + DebugAllocatedMem + "\n");
        }
Exemple #55
0
 public EntityInfoManager(WorldConfiguration config)
 {
     Logger.Trace <EntityInfoManager>($"Creating entity info for {config.MaxEntities} entities.");
     _entityInfos = MemoryUtils.AllocateBlock <EntityInfo>(config.MaxEntities, true);
     _worldId     = config.Id;
 }