-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc.c
More file actions
239 lines (193 loc) · 4.6 KB
/
malloc.c
File metadata and controls
239 lines (193 loc) · 4.6 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
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct s_block *t_block;
void* base = NULL;
struct s_block
{
size_t size;
t_block next;
t_block prev;
int free;
void* ptr;
char data[1];
};
const size_t BLOCK_SIZE = sizeof(struct s_block) - sizeof(char*);
#define align4(x) (((((x) - 1) >> 2) << 2) + 4)
t_block find_block(t_block last, size_t size)
{
t_block b = base;
while (b != NULL && !(b->free && b->size <= size))
{
last = b;
b = b->next;
}
return b;
}
t_block extend_heap(t_block last, size_t size)
{
int sb;
t_block b;
b = (t_block) sbrk(0);
sb = (int) sbrk(BLOCK_SIZE + size);
if (sb < 0) return NULL;
b->size = size;
b->free = 0;
b->next = NULL;
b->prev = last;
b->ptr = b->data;
if (last) last->next = b;
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 = align4(size);
if (base == NULL) {
b = extend_heap(NULL, s);
if (b == NULL) return NULL;
base = b;
} else {
last = base;
b = find_block(last, s);
if (b != NULL) {
if (b->size - s > BLOCK_SIZE + 4)
split_block(b, s);
b->free = 0;
} else {
b = extend_heap(last, s);
if (b == NULL) return NULL;
}
}
return b->data;
}
void* __calloc(size_t number, size_t size) {
size_t *new;
size_t s4;
new = __malloc(number * size);
if (new != NULL) {
s4 = align4(number * size) << 2;
for (int i = 0; i < s4; i++) new[i] = 0;
}
return new;
}
t_block fusion(t_block b) {
if (b->next != NULL && b->next->free) {
b->size += b->next->size + BLOCK_SIZE;
b->next = b->next->next;
if (b->next != NULL) {
b->next->prev = b;
}
}
return b;
}
t_block get_block(void* p) {
char* tmp = p;
tmp -= BLOCK_SIZE;
return (t_block) tmp;
}
int valid_adress(void* p) {
if (base != NULL) {
if (p > base && p < sbrk(0))
return (p == get_block(p)->ptr);
}
return 0;
}
void __free(void* p) {
t_block b;
if (valid_adress(p)) {
b = get_block(p);
b->free = 1;
if (b->prev != NULL && b->prev->free) {
fusion(b->prev);
}
if (b->next != NULL) {
fusion(b);
} else {
if (b->prev != NULL)
b->prev->next = NULL;
else
base = NULL;
brk(b);
}
}
}
void copy_block(t_block src, t_block dst) {
int* sdata;
int* ddata;
sdata = (int*) src->data;
ddata = (int*) dst->data;
for (int 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 == NULL)
return __malloc(size);
if (valid_adress(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 != NULL && 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 == NULL) return NULL;
new = get_block(newp);
copy_block(b, new);
__free(p);
return newp;
}
}
return p;
}
return NULL;
}
int main(void) {
int* ptr = NULL;
int* rptr = NULL;
printf("__calloc 10 ints:\n");
ptr = __calloc(10, sizeof(int));
if (ptr == NULL) {
printf("Failed to __calloc!\n");
exit(-1);
}
for (int i = 0; i < 10; i++) {
ptr[i] = i;
printf("%d\n", ptr[i]);
}
printf("__realloc to 20 ints:\n");
rptr = __realloc(ptr, sizeof(int) * 20);
if (rptr == NULL) {
printf("Failed to __realloc!\n");
exit(-1);
}
for (int i = 10; i < 20; i++) {
rptr[i] = i;
printf("%d\n", rptr[i]);
}
__free(ptr);
__free(rptr);
return 0;
}