forked from AEIS-Lab/myhw-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.c
165 lines (158 loc) · 3.9 KB
/
run.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
#include <stdio.h>
#include <sys/types.h>
#include <limits.h>
#include "run.h"
#include "util.h"
void *base = 0;
void *end = 0;
p_meta find_meta(p_meta *last, size_t size) {
p_meta index = base;
p_meta result = -1;
if(base!=last){
switch(fit_flag){
case FIRST_FIT:
{
do{
// fprintf(stderr,"index : %d,size : %d,last : %d\n",index,index->size,last);
if(result == -1 && index->free && index->size>=size) result=index;
if(result == -1 && !index->next) result=index;
index = index->next;
}while(index);
}
break;
case BEST_FIT:
{
do{
if(index->free && index->size>=size){
if(result==-1) result=index;
else if(result->size>index->size) result = index;
}
if(result == -1 && !index->next) result=index;
index = index->next;
}while(index);
}
break;
case WORST_FIT:
{
do{
if(index->free && index->size>=size){
if(result==-1) result=index;
else if(result->size < index->size) result = index;
}
if(result == -1 && !index->next) result=index;
index = index->next;
}while(index);
}
break;
}
}
return result;
}
void *m_malloc(size_t size) {
if(base == 0) {
base = sbrk(0);
end = base;
}
size = (size+3)/4*4;
int length = size + META_SIZE;
p_meta target = find_meta(end,size);
if(target==-1 || (!target->next && (!target->free || target->free && target->size<size))){
// fprintf(stderr,"target no! \n");
p_meta new_target = end;
end += length;
if(brk(end) == -1) return ((void*)0);
new_target->free = 0;
new_target->next = 0;
new_target->prev = target;
new_target->size = size;
if(target!=-1) target->next = new_target;
target = new_target;
}
else{
m_realloc(target->data,size);
}
// fprintf(stderr,"return\n");
return target->data;
}
void m_free(void *ptr) {
p_meta cur = ptr-META_SIZE;
cur->free = 1;
if(cur->next && cur->next->free == 1) {
cur->size +=cur->next->size + META_SIZE;
cur->next = cur->next->next;
}
if(cur->prev!=-1){
if(cur->prev->free){
cur = cur->prev;
cur->size +=cur->next->size + META_SIZE;
cur->next = cur->next->next;
}
if(!cur->next){
end-=cur->size + META_SIZE;
cur->prev->next = 0;
}
}
else if(!cur->next && !cur->prev){
end = base;
}
/* p_meta b_meta = base;
if(b_meta->free){
// fprintf(stderr,"first free\n");
b_meta = b_meta->next;
base = (void*)b_meta;
b_meta->prev = -1;
}*/
ptr = 0;
}
void *m_realloc(void* ptr, size_t size){
p_meta cur = ptr-META_SIZE;
size = (size+3)/4*4;
// fprintf(stderr,"cur->size : %d size : %d \n",cur->size, size);
if(cur->size == size) return ptr;
else if(cur->size < size){
if(cur->next && cur->next->free
&& cur->size + cur->next->size + META_SIZE>=size){
cur->size += cur->next->size + META_SIZE;
cur->next = cur->next->next;
cur->next->prev = cur;
if(cur->size-size < META_SIZE){
return ptr;
}
else{
// fprintf(stderr,"divide\n");
p_meta next = (int)cur + size + META_SIZE;
// fprintf(stderr,"%d + %d = %d\n",cur,size+META_SIZE,next);
next->prev = cur;
next->next = cur->next;
next->size = cur->size - size - META_SIZE;
cur->next = next;
cur->size = size;
cur->free = 0;
m_free(next->data);
return cur->data;
}
}
else {
m_free(cur->data);
void * new_ptr = m_malloc(size);
strcpy(new_ptr, ptr);
return new_ptr;
}
}
else if(cur->size-size < META_SIZE){
return ptr;
}
else{
// fprintf(stderr,"divide\n");
p_meta next = (int)cur + size + META_SIZE;
// fprintf(stderr,"%d + %d = %d\n",cur,size+META_SIZE,next);
next->prev = cur;
next->next = cur->next;
next->size = cur->size - size - META_SIZE;
cur->next = next;
cur->size = size;
cur->free = 0;
m_free(next->data);
return cur->data;
}
}