-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
109 lines (90 loc) · 2.83 KB
/
main.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
/**
* Copyright GOIDESCU Rares-Stefan 312CAb 2023-2024
* Tema-1-Structuri-de-Date
* Spring 2024
*/
#include <stdio.h>
#include <string.h>
#include "headers/structs.h"
#include "headers/commands.h"
#include "headers/doubly_ll_ops.h"
void
init_structs(seg_free_list_t *heap, allocd_t *allocd, heap_info_t *heap_info)
{
heap->mem_left = 0;
heap->n_blocks = 0;
heap->max_size = 0;
heap->curr_size = 0;
allocd->allocd_mem = 0;
allocd->blocks = create_dll(0);
heap_info->total_mem = 0;
heap_info->start_addr = 0;
heap_info->n_free_calls = 0;
heap_info->n_malloc_calls = 0;
heap_info->n_fragmentations = 0;
heap_info->last_fragmentation_id = 0;
}
int main(void)
{
int reconstruction_type = 0;
U_CHAR heap_is_initd = FALSE;
seg_free_list_t heap;
allocd_t allocd;
heap_info_t heap_info;
init_structs(&heap, &allocd, &heap_info);
while (TRUE) {
char command[16];
scanf("%s", command);
if (!strncmp(command, "INIT_HEAP", 9) && !heap_is_initd) {
unsigned int bytes_per_list = 0;
unsigned int list_num = 0;
scanf("%x%u%u%d", &heap_info.start_addr, &list_num,
&bytes_per_list, &reconstruction_type);
heap_info.total_mem = list_num * bytes_per_list;
init_heap(&heap, heap_info.start_addr, list_num, bytes_per_list);
heap_is_initd = TRUE;
} else if (!strncmp(command, "MALLOC", 6) && heap_is_initd) {
unsigned int bytes_to_alloc;
scanf("%u", &bytes_to_alloc);
malloc_cmd(&heap, bytes_to_alloc, &allocd, &heap_info);
} else if (!strncmp(command, "FREE", 4) && heap_is_initd) {
unsigned int addr_to_free;
scanf("%x", &addr_to_free);
if (reconstruction_type == 0)
free_cmd_0(&heap, &allocd, &heap_info, addr_to_free);
else
free_cmd_1(&heap, &allocd, &heap_info, addr_to_free);
} else if (!strncmp(command, "READ", 4) && heap_is_initd) {
unsigned int addr_to_read_from;
unsigned int bytes_to_read;
scanf("%x%u", &addr_to_read_from, &bytes_to_read);
int status = read_cmd(&allocd, addr_to_read_from, bytes_to_read);
if (status == -1) {
dump_memory(heap_info, heap, allocd);
goto kill_program;
}
} else if (!strncmp(command, "WRITE", 5) && heap_is_initd) {
unsigned int addr_to_write_at;
unsigned int data_size;
char string_to_write[256];
// se poate folosi regex in scanf LOL
scanf("%x \"%[^\"]\" %u", &addr_to_write_at,
string_to_write, &data_size);
int status = write_cmd(&allocd, addr_to_write_at,
string_to_write, data_size);
if (status == -1) {
dump_memory(heap_info, heap, allocd);
goto kill_program;
}
} else if (!strncmp(command, "DUMP_MEMORY", 11) && heap_is_initd) {
dump_memory(heap_info, heap, allocd);
} else if (!strncmp(command, "DESTROY_HEAP", 12) && heap_is_initd) {
kill_program:
destroy_heap(&heap, &allocd);
break;
} else if (!strncmp(command, "EXIT", 4)) {
goto kill_program;
}
}
return 0;
}