-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.c
288 lines (246 loc) · 6.58 KB
/
mm.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include "mm.h"
#include "memlib.h"
#define MAX_HEAP (64*(1<<20)) /* 64 MB */
/*********************************************************
* NOTE TO STUDENTS: Before you do anything else, please
* provide your information in the following struct.
********************************************************/
team_t team = {
/* Team name : Your student ID */
"2016-81755",
/* Your full name */
"Kozar Anastasia",
/* Your student ID, again */
"2016-81755",
/* leave blank */
"",
/* leave blank */
""
};
#define N 26
static char *heap_start;
static char *free_lists[N];
/* DON'T MODIFY THIS VALUE AND LEAVE IT AS IT WAS */
static range_t **gl_ranges;
/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8
#define SIZE 4
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(SIZE))
/*
* remove_range - manipulate range lists
* DON'T MODIFY THIS FUNCTION AND LEAVE IT AS IT WAS
*/
static void remove_range(range_t **ranges, char *lo)
{
range_t *p;
range_t **prevpp = ranges;
if (!ranges)
return;
for (p = *ranges; p != NULL; p = p->next) {
if (p->lo == lo) {
*prevpp = p->next;
free(p);
break;
}
prevpp = &(p->next);
}
}
//add block in free lists
void add_free_block(char* p) {
size_t size = *(size_t*)p & -2;
int i = 0;
char* index;
*(size_t*)p = size;
while(size > 1) {
size >>= 1;
i++;
}
index = free_lists[i];
if (index == NULL) {
free_lists[i] = p;
*(size_t*)(p + 4) = 0;
return;
}
else {
while((char*)(*(size_t*)(index + 4)) != NULL) {
index = (char*)(*(size_t*)(index + 4));
}
*(size_t*)(index + 4) = (size_t*)p;
*(size_t*)(p + 4) = 0;
}
}
/*
* mm_init - initialize the malloc package.
*/
int mm_init(range_t **ranges)
{
int i = 0;
heap_start = mem_heap_lo();
mem_sbrk(MAX_HEAP);
size_t size = MAX_HEAP;
*(size_t*)(heap_start + SIZE) = 9;
*(size_t*)(heap_start + 2 * SIZE) = 9;
*(size_t*)(heap_start + size - SIZE) = 1;
*(size_t*)(heap_start + 3 * SIZE) = size - 16;
*(size_t*)(heap_start + size - 2 * SIZE) = size - 16;
heap_start += 12;
for(i = 0; i < N; i++) {
free_lists[i] = NULL;
}
add_free_block(heap_start);
/* DON't MODIFY THIS STAGE AND LEAVE IT AS IT WAS */
gl_ranges = ranges;
return 0;
}
//remove block from lists
void remove_block(char* p)
{
size_t size = *(size_t*)p & -2;
int i = 0;
char* index;
*(size_t*)p = size;
while(size > 1) {
size >>= 1;
i++;
}
index = free_lists[i];
if (index == NULL)
return;
else {
if(index == p) {
free_lists[i] = (char*)(*(size_t*)(index + 4));
return;
}
while((char*)(*(size_t*)(index + 4)) != NULL) {
if((char*)(*(size_t*)(index + 4)) == p) {
*(size_t*)(index + 4) = *(size_t*)(p + 4);
return;
}
index = (char*)(*(size_t*)(index + 4));
}
}
}
//split current block in 2, one with size = size and
//other on what is left
char* split(char* ptr, size_t size) {
remove_block(ptr);
size_t old_size = *(size_t*)ptr & -2;
if(*(size_t*)ptr == 1) //end of heap
return NULL;
if(old_size - size <= 8) { //not too small
*(size_t*)ptr = old_size | 1;
*(size_t*)(ptr + old_size - SIZE) = old_size | 1;
return ptr;
}
*(size_t*)ptr = size | 1;
*(size_t*)(ptr + size) = old_size - size;
*(size_t*)(ptr + old_size - SIZE) = old_size - size;
*(size_t*)(ptr + size - SIZE) = size | 1;
add_free_block(ptr + size);
return ptr;
}
//finds appropriate block in list
char* find_block(size_t size)
{
size_t init_size = size;
int i = 0;
char* index;
while(size > 1) {
size >>= 1;
i++;
}
while (i < N){
index = free_lists[i];
while (index != NULL && *(size_t*)index < init_size) {
index = (char*)(*(size_t*)(index + 4));
}
if (index != NULL) {
return split(index, init_size);
}
i++;
}
return NULL;
}
/*
* mm_malloc - Allocate a block by incrementing the brk pointer.
* Always allocate a block whose size is a multiple of the alignment.
*/
void* mm_malloc(size_t size)
{
if(size == 0)
return NULL;
size_t new_size = ALIGN(size + SIZE_T_SIZE);
if(new_size > MAX_HEAP)
return NULL;
char *p = find_block(new_size);
return p + SIZE;
}
//Checks if prev or next block is free and merge if it is
void merge(char* ptr) {
size_t new_size = 0;
if(!(*(size_t*)(ptr - 4) % 2)) { //prev block is also free
remove_block(ptr - *(size_t*)(ptr - 4));
remove_block(ptr);
new_size = *(size_t*)ptr + *(size_t*)(ptr - 4);
*(size_t*)(ptr - *(size_t*)(ptr - 4)) = new_size;
*(size_t*)(ptr + *(size_t*)ptr - 4) = new_size;
add_free_block(ptr - *(size_t*)(ptr - 4));
}
else if(!(*(size_t*)(ptr + *(size_t*)ptr) % 2)) { //next block is also free
remove_block(ptr);
remove_block(ptr + *(size_t*)ptr);
new_size = *(size_t*)ptr + *(size_t*)(ptr + *(size_t*)ptr);
*(size_t*)(ptr + new_size - 4) = new_size;
*(size_t*)ptr = new_size;
add_free_block(ptr);
}
}
// mm_free - Freeing a block does nothing.
//Mark block from pointer as free and call merge
void mm_free(void *ptr)
{
if(ptr == NULL)
return;
char* p = ptr - 4;
size_t size = *(size_t*)p & -2;
if(!(*p % 2)) {
printf("Something is wrong with your intentions");
return;
}
*(size_t*)(p + size - 4) = size;
*(size_t*)p = size;
add_free_block(p);
merge(p);
// DON't MODIFY THIS STAGE AND LEAVE IT AS IT WAS
if (gl_ranges)
remove_range(gl_ranges, ptr);
}
/*
* mm_realloc - empty implementation; YOU DO NOT NEED TO IMPLEMENT THIS
*/
void* mm_realloc(void *ptr, size_t t)
{
return NULL;
}
/*
* mm_exit - finalize the malloc package.
Goes through whole heap and checks for not freed blocks
*/
void mm_exit(void)
{
char* tmp;
char* p = heap_start;
while(*(size_t*)p != 1) {
tmp = p + (*(size_t*)p & -2);
if(*(size_t*)p % 2)
mm_free(p + 4);
p = tmp;
}
}