内存分配指令执行器

来源:互联网 发布:macbook pro 软件推荐 编辑:程序博客网 时间:2024/05/17 22:43
import java.util.ArrayList;import java.util.List;import java.util.Scanner;class Mem {    int handle;    int start;    int count;    public Mem(int handle, int start, int count) {        this.handle = handle;        this.start = start;        this.count = count;    }}public class Main {    public static void fillTrue(boolean[] memory, int start, int count) {        for (int i = 0; i < count; i++) {            memory[start + i] = true;        }    }    public static void fillFalse(boolean[] memory, int start, int count) {        for (int i = 0; i < count; i++) {            memory[start + i] = false;        }    }    public static boolean[] def(boolean[] memory, List<Mem> allocatedMems) {        boolean[] result = new boolean[memory.length];        int k = 0;        int newStart;        for (Mem mem : allocatedMems) {            newStart = k;            for (int i = 0; i < mem.count; i++) {                result[k++] = true;            }            mem.start = newStart;        }        return result;    }    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        while (in.hasNext()) {            int n = in.nextInt();            int size = in.nextInt();            List<Mem> handles = new ArrayList<>();            boolean[] memory = new boolean[size];            int newCount = 1;            in.nextLine();            for (int i = 0; i < n; i++) {                String instruction = in.nextLine();                if (instruction.startsWith("new")) {                    String[] temp = instruction.split(" ");                    int allocSize = Integer.parseInt(temp[1]);                    int start = 0;                    int count = 0;                    for (int k = 0; k < size; k++) {                        if (!memory[k]) {                            start = k;                            while (k < size && !memory[k]) {                                count++;                                k++;                                if (count == allocSize) {                                    System.out.println(newCount);                                    fillTrue(memory, start, count);                                    Mem mem = new Mem(newCount, start, count);                                    handles.add(mem);                                    newCount++;                                    break;                                }                            }                            if (count < allocSize) {                                count = 0;                            } else {                                break;                            }                        }                    }                    if (count < allocSize) {                        System.out.println("NULL");                    }                } else if (instruction.startsWith("del")) {                    String[] temp = instruction.split(" ");                    boolean found = false;                    if (temp.length == 2) {                        int handleNum = Integer.parseInt(temp[1]);                        for (int z = 0; z < handles.size(); z++) {                            Mem mem = handles.get(z);                            if (mem.handle == handleNum) {                                found = true;                                handles.remove(z);                                fillFalse(memory, mem.start, mem.count);                            }                        }                    }                    if (temp.length < 2 || !found) {                        System.out.println("ILLEGAL_OPERATION");                    }                } else if (instruction.startsWith("def")) {                    memory = def(memory, handles);                }            }        }    }}

input demo:
6 10 //6条指令,总内存为10M
new 5 //分配5M内存
new 3 //分配3M内存
del 1 //清除第1次分配的内存
new 6 //分配6M内存
def //整理内存,清除碎片
new 6 //分配6M内存

output demo:
1 //第1次分配内存句柄1
2 //第2次分配内存句柄2
NULL //空间不足,无法分配内存
3 //第3次分配内存句柄3

0 0
原创粉丝点击