This repository was archived by the owner on Jan 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathumalloc.c
More file actions
234 lines (213 loc) · 6.15 KB
/
umalloc.c
File metadata and controls
234 lines (213 loc) · 6.15 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
#include "umalloc.h"
#include "csbrk.h"
#include <stdio.h>
#include <assert.h>
#include "ansicolors.h"
const char author[] = ANSI_BOLD ANSI_COLOR_RED "Derek Chen dyc377" ANSI_RESET;
/*
* The following helpers can be used to interact with the memory_block_t
* struct, they can be adjusted as necessary.
*/
// A sample pointer to the start of the free list.
memory_block_t *free_head;
/*
* is_allocated - returns true if a block is marked as allocated.
*/
bool is_allocated(memory_block_t *block) {
assert(block != NULL);
return block->block_size_alloc & 0x1;
}
/*
* allocate - marks a block as allocated.
*/
void allocate(memory_block_t *block) {
assert(block != NULL);
block->block_size_alloc |= 0x1;
}
/*
* deallocate - marks a block as unallocated.
*/
void deallocate(memory_block_t *block) {
assert(block != NULL);
block->block_size_alloc &= ~0x1;
}
/*
* get_size - gets the size of the block.
*/
size_t get_size(memory_block_t *block) {
assert(block != NULL);
return block->block_size_alloc & ~(ALIGNMENT-1);
}
/*
* get_next - gets the next block.
*/
memory_block_t *get_next(memory_block_t *block) {
assert(block != NULL);
return block->next;
}
/*
* put_block - puts a block struct into memory at the specified address.
* Initializes the size and allocated fields, along with NUlling out the next
* field.
*/
void put_block(memory_block_t *block, size_t size, bool alloc) {
assert(block != NULL);
assert(size % ALIGNMENT == 0);
assert(alloc >> 1 == 0);
block->block_size_alloc = size | alloc;
block->next = NULL;
}
/*
* get_payload - gets the payload of the block.
*/
void *get_payload(memory_block_t *block) {
assert(block != NULL);
return (void*)(block + 1);
}
/*
* get_block - given a payload, returns the block.
*/
memory_block_t *get_block(void *payload) {
assert(payload != NULL);
return ((memory_block_t *)payload) - 1;
}
/*
* The following are helper functions that can be implemented to assist in your
* design, but they are not required.
*/
/*
* find - finds a free block that can satisfy the umalloc request.
*/
memory_block_t *find(size_t size) {
assert(size % ALIGNMENT == 0);
memory_block_t *cur = free_head;
memory_block_t *prev = NULL;
while (cur) {
/* Iterate through the free linkedlist
Return a valid block that suits the size or NULL if none
*/
if(get_size(cur) >= size) {
return cur;
}
prev = cur;
cur = cur->next;
}
if(!cur) {
/* If no suitable blocks were found in the free list extend and get a new block
At this point, prev should point to the end of the list with cur
being the next value (null)
*/
memory_block_t *alloMem = extend(size);
if(prev) {
prev->next = alloMem;
}
return alloMem;
}
return NULL;
}
/*
* extend - extends the heap if more memory is required.
*/
memory_block_t *extend(size_t size) {
assert(size % ALIGNMENT == 0);
memory_block_t *temp = csbrk(size + ALIGNMENT);
temp->block_size_alloc = size + ALIGNMENT;
return temp;
}
/*
* split - splits a given block in parts, one allocated, one free.
*/
memory_block_t *split(memory_block_t *block, size_t size) {
assert(block != NULL);
assert(size % ALIGNMENT == 0);
memory_block_t *endBlock = (memory_block_t*)((unsigned long)block + (block->block_size_alloc - size));
block->block_size_alloc = block->block_size_alloc - size;
/* So turns out using put_block was constantly incrementing the size I gave due to the bitwise OR..... that would've been nice to find when I was debugging at 3 AM
Trying to set the block_size_alloc manually now and it seems to work as intended yay.
*/
endBlock->block_size_alloc = size;
return endBlock;
}
/*
* coalesce - coalesces a free memory block with neighbors.
*/
memory_block_t *coalesce(memory_block_t *block) {
//* STUDENT TODO
return NULL;
}
/*
* uinit - Used initialize metadata required to manage the heap
* along with allocating initial memory.
*/
int uinit() {
free_head = csbrk(PAGESIZE + ALIGNMENT);
return free_head ? 0 : -1;
}
/*
* removeBlock - removes the given block from the LinkedList
*/
memory_block_t *removeBlock(memory_block_t *tgt) {
assert(tgt != NULL);
memory_block_t *cur = free_head;
memory_block_t *prev = NULL;
while (cur) {
if(cur == tgt) {
if(!prev) {
// If the head is to be removed
free_head = cur->next;
} else {
// Block to be removed has a previous value
prev->next = cur->next;
}
return cur;
}
prev = cur;
cur = cur->next;
}
return NULL;
}
/*
* umalloc - allocates size bytes and returns a pointer to the allocated memory.
*/
void *umalloc(size_t size) {
// Change size to aligned
size_t alignedSize = ALIGN(ALIGNMENT + size);
memory_block_t *alloMem = find(alignedSize);
if ((get_size(alloMem) * 0.5) >= alignedSize) {
// If threshold is met, we split the block
memory_block_t *splitMem = split(alloMem, alignedSize);
allocate(splitMem);
return get_payload(splitMem);
}
// Otherwise remove the block from the list and give to user
removeBlock(alloMem);
allocate(alloMem);
return get_payload(alloMem);
}
/*
* ufree - frees the memory space pointed to by ptr, which must have been called
* by a previous call to malloc.
*/
void ufree(void *ptr) {
memory_block_t *block = get_block(ptr);
deallocate(block);
if(!free_head) {
// Makes free_head a block if it is null (list is empty)
free_head = block;
return;
}
memory_block_t *cur = free_head;
memory_block_t *prev = NULL;
while (cur) {
if((long)block > (long)cur) {
// Found spot where block is greater than cur in the list, insert it in
block->next = cur->next;
cur->next = block;
return;
}
prev = cur;
cur = cur->next;
}
// If cur is null (reached the end), add block to the end
prev->next = block;
}