内存分配指令执行器
来源:互联网 发布: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
- 内存分配指令执行器
- 应用程序执行 内存分配
- 编译到执行 关于内存分配
- 操作系统执行可执行文件时的内存分配
- 对系统调用和内存分配检查是否执行成功
- 操作系统执行可执行程序时,内存是如何分配的?
- ARM中状态转移指令+条件执行+内存操作
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 内存分配
- 树状数组+离散化——HDU 5877 (2016 ACM/ICPC Asia Regional Dalian Online 1010)
- Android Studio新功能,了解Instant Run
- POJ3253——哈夫曼思想的优先队列
- Maven隐式变量和依赖其他Maven项目
- Linux下 .o .a .so .mk 文件说明
- 内存分配指令执行器
- HDU 1576 (A/B)扩展欧几里德定理
- Activiti 学习笔记11:接收活动(receiveTask,即等待活动)
- CDH启停顺序
- 深入理解HTTP协议
- Ubuntu下QT控制台程序无法运行的解决方案以及XTerm的配置方法
- spring MVC配置详解
- Bmob后端云——私人云数据库
- samba服务配置