-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.c
More file actions
239 lines (211 loc) · 7.59 KB
/
Copy pathalloc.c
File metadata and controls
239 lines (211 loc) · 7.59 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
// Copyright Lazar Cristian-Stefan 314CA 2022-2023
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "struct.h"
#include "utils.h"
// Adds a miniblock to the end of the miniblock list of the l_block. If ver is
// 0, then the new miniblock is in the terminal node, who's next pointer will
// point to NULL. If ver is 1, then the function was called in order to
// concatenate two blocks, so, instead of NULL, the otherwise terminal node's
// next pointer is made to point to the head of the miniblock list of
// l_block->next.
void add_last(uint64_t address, size_t mblock_size, list_t *l_block, char ver)
{
block_t *block = (block_t *)l_block->data;
miniblock_t *new_mblock = malloc(sizeof(miniblock_t));
DIE(!new_mblock, "Malloc failed.\n");
list_t *new_l_block = malloc(sizeof(list_t));
DIE(!new_l_block, "Malloc failed.\n");
// The new miniblock is initialized.
new_mblock->start_address = address;
new_mblock->size = mblock_size;
new_mblock->perm = 6;
new_mblock->rw_buffer = calloc(1, mblock_size);
DIE(!new_mblock->rw_buffer, "Calloc failed.\n");
// The miniblock list of the l_block is sequenced through until the last
// node is reached.
list_t *curr = block->miniblock_list;
for (; curr->next; curr = curr->next)
;
// A new list node, containing the new miniblock, is initialized.
new_l_block->data = (void *)new_mblock;
new_l_block->prev = curr;
// Checks the context in which the function was called.
if (ver == 0) {
new_l_block->next = NULL;
} else {
new_l_block->next =
((block_t *)l_block->next->data)->miniblock_list;
new_l_block->next->prev = new_l_block;
}
curr->next = new_l_block;
// The block metadata is updated.
block->size += mblock_size;
}
// Adds a miniblock to the start of the miniblock list of the h_block.
void add_first(uint64_t address, size_t mblock_size, block_t *h_block)
{
miniblock_t *new_mblock = malloc(sizeof(miniblock_t));
DIE(!new_mblock, "Malloc failed.\n");
list_t *new_dll_block = malloc(sizeof(list_t));
DIE(!new_dll_block, "Malloc failed.\n");
// The new miniblock is initialized.
new_mblock->start_address = address;
new_mblock->size = mblock_size;
new_mblock->perm = 6;
new_mblock->rw_buffer = calloc(1, mblock_size);
DIE(!new_mblock->rw_buffer, "Calloc failed.\n");
// A list node containing the new miniblock is initialized.
new_dll_block->data = (void *)new_mblock;
new_dll_block->prev = NULL;
new_dll_block->next = h_block->miniblock_list;
new_dll_block->next->prev = new_dll_block;
// The block metadata is updated accordingly.
h_block->miniblock_list = new_dll_block;
h_block->start_address = address;
h_block->size += mblock_size;
}
// Creates a new miniblock and adds it to the end of the miniblock list of the
// l_block, and then concatenates the l_block and l_block->next.
void add_middle(uint64_t address, size_t mblock_size, list_t *l_block)
{
// Creates and adds the new miniblock to the end of the miniblock list of
// the l_block, and concatenates the two blocks.
add_last(address, mblock_size, l_block, 1);
// Increases the size of the l_block by the size of the h_block.
((block_t *)l_block->data)->size +=
((block_t *)l_block->next->data)->size;
// l_block is made to point to the l_block->next, which is then removed.
l_block = l_block->next;
l_block->prev->next = l_block->next;
if (l_block->next)
l_block->next->prev = l_block->prev;
free(l_block->data);
free(l_block);
}
// Creates a new block entry in the block list.
void alloc_new_block(arena_t *arena, uint64_t address, uint64_t size,
list_t *curr)
{
// The metadata of the arena are moddified.
arena->used_mem += size;
arena->mblocks++;
arena->blocks++;
block_t *new_block = malloc(sizeof(block_t));
DIE(!new_block, "Malloc failed.\n");
miniblock_t *new_mblock = malloc(sizeof(miniblock_t));
DIE(!new_mblock, "Malloc failed.\n");
list_t *new_dll_block_prim = malloc(sizeof(list_t));
DIE(!new_dll_block_prim, "Malloc failed.\n");
list_t *new_dll_block_sec = malloc(sizeof(list_t));
DIE(!new_dll_block_sec, "Malloc failed.\n");
// The miniblock is initialized.
new_mblock->start_address = address;
new_mblock->size = size;
new_mblock->perm = 6;
new_mblock->rw_buffer = calloc(1, size);
DIE(!new_mblock->rw_buffer, "Calloc failed.\n");
// A new list node is created, containing the new miniblock.
new_dll_block_sec->data = (void *)new_mblock;
new_dll_block_sec->next = NULL;
new_dll_block_sec->prev = NULL;
// This new node is added as the head of the miniblock list of the new
// block, and the new block is initialized.
new_block->miniblock_list = new_dll_block_sec;
new_block->size = size;
new_block->start_address = address;
// A new list node is created, storing the new block.
new_dll_block_prim->data = (void *)new_block;
// Checks if the block list is empty.
if (!curr) {
new_dll_block_prim->prev = NULL;
new_dll_block_prim->next = NULL;
// Checks if curr is the upper bound of the address.
} else if (address < ((block_t *)curr->data)->start_address) {
new_dll_block_prim->prev = curr->prev;
new_dll_block_prim->next = curr;
if (curr->prev)
curr->prev->next = new_dll_block_prim;
new_dll_block_prim->next->prev = new_dll_block_prim;
// Otherwise, curr must be the last block in the block list.
} else {
new_dll_block_prim->prev = curr;
new_dll_block_prim->next = NULL;
curr->next = new_dll_block_prim;
}
// Checks if the block was placed at the start of the block list.
if (!new_dll_block_prim->prev)
arena->alloc_list = new_dll_block_prim;
}
// Checks if the new miniblock is in a valid position, and then if it borders
// a block below it or above it.
int8_t check_aliniation(arena_t *arena, uint64_t address, uint64_t size,
list_t *curr)
{
block_t *upper = curr->data;
list_t *lower;
// If the address is between the start address of the last block and the
// end of the arena, then the block returned is instead the lower bound.
// A check is done in order to check this.
if (address > ((block_t *)curr->data)->start_address && !curr->next) {
lower = curr;
upper = NULL;
} else {
lower = curr->prev;
}
// Checks if the new block is adjasent to both the lower and upper
// blocks.
if (lower && upper)
if ((int64_t)address ==
(int64_t)(((block_t *)lower->data)->start_address +
((block_t *)lower->data)->size) &&
(int64_t)address == (int64_t)(upper->start_address - size)) {
arena->blocks--;
arena->used_mem += size;
arena->mblocks++;
add_middle(address, size, curr->prev);
return 1;
}
// Checks if the new block is adjasent to the lower block.
if (lower) {
if ((int64_t)address < (int64_t)(((block_t *)lower->data)
->start_address + ((block_t *)lower->data)->size)) {
puts("This zone was already allocated.");
return 1;
}
if (upper)
if ((int64_t)address > (int64_t)(upper->start_address - size)) {
puts("This zone was already allocated.");
return 1;
}
if ((int64_t)address ==
(int64_t)(((block_t *)lower->data)->start_address +
((block_t *)lower->data)->size)) {
arena->used_mem += size;
arena->mblocks++;
add_last(address, size, lower, 0);
return 1;
}
}
// Checks if the block is adjasent to the upper block.
if (upper) {
if ((int64_t)address > (int64_t)(upper->start_address - size)) {
puts("This zone was already allocated.");
return 1;
}
if (lower)
if ((int64_t)address < (int64_t)(((block_t *)lower->data)
->start_address + ((block_t *)lower->data)->size)) {
puts("This zone was already allocated.");
return 1;
}
if ((int64_t)address == (int64_t)(upper->start_address - size)) {
add_first(address, size, upper);
arena->used_mem += size;
arena->mblocks++;
return 1;
}
}
return 0;
}