-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm.c
352 lines (290 loc) · 7.09 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
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
338
/*
* mm-naive.c - The fastest, least memory-efficient malloc package.
*
* In this naive approach, a block is allocated by simply incrementing
* the brk pointer. A block is pure payload. There are no headers or
* footers. Blocks are never coalesced or reused. Realloc is
* implemented directly using mm_malloc and mm_free.
*
* NOTE TO STUDENTS: Replace this header comment with your own header
* comment that gives a high level description of your solution.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include "mm.h"
#include "memlib.h"
/*********************************************************
* NOTE TO STUDENTS: Before you do anything else, please
* provide your team information in the following struct.
********************************************************/
team_t team = {
/* Team name */
"LteaM",
/* First member's full name */
"Louis Mutricy",
/* First member's email address */
/* Second member's full name (leave blank if none) */
"",
/* Second member's email address (leave blank if none) */
"",
};
/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
/*
* mm_init - initialize the malloc package.
*/
typedef struct block block;
struct block{
block * next;
};
void* end;
block* last;
int enableDefrag =1; // use to disable defragmentatiom operations during reallloc and for perf comparaisons
int part =1;// number of part in memory (ie 1+#malloc-#free)
block* getNext(block *b){
block * bNext = b->next;
int bInt = (int)bNext;
bInt = bInt&(~1);
return((block*) bInt);
}
block * getFirst(){
void * first =mem_heap_lo();
//if (verbose) printf("%d \n",(int ) block);
return (block*) first;
}
int isFree(block* b){
if (b->next==NULL) return 0;
int address = (int) b->next;
//if (verbose) printf("%d \n",1 &address);
return (1&address);
}
/* block structure (size,pointer next)*/
block * findNextFree(block* b){
//unsigned char =mem_heap_lo(void);
//block* b = getFirst();
while ((!isFree(b))&&( (void*)b!=last)){
// printf("trying %p\n",b);
b = getNext(b);
}
if (!isFree(b))return NULL;
// printf("free block found : %p\n",(void*)b);
return b;
}
int getSize(block* b){
if ((void *)getNext(b) ==end) {
//printf("Warning : last Block");
return(((char*)end-(char*)b)-8);
}
return (((char*)getNext(b))-((char*)b)-8);
}
block * getLast(){
block* b = getFirst();
while((void *)getNext(b)!=end){
b = getNext(b);
}
return b;
}
// return address with flag 1
block* setfree(block* b){
int acc= (int)b;
acc = acc |1;
return ((block*)acc);
}
//remove flag or set it to 0
block* clear(block* b){
int acc = (int)b;
acc = acc & (~1);
return ((block*)acc);
}
void increaseSize(int size){
void* n =mem_sbrk(size);
block * b =last;
end =(void*)(((char*) mem_heap_hi())+1);
//case 1 : last block is free.
if (isFree(b)){
// end = mem_heap_hi();
// end =(void*)(((char*) mem_heap_hi())+1);
b->next = setfree(end);
}
//case 2 b is not free
else {
block* newb = (block*) n;
last->next= newb;
newb->next= setfree(end);
}
}
void defragmente(block *b){
//printf("test\n");
if (b>last)return;
block * n=b;
while ((n!=last)&&(isFree(n))){
n= getNext(n);
b->next=setfree(n);
}
//printf("critical : n %p b: %p max: %p, bn: %p \n",n,b,mem_heap_hi(),(void*)getNext(b));
}
block * findFirstFree(int size){
block * b =getFirst();
b= findNextFree(b);
block * defragmented = NULL; // last block defragmented
while(b!=NULL){
if (getSize(b)>=size) break;
// special case where the last block is free but too small
// pb : getNext(b) is out of bound
if (b==last){
b=NULL;
break;
}
if(isFree(getNext(b))&&(defragmented!=b)){
//printf("defragmenting at %p\n",b);
defragmented =b;
if (enableDefrag)defragmente(b);
}
else b=findNextFree(getNext(b));
}
if (b==NULL){
increaseSize(size+8);
return getLast();
}
return b;
}
// alternative strattegy to allocate but seems less efficient
block* findOneFree(int size){
int found =0;
block * result=NULL;
block * b =getFirst();
b= findNextFree(b);
block * defragmented = NULL; // last block defragmented
while(b!=NULL){
if (getSize(b)>=size) {
if (!found)result =b;
else {
if (getSize(result)>getSize(b))result=b;
}
found=1;
if(getSize(b)<=2*size){
result=b;
break;
}//only take small parts
}
// special case where the last block is free but too small
// pb : getNext(b) is out of bound
if (b==last){
b=NULL;
break;
}
if(isFree(getNext(b))&&(defragmented!=b)){
//printf("defragmenting at %p\n",b);
defragmented =b;
if (enableDefrag)defragmente(b);
}
else b=findNextFree(getNext(b));
}
if (!found){
increaseSize(size+8-getSize(last));
return getLast();
}
return result;
}
//choose strategy ...
block * findOpt(int size){
// if (part>100){
// printf("part %d\n",part);
// return findOneFree(size);
// }
// printf("%d\n",part);
return findFirstFree(size);
}
void take(block *b,int size){
//block* nextblock=getNext(b);
if(getSize(b)-size>8){
block * newb = (block*)((char*)b+size+8);
newb ->next =(block*)(((int)getNext(b))|1);
b->next= (block*)(((int)newb)&(~1));
if(b==last){
last=newb;
}
return;
}
b->next= (block*)(((int)getNext(b))&(~1));
}
int mm_init(void)
{
void * first = mem_sbrk(16);
end =(void*)(((char*) mem_heap_hi())+1);
block * b = (block*) first;
b ->next = (block*) (((int) end)|1);
last = getLast();
return 0;
}
/*
* 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)
{
//printf("Try to allocate %d\n",(int)size);
//int newsize = ALIGN(size + SIZE_T_SIZE);
int newsize =ALIGN(size+8);
block* b= findOpt(newsize);
if (((char*)b+newsize)-((char*)mem_heap_hi())>0){
printf("ran out of memory\n");
return NULL;
}
//printf("allocate at %p\n",(void*)b);
take(b,newsize -8);
part++;
return((void*)((( char*) b)+8));
}
/*
* mm_free - just set the flag to free
*/
void mm_free(void *ptr)
{
if (ptr==NULL)return;
part --;
block *b = (block*)((char*)ptr-8);
b->next= setfree(b->next);
defragmente(b);
}
/*
* mm_realloc - Implemented simply in terms of mm_malloc and mm_free
*/
void *mm_realloc(void *ptr, size_t size)
{
enableDefrag=0;
if (size==0){
enableDefrag=1;
mm_free(ptr);
return NULL;
}
if (ptr==NULL){
enableDefrag=1;
return mm_malloc(size);
}
void *oldptr = ptr;
void * newptr;
size_t copySize;
size_t oldSize;
block * oldb = (block *)((char*)ptr - 8);
oldSize =(size_t) getSize(oldb);
mm_free(ptr);
newptr = mm_malloc(size);
if (newptr ==NULL){
enableDefrag=1;
printf("Realloc failed due to malloc failed \n");
return NULL;
}
copySize = oldSize;
if (copySize >size)copySize=size;
memcpy(newptr,oldptr,copySize);
enableDefrag=1;
return newptr;
}