天津理工大学_操作系统_存储器的分配与回收算法实现_实验报告

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验报告学院(系)名称:计算机与通信工程学院

【实验过程记录(源程序、测试用例、测试结果及心得体会等)】源程序:

MemoryBlock.java:

//内存块类,包含各种操作

public class MemoryBlock {

static final int BLOCK_SIZE = 4096;

private int baseBlock; //内存块基地址

private int blockNum; //大小

private boolean inUse; //是否已分配

private MemoryBlock prev, next;

public MemoryBlock(int blockNum) {

this.baseBlock = 0;

this.blockNum = blockNum;

inUse = false;

prev = null;

next = null;

}

public MemoryBlock(int base, int blockNum) {

this.baseBlock = base;

this.blockNum = blockNum;

inUse = false;

prev = null;

next = null;

}

public int getBlockNum() {

return blockNum;

}

public void setBlockNum(int blockNum) {

this.blockNum = blockNum;

}

public MemoryBlock getPrev() {

return prev;

}

public void setPrev(MemoryBlock prev) {

this.prev = prev;

public MemoryBlock getNext() {

return next;

}

public void setNext(MemoryBlock next) {

this.next = next;

}

public boolean inUse() {

return inUse;

}

public void setUse() {

inUse = true;

}

public void free() {

inUse = false;

}

public int getBaseBlock() {

return baseBlock;

}

public void setBaseBlock(int baseBlock) { this.baseBlock = baseBlock;

}

//分配内存块,如果可分配,则返回剩余内存块

public MemoryBlock allocate(int blockNum) { if(this.blockNum - blockNum>0) {

int newBase = baseBlock + blockNum;

int newBlock = this.blockNum-blockNum;

this.blockNum = blockNum;

setUse();

return new MemoryBlock(newBase, newBlock);

}

else if(this.blockNum - blockNum ==0) {

this.blockNum = 0;

}

return null;

}

//判断内存块是否能合并

public boolean merge(MemoryBlock memBlock) {

if(baseBlock+blockNum==memBlock.getBaseBlock()) {

setBlockNum(blockNum+memBlock.blockNum);

memBlock.setBaseBlock(0);

memBlock.setBlockNum(0);

return true;

}

else

return false;

}

@Override

public String toString() {

String inUse = null;

if(inUse())inUse = "已分配";

else inUse = "未分配";

return"内存块 [基地址=" + baseBlock + ", 大小=" + blockNum +

", " + inUse + "]";

}

}

MemoryTable.java:

//虚类MemTable,提供内存链表的各种基本方法

public abstract class MemoryTable {

//MemoryBlock链表表头

protected MemoryBlock memList;

public MemoryTable(int blockNum) {

memList = new MemoryBlock(0, blockNum);

}

//把newBlock插入到memBlock前面

public void insertBefore(MemoryBlock memBlock, MemoryBlock newBlock){

if(memBlock.getPrev() != null)

memBlock.getPrev().setNext(newBlock);

if(memList == memBlock)

memList = newBlock;

newBlock.setPrev(memBlock.getPrev());

newBlock.setNext(memBlock);

memBlock.setPrev(newBlock);

}

相关文档
最新文档