-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_system.c
337 lines (323 loc) · 7.48 KB
/
my_system.c
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Test-Umgebung
2006 urspruenglich von Christian Ehrhardt entwickelt
mit Anpassungen von Andreas F. Borchert
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include "my_system.h"
#define SYSBLOCKSIZE 8192
struct sysblock {
char * start;
size_t offset;
struct sysblock * next;
};
static struct sysblock * sysblocks = NULL;
static size_t sys_blockcount = 0;
static struct avl_node * blocks = NULL;
void * get_block_from_system ()
{
char * ret;
if (sysblocks == NULL || sysblocks->offset == SYSBLOCKSIZE) {
struct sysblock * nb = malloc (sizeof (struct sysblock));
/* Betriebssystem hat keinen weiteren Speicher mehr. */
my_assert (nb, "Betriebssystem hat keinen weiteren Speicher mehr");
nb->start = mmap (0, SYSBLOCKSIZE, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANON, -1, 0);
if (nb->start == NULL || nb->start == MAP_FAILED) {
free (nb);
return NULL;
}
nb->offset = 0;
nb->next = sysblocks;
sysblocks = nb;
}
ret = sysblocks->start + sysblocks->offset;
sysblocks->offset += BLOCKSIZE;
sys_blockcount++;
if (blocks == NULL) {
blocks = create_avl ();
}
insert_avl (&blocks, (size_t)ret, BLOCKSIZE);
return ret;
}
size_t get_sys_blockcount ()
{
return sys_blockcount;
}
bool valid_area (size_t start, size_t len)
{
struct avl_node * node;
if (!blocks)
return false;
node = find_avl (blocks, start);
assert (node->start <= start);
/* Speicherbereich an Adresse 0 oder nicht an einer 8 Byte Kante. */
my_assert (start, "Speicherbereich an Adresse 0");
my_assert (start % 8 == 0 && len % 8 == 0, "Speicherbereich nicht an einer 8 Byte Kante");
if (node->start + node->len < start + len)
for (size_t n=start; n < start+len; n+=8)
my_assert(find_avl(blocks, n), "Speicherbereich ragt in eine Region, die nicht mit get_block_from_system angfordert wurde");
return true;
}
#define HEIGHT_LEFT(N) (((N)->left)?((N)->left->height):0)
#define HEIGHT_RIGHT(N) (((N)->right)?((N)->right->height):0)
#define GETHEIGHT(N) ((HEIGHT_LEFT(N)>HEIGHT_RIGHT(N))?(1+HEIGHT_LEFT(N)):(1+HEIGHT_RIGHT(N)))
static void rebalance1 (struct avl_node ** root, struct avl_node * n)
{
struct avl_node * parent;
if (n == 0)
return;
parent = n->parent;
n->height = GETHEIGHT(n);
if (HEIGHT_LEFT(n) + 1 < HEIGHT_RIGHT(n)) {
struct avl_node * r = n->right;
struct avl_node * rl = r->left;
if (HEIGHT_RIGHT(r) > HEIGHT_LEFT(n)) {
r->left = n; n->parent = r;
n->right = rl;
if (rl) {
rl->parent = n;
}
n->height = GETHEIGHT(n);
r->height = GETHEIGHT(r);
r->parent = parent;
if (parent) {
if (parent->left == n) {
parent->left = r;
} else {
parent->right = r;
}
parent->height = GETHEIGHT(parent);
} else {
(*root) = r;
}
} else {
struct avl_node * rll = rl->left;
struct avl_node * rlr = rl->right;
n->right = rll;
if (rll)
rll->parent = n;
r->left = rlr;
if (rlr)
rlr->parent = r;
rl->left = n; n->parent = rl;
rl->right = r; r->parent = rl;
rl->parent = parent;
n->height = GETHEIGHT(n);
r->height = GETHEIGHT(r);
rl->height = GETHEIGHT(rl);
if (parent) {
if (parent->left == n) {
parent->left = rl;
} else {
parent->right = rl;
}
parent->height = GETHEIGHT(parent);
} else {
(*root) = rl;
}
}
return;
}
if (HEIGHT_RIGHT(n) + 1 < HEIGHT_LEFT(n)) {
struct avl_node * l = n->left;
struct avl_node * lr = l->right;
if (HEIGHT_LEFT(l) > HEIGHT_RIGHT(n)) {
l->right = n; n->parent = l;
n->left = lr;
if (lr)
lr->parent = n;
n->height = GETHEIGHT(n);
l->height = GETHEIGHT(l);
l->parent = parent;
if (parent) {
if (parent->left == n) {
parent->left = l;
} else {
parent->right = l;
}
parent->height = GETHEIGHT(parent);
} else {
(*root) = l;
}
} else {
struct avl_node * lrl = lr->left;
struct avl_node * lrr = lr->right;
n->left = lrr;
if (lrr)
lrr->parent = n;
l->right = lrl;
if (lrl)
lrl->parent = l;
lr->right = n; n->parent = lr;
lr->left = l; l->parent = lr;
lr->parent = parent;
n->height = GETHEIGHT(n);
l->height = GETHEIGHT(l);
lr->height = GETHEIGHT(lr);
if (parent) {
if (parent->left == n) {
parent->left = lr;
} else {
parent->right = lr;
}
parent->height = GETHEIGHT(parent);
} else {
(*root) = lr;
}
}
return;
}
}
static void rebalance (struct avl_node ** root, struct avl_node * node)
{
struct avl_node * next;
while (node) {
next = node->parent;
rebalance1 (root, node);
node = next;
}
}
struct avl_node * create_avl (void)
{
struct avl_node * ret = malloc (sizeof (struct avl_node));
assert (ret);
ret->prev = ret->next = NULL;
ret->left = ret->right = ret->parent = NULL;
ret->start = ret->len = 0;
ret->height = 1;
return ret;
}
struct avl_node * find_avl (struct avl_node * root, size_t start)
{
struct avl_node * node = root;
assert (node);
while (1) {
if (start < node->start) {
if (!node->left) {
break;
}
node = node->left;
continue;
}
if (node->start == start)
return node;
if (!node->right)
break;
node = node->right;
}
if (start < node->start) {
assert (node->prev);
node = node->prev;
}
assert (start >= node->start);
if (node->next) {
assert (node->next->start > start);
}
return node;
}
void insert_avl (struct avl_node ** root, size_t start, size_t len)
{
struct avl_node * prev = find_avl (*root, start);
struct avl_node * next = prev->next;
struct avl_node * n;
/* Ueberlappende Speicherbereiche! */
my_assert (prev->start + prev->len <= start, "Ueberlappende Speicherbereiche");
if (next) {
/* Ueberlappende Speicherbereiche */
my_assert (next->start >= start + len, "Ueberlappende Speicherbereiche");
}
n = malloc (sizeof (struct avl_node));
assert (n);
n->start = start;
n->len = len;
n->prev = prev;
n->next = next;
prev->next = n;
if (next) {
next->prev = n;
}
n->height = 1;
n->left = n->right = NULL;
if (prev->right == NULL) {
prev->right = n;
n->parent = prev;
} else {
prev = prev->right;
while (prev->left) {
prev = prev->left;
}
prev->left = n;
n->parent = prev;
}
rebalance (root, n);
}
void remove_avl (struct avl_node ** root, struct avl_node * node)
{
struct avl_node * todel;
if (node->left == NULL) {
struct avl_node * prev = node->prev;
struct avl_node * next = node->next;
struct avl_node * parent = node->parent;
prev->next = next;
if (next) {
next->prev = prev;
} else {
assert (node->right == NULL);
}
if (parent) {
if (parent->left == node) {
parent->left = node->right;
} else {
assert (parent->right == node);
parent->right = node->right;
}
} else {
(*root) = node->right;
}
if (node->right) {
node->right->parent = parent;
}
free (node);
rebalance (root, parent);
} else {
struct avl_node * prev;
struct avl_node * next;
struct avl_node * parent;
todel = node->prev;
assert (todel && todel->right == NULL);
node->start = todel->start;
node->len = todel->len;
prev = todel->prev;
next = todel->next;
parent = todel->parent;
if (prev) {
prev->next = next;
}
if (next) {
next->prev = prev;
}
if (parent) {
if (parent->left == todel) {
parent->left = todel->left;
} else {
parent->right = todel->left;
}
} else {
(*root) = todel->left;
}
if (todel->left) {
todel->left->parent = parent;
}
free (todel);
rebalance (root, parent);
}
}
void __my_assert (char * text)
{
fprintf (stderr, "\nFEHLER: %s\n", text);
exit (1);
}