public void Set(Pointer start, uint size, byte value) { for (var i = start.Address; i < start.Address + size; i++) { _memory[i] = value; } }
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]; } }
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); } }
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; }
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; } } } }
private void ExpandAllocated(Pointer address, uint newBlockCount) { SetBlockSize(address, newBlockCount); }
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); } }
public byte[] Read(Pointer start, uint size) { return _memory.Skip((int)(start.Address)).Take((int)size).ToArray(); }
private void SetBlockSize(Pointer address, uint size) { _memory.WriteUint(address, size); }
private void SetBlockStatus(Pointer address, uint status) { _memory.WriteUint(address, status); }
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; }
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); }
private Pointer GetBlockStart(Pointer dataStart) { return dataStart.Sub(SystemDataSize); }
private uint GetBlockStatus(Pointer address) { return _memory.ReadUint(address); }
public byte Read(Pointer address) { return _memory[address.Address]; }
private Pointer GetBlockDataStart(Pointer blockStart) { return blockStart.Add(SystemDataSize); }
private Pointer AddBlocks(Pointer address, uint count) { return address.Add(_blockSize * count); }
private Pointer AddBlock(Pointer address) { return address.Add(_blockSize); }
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); }
public void Write(Pointer start, byte[] data) { for (int i = 0; i < data.Length; i++) { _memory[start.Address + i] = data[i]; } }
public void Write(Pointer address, byte value) { _memory[address.Address] = value; }