-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemorySpace.java
More file actions
121 lines (112 loc) · 4.57 KB
/
MemorySpace.java
File metadata and controls
121 lines (112 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//package MMS;
/*
Assignment number :10
File Name : MemorySpace.java
Name: Ran Zaaroor
Student ID : 209374040
Email : Ran.zaaroor@gmail.com
*/
import java.nio.channels.spi.AbstractInterruptibleChannel;
import java.util.Iterator;
/**
* Represents a managed memory space (also called "heap"). The memory space is managed by three
* methods: <br> <b> malloc </b> allocates memory blocks, <br> <b> free </b> recycles memory blocks,
* <br> <b> defrag </b> reorganizes the memory space, for better allocation and rescheduling.
* <br> (Part of Homework 10 in the Intro to CS course, Efi Arazi School of CS)
*/
public class MemorySpace {
// A list that keeps track of the memory blocks that are presently allocated
private List allocatedList;
// A list that keeps track of the memory blocks that are presently free
private List freeList;
/**
* Constructs a managed memory space ("heap") of a given maximal size.
*
* @param maxSize The size of the memory space to be managed
*/
public MemorySpace(int maxSize) {
// Constructs and intilaizes an empty list of allocated memory blocks, and a free list containing
// a single memory block which represents the entire memory space. The base address of this single
// memory block is zero, and its length is the given memory size (maxSize).
allocatedList = new List();
freeList = new List();
freeList.addLast(new MemBlock(0, maxSize));
}
/**
* Allocates a memory block.
*
* @param length The length (in words) of the memory block that has to be allocated
* @return the base address of the allocated block, or -1 if unable to allocate
*/
public int malloc(int length) {
// Scans the freeList, looking for the first free memory block whose length equals at least
// the given length. If such a block is found, the method performs the following operations:
//
// (1) A new memory block is constructed. The base address of the new block is set to
// the base address of the found free block. The length of the new block is set to the value
// of the method's length parameter.
//
// (2) The new memory block is appended to the end of the allocatedList.
//
// (3) The base address and the length of the found free block are updated, to reflect the allocation.
// For example, suppose that the requested block length is 17, and suppose that the base
// address and length of the the found free block are 250 and 20, respectively.
// In such a case, the base address and length of of the allocated block are set to 250 and 17,
// respectively, and the base address and length of the found free block are updated to 267 and 3, respectively.
//
// (4) The base address of the new memory block is returned.
//
// If the length of the found block is exactly the same as the requested length,
// then the found block is removed from the freeList, and appended to the allocatedList.
ListIterator list = freeList.iterator();
while (list.hasNext()){
if(list.current.block.length >= length){
if(list.current.block.length == length){
MemBlock mbEqual = list.current.block;
allocatedList.addLast(list.current.block);
freeList.remove(list.current.block);
return mbEqual.baseAddress;
}
MemBlock mb = new MemBlock(list.current.block.baseAddress, length);
allocatedList.addLast(mb);
list.current.block.baseAddress += length;
list.current.block.length = list.current.block.length - length;
return mb.baseAddress;
}
list.next();
}
return -1;
}
/**
* Frees the memory block whose base address equals the given address
*
* @param address The base address of the memory block to free
*/
public void free(int address) {
// Adds the memory block to the free list, and removes it from the allocated list.
ListIterator list = allocatedList.iterator();
while(list.hasNext()){
if(list.current.block.baseAddress == address){
freeList.addLast(list.current.block);
allocatedList.remove(list.current.block);
}
list.next();
}
}
/**
* A textual representation of this memory space
* @return a string representation of this memory space.
*/
public String toString() {
// Returns the textual representation of the free list, a new line, and then
// the textual representation of the allocated list, as one string
StringBuilder st = new StringBuilder();
st.append(freeList + "\n");
if(allocatedList.getNode(0) == null){
return st.toString();
}else{
st.append(allocatedList);
return st.toString();
}
}
}