-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimsys.c
More file actions
executable file
·277 lines (210 loc) · 7.12 KB
/
simsys.c
File metadata and controls
executable file
·277 lines (210 loc) · 7.12 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "simsys.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
long BLOCK_SIZE = 0;
long NUM_BLOCKS = 0;
DirTree ROOT_DIR = NULL;
DirTree WORK_DIR = NULL;
/**
* The list of allocated blocks.
* Invariant - For any index i = 2n,
* i is the start of a memory
* block, and i+1 is the end of
* the same block.
* Logic: In memory, we suppose that we have some set
* of allocated sectors B_i = [a, b), where all
* addresses in blocks [a, b) are taken. We say
* that we will hold a list of these pairs, such
* that any two consecutive values represent either
* the beginning and end of sectors or the borders
* of consective sectors.
*/
LList MEM_ALLOC;
void init_filesystem(long blk_size, long size) {
/* Prevent the function from being called more than once. */
if (ROOT_DIR) {
printf("\033[1m\033[31mERROR\033[0m: Cannot call init_filesystem more than once!\n");
return;
}
/* The size of a block and the number of blocks are stored. */
BLOCK_SIZE = blk_size;
NUM_BLOCKS = size / blk_size;
/* The root node of the filesystem. */
ROOT_DIR = makeDirTree("", 0);
/* The initial working directory is root by default. */
WORK_DIR = ROOT_DIR;
/* The list of memory allocations. */
MEM_ALLOC = makeLL();
}
void flush_filesystem() {
WORK_DIR = NULL;
/* Recursively destroy the file tree */
flushDirTree(ROOT_DIR);
/* Dispose of memory allocation */
while(!isEmptyLL(MEM_ALLOC))
free(remFromLL(MEM_ALLOC, 0));
free(MEM_ALLOC);
MEM_ALLOC = NULL;
BLOCK_SIZE = 0;
NUM_BLOCKS = 0;
}
DirTree getRootNode() {
return ROOT_DIR;
}
DirTree getWorkDirNode() {
return WORK_DIR;
}
void setWorkDirNode(DirTree node) {
WORK_DIR = node;
}
long blockSize() {
return BLOCK_SIZE;
}
long numBlocks() {
return NUM_BLOCKS;
}
long numSectors() {
return sizeOfLL(MEM_ALLOC) / 2;
}
void freeBlock(long blk) {
int sectors = sizeOfLL(MEM_ALLOC);
long lo, hi;
int i;
/* End result: The ith block contains block blk. */
for (i = 0; i < sectors; i++) {
/* If block i contains blk, it must be freed from the block. */
if ( *((long*) getFromLL(MEM_ALLOC, 2*i)) <= blk
&& *((long*) getFromLL(MEM_ALLOC, 2*i+1)) > blk)
break;
}
if (i == sectors)
return;
lo = *((long*) getFromLL(MEM_ALLOC, 2*i));
hi = *((long*) getFromLL(MEM_ALLOC, 2*i+1));
if (lo == hi-1) {
/* The sector is of size 1, so free the whole sector */
free(remFromLL(MEM_ALLOC, 2*i));
free(remFromLL(MEM_ALLOC, 2*i));
} else if (lo == blk) {
/* The block is at the front of the sector */
*((long*) getFromLL(MEM_ALLOC, 2*i)) += 1;
} else if (hi-1 == blk) {
/* The block is at the back of the sector */
*((long*) getFromLL(MEM_ALLOC, 2*i+1)) -= 1;
} else {
/* IN any other case, the free will split the block in two. */
/* The new bounds are computed (mLo and mHi represent the freed block) */
long *mLo = (long*) malloc(sizeof(long));
long *mHi = (long*) malloc(sizeof(long));
*mLo = blk;
*mHi = blk+1;
/* Create a one block gap in the memory by inserting the pair */
/* [a,b)...[c,d)...[e,f)... ==> [a,b)...[c,mLo)x[mHi, d)... */
addToLL(MEM_ALLOC, 2*i+1, mLo);
addToLL(MEM_ALLOC, 2*i+2, mHi);
}
}
long allocBlock() {
/* The number of sectors */
long sectors = sizeOfLL(MEM_ALLOC) / 2;
long *tmp;
if (sectors == 0) {
/* No memory allocated */
/* We add the two bounds of the block to allocate. */
tmp = (long*) malloc(sizeof(long));
*tmp = 0;
addToLL(MEM_ALLOC, 0, tmp);
tmp = (long*) malloc(sizeof(long));
*tmp = 1;
addToLL(MEM_ALLOC, 1, tmp);
return 0;
} else {
/* The bounds of the memory available */
long min_free = 0;
long max_free = *((long*) getFromLL(MEM_ALLOC, 0));
if (min_free == max_free) {
/* Can't place in front, 0 bytes at the front */
/* The new minimum is the end of the first alloc'd sector */
min_free = *((long*) getFromLL(MEM_ALLOC, 1));
/* The end of the free sector is either the start of the next
sector or the end of memory */
max_free = sectors == 1
? NUM_BLOCKS
: *((long*) getFromLL(MEM_ALLOC, 2));
if (min_free == max_free) {
/* No available memory */
return -1;
} else if (min_free == max_free - 1) {
/* Closes a one-byte gap in memory (only one byte between) */
free(remFromLL(MEM_ALLOC, 1));
free(remFromLL(MEM_ALLOC, 1));
} else {
*((long*) getFromLL(MEM_ALLOC, 1)) += 1;
}
/* The first free block was alloc'd */
return min_free;
} else if (min_free == max_free - 1) {
/* Close a one-block frag in the front */
*((long*) getFromLL(MEM_ALLOC, 0)) = 0;
return 0;
} else {
/* Memory in non-bordering space */
tmp = (long*) malloc(sizeof(long));
*tmp = 0;
addToLL(MEM_ALLOC, 0, tmp);
tmp = (long*) malloc(sizeof(long));
*tmp = 1;
addToLL(MEM_ALLOC, 1, tmp);
return 0;
}
}
}
int enoughMemFor(long amt) {
int secs = sizeOfLL(MEM_ALLOC) / 2;
long avail = 0;
int i;
/* If memory is empty, the answer is simple. */
if (secs == 0)
return NUM_BLOCKS >= amt;
/* The amount of memory is equal to the total amount
of memory between each block of allocated memory. */
avail = *((long*) getFromLL(MEM_ALLOC, 0));
for (i = 1; i < secs && avail < amt; i++) {
avail += *((long*) getFromLL(MEM_ALLOC, 2*i-1))
- *((long*) getFromLL(MEM_ALLOC, 2*i));
}
/* If all gaps were viewed, tack the end onto the total */
if (i == secs)
avail += NUM_BLOCKS - *((long*) getFromLL(MEM_ALLOC, 2*secs-1));
/* Answer the question */
return amt <= avail;
}
LList getAllocData() {
return cloneLL(MEM_ALLOC);
}
long blocksAllocated() {
long amt = 0;
LLiter iter = makeLLiter(MEM_ALLOC);
while (iterHasNextLL(iter))
amt -= (*((long*) iterNextLL(iter)) - *((long*) iterNextLL(iter)));
return amt;
}
long nextBlock() {
if (isEmptyLL(MEM_ALLOC))
return 0;
else if (*((long*) getFromLL(MEM_ALLOC, 0)))
return 0;
else
return *((long*) getFromLL(MEM_ALLOC, 1));
}
DirTree getRelTree(DirTree tree, char **path) {
if (!path)
return getRootNode();
else if (!path[0])
return getWorkDirNode();
else if (strcmp(path[0], ""))
return getDirSubtree(tree, path);
else
return getDirSubtree(getRootNode(), &path[1]);
}