public void Start(Process process, MemoryBitmapEntry head) { var entry = head; while (entry != null) { if (entry.Type is MemoryBitmapEntryType.Hole && entry.Value >= process.Size) { if (entry.Value == process.Size) // Exact fit { entry.Type = MemoryBitmapEntryType.Process; entry.Process = process; } else { MemoryBitmapEntry holeEntry = new(MemoryBitmapEntryType.Hole, entry.Value - process.Size, entry.Next); entry.Next = holeEntry; entry.Type = MemoryBitmapEntryType.Process; entry.Value = process.Size; entry.Process = process; } return; } entry = entry.Next; } throw new Exception("Can't allocate memory"); }
public static void MergeHoles(MemoryBitmapEntry previous, MemoryBitmapEntry current) { if (previous is not null && previous.Type is MemoryBitmapEntryType.Hole) { previous.Value += current.Value; previous.Next = current.Next; MergeHoles(null, previous); return; } if (current.Next is not null && current.Next.Type is MemoryBitmapEntryType.Hole) { MergeHoles(current, current.Next); return; } }
public void Start(Process process, MemoryBitmapEntry head) { MemoryBitmapEntry entry = head; MemoryBitmapEntry min = null; while (entry != null) { if (entry.Type is MemoryBitmapEntryType.Hole && entry.Value >= process.Size) { if (min is null) { min = entry; } if (entry.Value < min.Value) { min = entry; } if (min.Value == process.Size) { break; } } entry = entry.Next; } if (min is null) { throw new Exception("Can't allocate memory"); } if (min.Value == process.Size) { min.Type = MemoryBitmapEntryType.Process; min.Process = process; } else { MemoryBitmapEntry holeEntry = new(MemoryBitmapEntryType.Hole, min.Value - process.Size, min.Next); min.Next = holeEntry; min.Type = MemoryBitmapEntryType.Process; min.Value = process.Size; min.Process = process; } }
private void OnProcessKilled(object sender, ProcessKilledEventArgs eventArgs) { MemoryBitmapEntry previous = null, current = Head; while (current != null) { if (current.Type is MemoryBitmapEntryType.Process && current.Process.Pid == eventArgs.Pid) { current.Type = MemoryBitmapEntryType.Hole; current.Process.Killed -= OnProcessKilled; current.Process = null; HoleMerger.MergeHoles(previous, current); } previous = current; current = current.Next; } }
public static string BuildView(MemoryBitmapEntry head) { StringBuilder view = new(); var current = head; List <MemoryBitmapEntry> entries = new(); while (current != null) { entries.Add(current); current = current.Next; } view.AppendJoin("=>", entries.Select(entry => { var symbol = entry.Type is MemoryBitmapEntryType.Hole ? 'H' : 'P'; return($"[{symbol}|{entry.Value}]"); })); return(view.ToString()); }
public BitmapMemoryManager(long memoryAmount) { Head = new MemoryBitmapEntry(MemoryBitmapEntryType.Hole, memoryAmount); }