内存分配指令执行器

import java.util.HashMap;
import java.util.Map;
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 fill(boolean[] memory, int start, int count, boolean flag) {
        for (int i = start; i < start + count; i++) {
            memory[i] = flag;
        }
    }

    public static void def(boolean[] memory, Map<Integer, Mem> allocatedMap) {
        int k = 0;
        for (Map.Entry<Integer, Mem> entry : allocatedMap.entrySet()) {
            Mem currMem = entry.getValue();
            currMem.start = k;
            for (int i = 0; i < currMem.count; i++) {
                memory[k++] = true;
            }
        }

        for (int i = k; i < memory.length; i++) {
            memory[i] = false;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int instructionCount = in.nextInt();
            int memSize = in.nextInt();

            Map<Integer, Mem> allocatedMap = new HashMap<>();

            boolean[] memory = new boolean[memSize];
            int newMemCount = 1;
            in.nextLine();
            for (int i = 0; i < instructionCount; i++) {
                String instruction = in.nextLine();

                //分配内存
                if (instruction.startsWith("new")) {
                    String[] temp = instruction.split(" ");
                    int allocSize = Integer.parseInt(temp[1]);
                    int allocSizeCount = allocSize;
                    int pos = 0;

                    while (pos < memSize && memory[pos]) {
                        pos++;
                    }

                    int start = pos;

                    while (pos < memSize && !memory[pos]) {
                        pos++;
                        allocSizeCount--;
                        if (allocSizeCount == 0) {
                            break;
                        }
                    }

                    if (allocSizeCount == 0) {
                        System.out.println(newMemCount);
                        fill(memory, start, allocSize, true);
                        Mem mem = new Mem(newMemCount, start, allocSize);
                        allocatedMap.put(newMemCount++, mem);
                    } else {
                        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]);
                        Mem removedMem = allocatedMap.remove(handleNum);
                        if (removedMem != null) {
                            found = true;
                            fill(memory, removedMem.start, removedMem.count, false);
                        }
                    }

                    if (temp.length != 2 || !found) {
                        System.out.println("ILLEGAL_OPERATION");
                    }
                } else if (instruction.startsWith("def")) {
                    //整理内存
                    def(memory, allocatedMap);
                }
            }
        }
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值