-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc2.c
99 lines (78 loc) · 1.77 KB
/
malloc2.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
/* malloc() and free() for `char` type */
#include "hdr.h"
typedef struct node {
size_t len;
struct node *prev;
struct node *next;
char *buf;
} node_t;
/* free list */
static node_t *head;
char *malloc2(size_t size){
char *p;
int counter = 0;
printf("program break(counter=%d): %10p\n", counter++, sbrk(0));
if (head == NULL) {
p = (char *)sbrk(size + 2); /* extra two bytes for '\0' and size */
if (p == (char *) -1) {
fprintf(stderr, "sbrk failed, errno=%d\n", errno);
return NULL;
}
*p = (char) size;
printf("program break(counter=%d): %10p\n", counter++, sbrk(0));
return p + 1;
} else {
/* first fit allocator */
node_t *tmp = head;
while(tmp != NULL) {
if(tmp->len > size) {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
printf("program break(counter=%d): %10p\n", counter++, sbrk(0));
return tmp->buf;
}
tmp = tmp->next;
}
p = (char *)sbrk(size + 2);
if (p == (char *) -1) {
fprintf(stderr, "sbrk failed, errono=%d\n", errno);
return NULL;
}
*p = (char) size;
printf("program break(counter=%d): %10p\n", counter++, sbrk(0));
return p + 1;
}
}
void free2(char *ptr){
node_t e, *tmp;
e.len = *(ptr - 1);
e.next = NULL; /* tail */
e.buf = NULL; /* derefrence */
if (head == NULL){
e.prev = NULL;
head = &e;
} else {
/* append to free list */
tmp = head;
while(tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = &e;
e.prev = tmp;
}
ptr = NULL;
}
int main() {
char *buf1, *buf2;
buf1 = malloc2(5); /* "hello" */
buf2 = malloc2(5); /* "world" */
memcpy(buf1, "hello", 5);
memcpy(buf2, "world", 5);
printf("buf1: %s\n", buf1);
printf("buf2: %s\n", buf2);
free2(buf1);
free2(buf2);
/* seg fault, double free */
printf("buf1: %s\n", buf1);
return 0;
}