-
Notifications
You must be signed in to change notification settings - Fork 1
/
janitor.c
193 lines (149 loc) · 4.62 KB
/
janitor.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
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
#include "janitor.h"
/* keep a head and tail block */
janitor_t *head, *tail;
/* implement mutex lock to prevent races */
pthread_mutex_t global_malloc_lock;
static janitor_t *
get_free_block(size_t size)
{
/* set head block as current block */
janitor_t *current = head;
while (current) {
/* check if block is marked free and if ample space is provided for allocation */
if (current->flag && current->size >= size)
return current;
/* check next block */
current = current->next;
}
return NULL;
}
void *
jmalloc(size_t size)
{
size_t total_size;
void *block;
janitor_t *header;
/* error-check size */
if (!size)
return NULL;
/* critical section start */
pthread_mutex_lock(&global_malloc_lock);
/* first-fit: check if there already is a block size that meets our allocation size and immediately fill it and return */
header = get_free_block(size);
if (header) {
header->flag = 0;
pthread_mutex_unlock(&global_malloc_lock);
return (void *) (header + 1);
}
/* if not found, continue by extending the size of the heap with sbrk, extending the break to meet our allocation */
total_size = BLOCK_SIZE + size;
block = sbrk(total_size);
if (block == (void *) -1 ) {
pthread_mutex_unlock(&global_malloc_lock);
return NULL;
}
/* set struct entries with allocation specification and mark as not free */
header = block;
header->size = size;
header->flag = 0;
header->next = NULL;
/* switch context to next free block
* - if there is no head block for the list, set header as head
* - if a tail block is present, set the next element to point to header, now the new tail
*/
if (!head)
head = header;
if (tail)
tail->next = header;
tail = header;
/* unlock critical section */
pthread_mutex_unlock(&global_malloc_lock);
/* returned memory after break */
return (void *)(header + 1);
}
void *
jcalloc(size_t num, size_t size)
{
size_t total_size;
void *block;
/* check if parameters were provided */
if (!num || !size)
return (void *) NULL;
/* check if size_t bounds adhere to multiplicative inverse properties */
total_size = num * size;
if (size != total_size / num)
return (void *) NULL;
/* perform conventional malloc with total_size */
block = jmalloc(total_size);
if (!block)
return (void *) NULL;
/* zero out our newly heap allocated block */
memset(block, 0, size);
return block;
}
void
jfree(void *block)
{
janitor_t *header, *tmp;
void * programbreak;
/* if the block is provided */
if (!block)
return (void) NULL;
/* start critical section */
pthread_mutex_lock(&global_malloc_lock);
/* set header as previous block */
header = (janitor_t *) block - 1;
/* start programbreak at byte 0 */
programbreak = sbrk(0);
/* start to NULL out block until break point of heap
* NOTE: header (previous block) size + target block should meet
*/
if (( char *) block + header->size == programbreak){
/* check if block is only allocated block (since it is both head and tail), and NULL */
if (head == tail)
head = tail = NULL;
else {
/* copy head into tmp block, NULL each block from tail back to head */
tmp = head;
while (tmp) {
if (tmp->next == tail){
tmp->next = NULL;
tail = tmp;
}
tmp = tmp->next;
}
}
/* move break back to memory address after deallocation */
sbrk(0 - BLOCK_SIZE - header->size);
/* unlock critical section*/
pthread_mutex_unlock(&global_malloc_lock);
/* returns nothing */
return (void) NULL;
}
/* set flag to unmarked */
header->flag = 1;
/* unlock critical section */
pthread_mutex_unlock(&global_malloc_lock);
}
void *
jrealloc(void *block, size_t size)
{
janitor_t *header;
void *ret;
/* create a new block if parameters not set */
if (!block)
return jmalloc(size);
/* set header to be block's previous bit */
header = (janitor_t *) block - 1;
/* check if headers size is greater than specified paramater */
if (header->size >= size)
return block;
/* create a new block allocation */
ret = jmalloc(size);
/* add content from previous block to newly allocated block */
if (ret) {
memcpy(ret, block, header->size);
jfree(block);
}
return ret;
}