-
Notifications
You must be signed in to change notification settings - Fork 0
/
vma.c
296 lines (277 loc) · 7.09 KB
/
vma.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
// Copyright Razvan-Constantin Rus 312CAa 2022-2023
#include "vma.h"
//functiile cerute
arena_t alloc_arena(const long size)
{
arena_t a = (arena_t)malloc(sizeof(arena));
verf_din_alloc(a);
if (a) {
a->arena_size = size;
a->alloc_list = dll_create(sizeof(block));
if (!a->alloc_list) {
free(a);
a = NULL;
}
}
a->free_mem = size;
a->miniblocks = 0;
return a;
}
void dealloc_arena(arena_t arena)
{
dll_free(&arena->alloc_list, dealloc_blocks);
my_free(arena);
}
void alloc_block(arena_t a, const long addr, const long size)
{
miniblock_t mnb = (miniblock_t)malloc(sizeof(miniblock));
dll_node_t nx, prv;
dl_list_t list1, list2;
if (verf_din_alloc(mnb))
return;
init_miniblock(mnb, addr, size);
block_t bl = (block_t)malloc(sizeof(block)), nx_bl, prv_bl;
if (verf_din_alloc(bl)) {
free(mnb);
return;
}
bl->start_address = addr;
bl->size = size;
bl->miniblock_list = dll_create(sizeof(miniblock));
if (verf_din_alloc(bl->miniblock_list)) {
free(mnb);
free(bl);
return;
}
if (dll_add_nth_node(bl->miniblock_list, 0, mnb) == -1)
printf("Miniblock allocation failed\n");
free(mnb);
if (a->alloc_list->size == 0 && addr + size <= a->arena_size) {
if (dll_add_nth_node(a->alloc_list, 0, bl) == -1) {
printf("Block allocation failed\n");
} else {
a->miniblocks++;
a->free_mem -= size;
}
free(bl);
return;
}
long poz = valid_address(a, addr, size);
if (poz == -1) {
dealloc_blocks(bl);
return;
}
if (poz != a->alloc_list->size) {
nx = dll_get_nth_node(a->alloc_list, poz);
nx_bl = (block_t)nx->data;
if (addr + size == nx_bl->start_address) {
list1 = bl->miniblock_list;
list2 = nx_bl->miniblock_list;
if (dll_merge(list1, list2, dealloc_miniblocks) == -1)
return;
bl->size += nx_bl->size;
nx = dll_remove_nth_node(a->alloc_list, poz);
my_free(nx_bl->miniblock_list);
my_free(nx_bl);
my_free(nx);
}
}
if (poz != 0) {
prv = dll_get_nth_node(a->alloc_list, poz - 1);
prv_bl = (block_t)prv->data;
if (addr == prv_bl->start_address + prv_bl->size) {
list1 = prv_bl->miniblock_list;
list2 = bl->miniblock_list;
if (dll_merge(list1, list2, dealloc_miniblocks) == -1)
return;
prv_bl->size += bl->size;
my_free(bl->miniblock_list);
my_free(bl);
a->miniblocks++;
a->free_mem -= size;
return;
}
}
if (dll_add_nth_node(a->alloc_list, poz, bl) == -1) {
printf("Block allocation failed\n");
} else {
a->miniblocks++;
a->free_mem -= size;
}
free(bl);
}
void free_block(arena_t a, const long addr)
{
dll_node_t node = a->alloc_list->head;
if (!node) {
printf("Invalid address for free.\n");
return;
}
block_t bl = (block_t)node->data;
long i, n = a->alloc_list->size;
for (i = 0; i < n; i++) {
if (bl->start_address <= addr && bl->start_address + bl->size > addr)
break;
node = node->next;
bl = (block_t)node->data;
}
if (bl->start_address <= addr && bl->start_address + bl->size > addr) {
if (i < n)
free_miniblock(a, i, addr);
} else {
printf("Invalid address for free.\n");
}
}
void read(arena_t a, long addr, long size)
{
dll_node_t node, r_beg = NULL;
block_t bl;
miniblock_t mnb;
node = find_addr_block(a, addr);
if (!node) {
printf("Invalid address for read.\n");
return;
}
bl = node->data;
r_beg = find_addr_node(bl, addr, 4);
if (!r_beg) {
printf("Invalid permissions for read.\n");
return;
}
long new_size = size;
if (addr + size > bl->start_address + bl->size) {
new_size = new_size - (addr + size - bl->start_address - bl->size);
printf("Warning: size was bigger than the block size.");
printf(" Reading %ld characters.\n", new_size);
}
mnb = (miniblock_t)r_beg->data;
int offset = addr - mnb->start_address, min;
char *aux;
while (new_size > 0) {
if (!mnb->rw_buffer) {
printf("\n");
return;
}
min = my_min(new_size, mnb->size - offset);
aux = calloc(min + 1, sizeof(char));
if (verf_din_alloc(aux))
return;
memcpy(aux, mnb->rw_buffer + offset, min);
aux[min] = '\0';
printf("%s", aux);
free(aux);
new_size -= min;
offset = 0;
r_beg = r_beg->next;
mnb = (miniblock_t)r_beg->data;
}
printf("\n");
}
void write(arena_t a, const long addr, const long size, void *data)
{
dll_node_t node, w_beg = NULL;
block_t bl;
miniblock_t mnb;
node = find_addr_block(a, addr);
if (!node) {
printf("Invalid address for write.\n");
return;
}
bl = node->data;
w_beg = find_addr_node(bl, addr, 2);
if (!w_beg) {
printf("Invalid permissions for write.\n");
return;
}
long new_size = size;
if (addr + size > bl->start_address + bl->size) {
new_size = new_size - (addr + size - bl->start_address - bl->size);
printf("Warning: size was bigger than the block size.");
printf(" Writing %ld characters.\n", new_size);
}
mnb = (miniblock_t)w_beg->data;
long offset = addr - mnb->start_address, min;
while (new_size > 0) {
if (!mnb->rw_buffer)
mnb->rw_buffer = calloc(mnb->size, sizeof(char));
if (verf_din_alloc(mnb->rw_buffer))
return;
min = my_min(new_size, mnb->size - offset);
memcpy(mnb->rw_buffer + offset, data, min);
data = data + min;
new_size -= min;
offset = 0;
w_beg = w_beg->next;
mnb = (miniblock_t)w_beg->data;
}
}
void pmap(const arena_t arena)
{
long n = arena->alloc_list->size, j, m, i, end_addr;
printf("Total memory: 0x%lX bytes\n", arena->arena_size);
printf("Free memory: 0x%lX bytes\n", arena->free_mem);
printf("Number of allocated blocks: %ld\n", n);
printf("Number of allocated miniblocks: %ld\n", arena->miniblocks);
char *perm = (char *)calloc(4, sizeof(char));
if (verf_din_alloc(perm))
return;
dll_node_t bl_node = arena->alloc_list->head, mnb_node;
block_t bl;
miniblock_t mnb;
for (i = 0; i < n; i++) {
bl = (block_t)bl_node->data;
mnb_node = (dll_node_t)bl->miniblock_list->head;
mnb = (miniblock_t)mnb_node->prev->data;
end_addr = mnb->start_address + mnb->size;
printf("\nBlock %ld begin\n", i + 1);
printf("Zone: 0x%lX - 0x%lX\n", bl->start_address, end_addr);
m = bl->miniblock_list->size;
mnb = (miniblock_t)mnb_node->data;
for (j = 0; j < m; j++) {
printf("Miniblock %ld:\t\t0x", j + 1);
printf("%lX\t\t-\t\t0x", mnb->start_address);
printf("%lX\t\t| ", mnb->start_address + mnb->size);
printf("%s\n", perm_rev(perm, mnb->perm));
mnb_node = mnb_node->next;
mnb = (miniblock_t)mnb_node->data;
}
bl_node = bl_node->next;
printf("Block %ld end\n", i + 1);
}
free(perm);
}
void mprotect(arena_t a, long addr, long *permission)
{
dll_node_t node, p_beg = NULL;
block_t bl;
miniblock_t mnb;
node = find_addr_block(a, addr);
if (!node) {
printf("Invalid address for mprotect.\n");
//printf("222\n");
return;
}
bl = node->data;
p_beg = find_addr_node(bl, addr, 0);
if (!p_beg) {
printf("Invalid address for mprotect.\n");
//printf("111\n");
return;
}
long fin_bl = bl->start_address + bl->size;
long perm = *permission;
mnb = p_beg->data;
if (addr != mnb->start_address) {
printf("Invalid address for mprotect.\n");
//printf("111\n");
return;
}
//printf("%ld\n", mnb->start_address);
while (1) {
mnb->perm = perm;
if (mnb->start_address + mnb->size >= fin_bl)
break;
p_beg = p_beg->next;
mnb = (miniblock_t)p_beg->data;
}
}