-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmalloc.c
More file actions
executable file
·172 lines (154 loc) · 3.69 KB
/
malloc.c
File metadata and controls
executable file
·172 lines (154 loc) · 3.69 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
#include "malloc.h"
#define align4(x) ((((x) - 1) >> 2) << 2) + 4
#define BLOCK_SIZE 20
void *base = NULL;
t_block extend_heap(t_block last, size_t size);
t_block find_block(t_block last, size_t size);
t_block get_block(void *p);
void split_block(t_block b, size_t size);
void copy_block(t_block src, t_block dst);
t_block find_block(t_block last, size_t size){
t_block b = base;
while (b && !(b->free && b->size >= size)){
last = b;
b = b->next;
}
return b;
}
t_block extend_heap(t_block last, size_t size){
size_t sb;
t_block b;
b = sbrk(0);
sb = (size_t)sbrk(BLOCK_SIZE + size);
if(sb < 0) return NULL;
b->size = size;
b->next = NULL;
b->prev = last;
b->ptr = b->data;
if(last) last->next = b;
b->free = 0;
return b;
}
void split_block(t_block b, size_t size){
t_block new;
new = (t_block)(b->data + size);
new->size = b->size - size - BLOCK_SIZE;
new->next = b->next;
new->prev = b;
new->free = 1;
new->ptr = new->data;
b->size = size;
b->next = new;
if(new->next) new->next->prev = new;
}
void *malloc(size_t size){
t_block b, last;
size_t s;
s = align4(size);
if(base){
last = base;
b = find_block(last, s);
if(b){
if((b->size - s) >= (BLOCK_SIZE + 4)) split_block(b, s);
b->free = 0;
}
else{
b = extend_heap(last, s);
if(!b) return NULL;
}
}
else{
b = extend_heap(NULL, s);
if(!b) return NULL;
base = b;
}
return b->data;
}
void *calloc(size_t number, size_t size){
size_t *new;
size_t s4, i;
new = malloc(number * size);
if (new){
s4 = align4(number * size) << 2;
for (i = 0; i < s4; i++) new[i] = 0;
}
return new;
}
t_block fusion(t_block b){
if(b->next && b->next->free){
b->size += BLOCK_SIZE + b->next->size;
b->next = b->next->next;
if (b->next) b->next->prev = b;
}
return b;
}
t_block get_block(void *p){
char *tmp;
tmp = p;
return (p = tmp -= BLOCK_SIZE);
}
int valid_addr(void *p){
if (base){
if(p > base && p < sbrk(0)) return (p == (get_block(p))->ptr);
}
return 0;
}
void free(void *p){
t_block b;
if (valid_addr(p)){
b = get_block(p);
b->free = 1;
if(b->prev && b->prev->free) b = fusion(b->prev);
if(b->next){
fusion(b);
}
else{
if(b->prev) b->prev->next = NULL;
else base = NULL;
brk(b);
}
}
}
void copy_block(t_block src, t_block dst){
int *sdata, *ddata;
size_t i;
sdata = src->ptr;
ddata = dst->ptr;
for(i = 0; i * 4 < src->size && i * 4 < dst->size; i++)
ddata[i] = sdata[i];
}
void *realloc(void *p, size_t size){
size_t s;
t_block b, new;
void *newp;
if(!p) return (malloc(size));
if (valid_addr(p)){
s = align4(size);
b = get_block(p);
if (b->size >= s){
if(b->size - s >= (BLOCK_SIZE + 4)) split_block(b, s);
}
else{
if(b->next && b->next->free && (b->size + BLOCK_SIZE + b->next->size) >= s){
fusion(b);
if(b->size - s >= (BLOCK_SIZE + 4)) split_block(b, s);
}
else{
newp = malloc(s);
if (!newp) return NULL;
new = get_block(newp);
copy_block(b, new);
free(p);
return newp;
}
}
return p;
}
return NULL;
}
void *reallocf(void *p, size_t size){
void *newp;
newp = realloc(p, size);
if (!newp) free(p);
return newp;
}