public D DoStep() { if (State.Equals(EndState)) { return(State); } var instruction = Instructions[State][Current.Value]; if (instruction.IsEmpty()) { throw new DataException("Empty instruction"); } if (instruction.ChangeValue) { Current.Value = instruction.NewValue; } if (instruction.ChangeState) { State = instruction.NewState; } if (instruction.ReaderStep > 0) { if (Current.Next == null) { Data.AddAfter(Current, DefaultValue); } Current = Current.Next; } else if (instruction.ReaderStep < 0) { if (Current.Previous == null) { Data.AddBefore(Current, DefaultValue); } Current = Current.Previous; } return(State); }
public HeapData Malloc(int size) { if (size == 0) { throw new XiVMError("Malloc space of size 0 is not supported"); } if (Size + size > SizeLimit) { GarbageCollector.CollectGarbage(); if (Size + size > SizeLimit) { throw new XiVMError("Heap overflow"); } } // Best Fit LinkedListNode <HeapData> best = null; int bestFragmentSize = 0; LinkedListNode <HeapData> cur = Data.First; while (cur != null) { if (cur.Next != null) { int fragmentSize = (int)(cur.Next.Value.Offset - (cur.Value.Offset + cur.Value.Data.Length)); if (fragmentSize >= size) { // 可以填入 if (best == null || bestFragmentSize > fragmentSize) { // best fit best = cur; bestFragmentSize = fragmentSize; } } } cur = cur.Next; } HeapData ret = null; if (best == null) { // 未找到内碎片,在末尾添加 ret = new HeapData( Data.Count == 0 ? 0 : Data.Last.Value.Offset + (uint)Data.Last.Value.Data.Length, new byte[size]); Data.AddLast(ret); } else { // fit ret = new HeapData(best.Value.Offset + (uint)best.Value.Data.Length, new byte[size]); Data.AddAfter(best, ret); } DataMap.Add(ret.Offset, ret); Size += size; if (Size > MaxSize) { MaxSize = size; } return(ret); }