コード例 #1
0
 public void Set(Pointer start, uint size, byte value)
 {
     for (var i = start.Address; i < start.Address + size; i++)
     {
         _memory[i] = value;
     }
 }
コード例 #2
0
 public void Copy(Pointer from, Pointer to, uint size)
 {
     var fromAddr = from.Address;
     var toAddr = to.Address;
     for (int i = 0; i < size; i++)
     {
         _memory[toAddr + i] = _memory[fromAddr + i];
     }
 }
コード例 #3
0
 public void Free(Pointer address)
 {
     Pointer blockStart = GetBlockStart(address);
     uint blockCount = GetBlockSize(blockStart);
     for (var i = 0; i < blockCount; i++)
     {
         SetBlockStatus(blockStart, BlockStatusFree);
         blockStart = AddBlock(blockStart);
     }
 }
コード例 #4
0
 public uint ReadUint(Pointer address)
 {
     byte[] data = Read(address, 4);
     uint result = 0;
     for (int i = 3; i >= 0; i--)
     {
         result <<= 8;
         result |= data[i];
     }
     return result;
 }
コード例 #5
0
        public Pointer Reallocate(Pointer address, uint newSize)
        {
            if (newSize == 0)
            {
                Free(address);
                return Pointer.Null;
            }

            Pointer blockStart = GetBlockStart(address);

            uint allocatedBlocks = GetBlockSize(blockStart);
            uint requiredBlocks = GetRequiredBlocks(newSize);

            if (allocatedBlocks >= requiredBlocks)
            {
                TruncAllocated(blockStart, requiredBlocks);
                return address;
            }
            else
            {
                uint freeBlocks = GetFreeBlockCountAfter(address);
                if (allocatedBlocks + freeBlocks >= requiredBlocks)
                {
                    ExpandAllocated(blockStart, requiredBlocks);
                    return address;
                }
                else
                {
                    Pointer newBlocks = Allocate(newSize);
                    if (newBlocks != null)
                    {
                        _memory.Copy(address, newBlocks, allocatedBlocks * _blockSize - SystemDataSize);
                        Free(address);
                        return GetBlockDataStart(newBlocks);
                    }
                    else
                    {
                        return Pointer.Null;
                    }
                }
            }
        }
コード例 #6
0
 private void ExpandAllocated(Pointer address, uint newBlockCount)
 {
     SetBlockSize(address, newBlockCount);
 }
コード例 #7
0
        private void TruncAllocated(Pointer address, uint newBlockCount)
        {
            uint allocatedBlocks = GetBlockSize(address);
            SetBlockSize(address, newBlockCount);

            Pointer freeBlock = AddBlocks(address, newBlockCount);
            for (uint i = newBlockCount; i < allocatedBlocks; i++)
            {
                SetBlockStatus(freeBlock, BlockStatusFree);
                freeBlock = AddBlock(freeBlock);
            }
        }
コード例 #8
0
 public byte[] Read(Pointer start, uint size)
 {
     return _memory.Skip((int)(start.Address)).Take((int)size).ToArray();
 }
コード例 #9
0
 private void SetBlockSize(Pointer address, uint size)
 {
     _memory.WriteUint(address, size);
 }
コード例 #10
0
 private void SetBlockStatus(Pointer address, uint status)
 {
     _memory.WriteUint(address, status);
 }
コード例 #11
0
        private uint GetFreeBlockCountAfter(Pointer address)
        {
            uint freeBlockCount = 0;
            uint allocatedBlocks = GetBlockSize(address);
            Pointer currentBlockAddress = AddBlocks(address, allocatedBlocks);

            while (GetBlockStatus(currentBlockAddress) == BlockStatusFree)
            {
                freeBlockCount++;
                currentBlockAddress = AddBlock(currentBlockAddress);
            }
            return freeBlockCount;
        }
コード例 #12
0
 private void PrepareMemory()
 {
     _blockCount = _memory.Size / _blockSize;
     Pointer blockStart = _memory.GetPointer(0);
     for (int i = 0; i < _blockCount; i++)
     {
         SetBlockStatus(blockStart, BlockStatusFree);
     }
     _endBlock = _memory.GetPointer(_blockSize * _blockCount);
 }
コード例 #13
0
 private Pointer GetBlockStart(Pointer dataStart)
 {
     return dataStart.Sub(SystemDataSize);
 }
コード例 #14
0
 private uint GetBlockStatus(Pointer address)
 {
     return _memory.ReadUint(address);
 }
コード例 #15
0
 public byte Read(Pointer address)
 {
     return _memory[address.Address];
 }
コード例 #16
0
 private Pointer GetBlockDataStart(Pointer blockStart)
 {
     return blockStart.Add(SystemDataSize);
 }
コード例 #17
0
 private Pointer AddBlocks(Pointer address, uint count)
 {
     return address.Add(_blockSize * count);
 }
コード例 #18
0
 private Pointer AddBlock(Pointer address)
 {
     return address.Add(_blockSize);
 }
コード例 #19
0
 public void WriteUint(Pointer address, uint value)
 {
     uint input = value;
     byte[] data = new byte[4];
     for (int i = 0; i < 4; i++)
     {
         data[i] = (byte)(input & 0xFF);
         input >>= 8;
     }
     Write(address, data);
 }
コード例 #20
0
 public void Write(Pointer start, byte[] data)
 {
     for (int i = 0; i < data.Length; i++)
     {
         _memory[start.Address + i] = data[i];
     }
 }
コード例 #21
0
 public void Write(Pointer address, byte value)
 {
     _memory[address.Address] = value;
 }