-
Notifications
You must be signed in to change notification settings - Fork 0
/
abmalloc.c
158 lines (135 loc) · 3.89 KB
/
abmalloc.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
#include "abmalloc.h"
#include <unistd.h>
#include <stdbool.h>
#define _ABMALLOC_ALIGNMENT_SIZE 8
typedef struct Header {
struct Header *previous, *next;
size_t size;
bool available;
} Header;
Header *first = NULL;
Header *last = NULL;
Header *header_new(Header *previous, size_t size, bool available);
void header_init(Header *header, size_t size, bool available);
void header_plug(Header *header, Header *previous, Header *next);
void *header_user_area(Header *header);
void *header_address_after_block(Header *header);
bool header_previous_available(Header* header);
bool header_next_available(Header* header);
Header *header_split(Header *header, size_t size);
Header *header_merge(Header *header);
void *abmalloc(size_t size) {
if (size == 0) {
return NULL;
}
size_t rest = size % _ABMALLOC_ALIGNMENT_SIZE;
if (rest != 0) {
size = size - rest + _ABMALLOC_ALIGNMENT_SIZE;
}
if (first == NULL) {
first = last = header_new(NULL, size, false);
return header_user_area(first);
}
Header *header = first;
while (header != NULL) {
if (header->available && (header->size >= size)) {
header = header_split(header, size);
header->available = false;
return header_user_area(header);
}
header = header->next;
}
last = header_new(last, size, false);
return header_user_area(last);
}
void abfree(void *ptr) {
if (ptr == NULL) {
return;
}
Header *header = (Header*) ptr - 1;
header = header_merge(header);
if (header == last) {
while (header_previous_available(header)) {
header = header->previous;
}
last = header->previous;
if (last != NULL) {
last->next = NULL;
} else {
first = NULL;
}
brk(header);
} else {
header->available = true;
}
}
Header *header_new(Header *previous, size_t size, bool available) {
Header *header = sbrk(sizeof(Header) + size);
header_init(header, size, available);
header_plug(header, previous, NULL);
return header;
}
void header_init( Header *header, size_t size, bool available) {
header->size = size;
header->available = available;
}
void header_plug(Header *header, Header *previous, Header *next) {
header->previous = previous;
if (previous != NULL) {
previous->next = header;
}
header->next = next;
if (next != NULL) {
next->previous = header;
}
}
void *header_user_area(Header *header) {
return header + 1;
}
void *header_address_after_block(Header *header) {
return header_user_area(header) + header->size;
}
bool header_previous_available(Header *header) {
return (header->previous != NULL) && header->previous->available;
}
bool header_next_available(Header *header) {
return (header->next != NULL) && header->next->available;
}
Header *header_merge(Header *header) {
Header *first_available = header;
Header *last_available = header;
if (header_previous_available(header)) {
first_available = header->previous;
}
if (header_next_available(header)) {
last_available = header->next;
}
if ((first_available == header) && (last_available == header)) {
return header;
}
void *end = header_address_after_block(last_available);
size_t size = end - header_user_area(first_available);
header_init(first_available, size, true);
Header *previous = first_available->previous;
Header *next = last_available->next;
header_plug(first_available, previous, next);
if (last_available == last) {
last = first_available;
}
return first_available;
}
Header *header_split(Header *header, size_t size) {
size_t original_size = header->size;
if (original_size > size + sizeof(Header)) {
header->size = original_size - size - sizeof(Header);
Header *new_header = header_address_after_block(header);
header_init(new_header, size, true);
header_plug(new_header, header, header->next);
if (header == last) {
last = new_header;
}
return new_header;
} else {
return header;
}
}