예제 #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;
 }