//调入进程后更新空闲区表信息 private void UpdateFreeMemList_AddProcess(MemBlock freeMemBlock, int size) { //若无需删除整个空闲块结点,更改起点与大小 if (freeMemBlock.blockSize != size) { freeMemBlock.starting += size; freeMemBlock.blockSize -= size; } //若要删除空闲区表头结点,将下一个结点当做头结点 else if (freeMemBlock == freeMemList.first) { freeMemList.first = freeMemBlock.next; if (freeMemList.first != null) { freeMemList.first.last = null; } } //删除某个空闲块结点 else { MemBlock lastBlock = freeMemBlock.last; lastBlock.next = freeMemBlock.next; if (lastBlock.next != null) { lastBlock.next.last = lastBlock; } } //更新空闲区表信息 PrintFreeMemList(); }
//释放进程后更新占用表 private void UpdateUsedMemList_Release(int start, int size) { //若删去整个头结点,修改新的头结点 if (usedMemList.first.starting == start && usedMemList.first.blockSize == size) { usedMemList.first = usedMemList.first.next; } else { MemBlock block = usedMemList.first; while (!(block.starting <= start && block.starting + block.blockSize > start)) { block = block.next; } //若要删去某个节点 if (start == block.starting && size == block.blockSize) { block.last.next = block.next; if (block.next != null) { block.next.last = block.last; } } //若要删去某个节点前半部分,更新起址,大小 else if (start == block.starting) { block.starting += size; block.blockSize -= size; } //若要删去某个节点后半部分,更新大小 else if (start + size == block.starting + block.blockSize) { block.blockSize -= size; } //删去某个节点的中间部分,创建新的结点并连接 else { MemBlock newBlock = new MemBlock(start + size, block.starting + block.blockSize - start - size); block.blockSize = start - block.starting; newBlock.next = block.next; if (newBlock.next != null) { newBlock.next.last = newBlock; } block.next = newBlock; newBlock.last = block; } } PrintUsedMemList(); }
//打印空闲表信息 private void PrintFreeMemList() { freeMemListTextBox.Text = string.Empty; if (freeMemList.first == null) { freeMemListTextBox.AppendText("无空闲区"); } else { int i = 1; for (MemBlock block = freeMemList.first; block != null; block = block.next, i++) { freeMemListTextBox.AppendText( "空闲区" + i + " " + "起址:" + block.starting + "K " + "长度:" + block.blockSize + "K\r\n"); } } }
//调入进程后更新占用区表信息 private void UpdateUsedMemList_AddProcess(int start, int size) { //若占用表为空,创建头结点 if (usedMemList.first == null) { usedMemList.first = new MemBlock(start, size); } else { MemBlock block = usedMemList.first; //若新表项在表头前,更新头结点 if (start < usedMemList.first.starting) { MemBlock newBlock = new MemBlock(start, size); newBlock.next = block; block.last = newBlock; usedMemList.first = newBlock; block = newBlock; } //新表项紧挨某项后方(增大某项体积) else { while (block != null && block.starting + block.blockSize != start) { block = block.next; } block.blockSize += size; } //检测是否要与下一个结点项合并 if (block.next != null && block.starting + block.blockSize == block.next.starting) { block.blockSize += block.next.blockSize; block.next = block.next.next; if (block.next != null) { block.next.last = block; } } } //更新占用表信息 PrintUsedMemList(); }
//获取调入位置,若无调入空间,返回false private bool GetPositon(out int p, int size) { p = -1; MemBlock freeMemBlock = null; //首次适应算法 if (algorithm) { freeMemBlock = freeMemList.first; while (freeMemBlock != null) { if (freeMemBlock.blockSize >= size) { p = freeMemBlock.starting; UpdateFreeMemList_AddProcess(freeMemBlock, size); break; } freeMemBlock = freeMemBlock.next; } } //最佳适应算法 else { int maxSize = 0; MemBlock block = freeMemList.first; //寻找最大且足够大的块 while (block != null) { if (block.blockSize >= size && block.blockSize > maxSize) { freeMemBlock = block; maxSize = block.blockSize; } block = block.next; } if (freeMemBlock != null) { p = freeMemBlock.starting; UpdateFreeMemList_AddProcess(freeMemBlock, size); } } return(p >= 0); }
public FreeMemList(MemBlock fst) { first = fst; }
//释放进程后更新空闲表 private void UpdateFreeMemList_Release(int start, int size) { //若空闲表为空,创建头结点 if (freeMemList.first == null) { freeMemList.first = new MemBlock(start, size); } else { //储存已修改的结点及其上一项,下一项的结点信息,便于执行合并操作 MemBlock block = new MemBlock(start, size); MemBlock lastBlock = freeMemList.first; MemBlock nextBlock = null; //若新表项添加在头结点前,更改头结点 if (start < lastBlock.starting) { block.next = lastBlock; lastBlock.last = block; freeMemList.first = block; lastBlock = block; block = lastBlock.next; nextBlock = block.next; } else { //寻找插入的位置(插在哪一结点后方) while (lastBlock.next != null && lastBlock.next.starting < start) { lastBlock = lastBlock.next; } //若该结点不是尾节点,连接下一项 if (lastBlock.next != null) { nextBlock = lastBlock.next; block.next = nextBlock; nextBlock.last = block; } //连接上一项 lastBlock.next = block; block.last = lastBlock; } //合并操作 //检查是否要与上一项合并 if (lastBlock != null) { if (lastBlock.starting + lastBlock.blockSize == block.starting) { lastBlock.blockSize += block.blockSize; lastBlock.next = nextBlock; if (nextBlock != null) { nextBlock.last = lastBlock; } block = lastBlock; } } //检查是否要与下一项合并 if (nextBlock != null) { if (block.starting + block.blockSize == nextBlock.starting) { block.blockSize += nextBlock.blockSize; block.next = nextBlock.next; if (block.next != null) { block.next.last = block; } } } } //更新空闲表信息 PrintFreeMemList(); }