-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_v6.c
More file actions
232 lines (182 loc) · 5.6 KB
/
Copy pathmalloc_v6.c
File metadata and controls
232 lines (182 loc) · 5.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
//mmap
#include<stdio.h>
#include<unistd.h>
#include<stddef.h>
#include<sys/mman.h>
#define MMAP_THRESHOLD (128*1024)
/**
* struct block_meta - metadata header for each allocated block
* @size: size of user data reigion in bytes
* @is_free: nonzero if free, 0 if allocated
* @next: pointer to the next metadata block in the list
*/
struct block_meta
{
size_t size;
int is_free;
int is_mmap;
struct block_meta *next;
} __attribute__((aligned(16)));
static struct block_meta *head = NULL;
static struct block_meta *tail = NULL;
/**
* find_free_block - finds a free block large enough for allocation
* @size: size of user data
*
* Performs a first-fit search over the block list starting at @head and
* returns the first free block whose size is greater than or equal to @size
*
* Return:
* Pointer to a suitable free block on success, or NULL if no such block exists
*/
static struct block_meta* find_free_block(size_t size)
{
//search if free block of enough size is available to reuse -- first fit
struct block_meta *curr = head;
while(curr != NULL)
{
if(curr->is_mmap) continue;
if(curr->is_free==1 && curr->size >= size)
{
return curr;
}
curr = curr->next;
}
return NULL;
}
/**
* split_block - splits a block into required size
* @block - pointer to free block header
* @size - required size
*/
static void split_block(struct block_meta *block, size_t size)
{
//block splitting
// | free | ----> | required_size | remainder |
struct block_meta *remainder = (struct block_meta*)((char*)(block + 1) + size + sizeof(size_t));
// assigning meta data to new block
remainder->size = block->size - size - sizeof(struct block_meta) - sizeof(size_t);
remainder->is_free = 1;
remainder->next = block->next;
//editing footer
size_t *footer = (size_t*)((char*)(remainder+1) + remainder->size);
*footer = remainder->size;
//editing free block's meta data
block->size = size;
block->next = remainder;
footer = (size_t*)((char*)(block+1) + block->size);
*footer = block->size;
if(block == tail)
tail = remainder;
}
//foward coalescing
void forward_coalesce(struct block_meta *curr_block)
{
//check if it's the last block in physical memory
if((void*)((char*)(curr_block+1)+curr_block->size) >= sbrk(0)) return;
struct block_meta *next_block = (struct block_meta*)((char*)(curr_block+1)+curr_block->size + sizeof(size_t));
if(!next_block->is_free) return;
curr_block->size = curr_block->size + sizeof(size_t) + sizeof(struct block_meta) + next_block->size;
curr_block->next = next_block->next;
size_t *footer = (size_t*)((char*)(curr_block+1) + curr_block->size);
*footer = curr_block->size;
}
//backward coalescing
void backward_coalesce(struct block_meta *curr_block)
{
if(curr_block == head) return;
size_t *footer = (size_t*)((char*)curr_block - sizeof(size_t));
struct block_meta *prev_block = (struct block_meta*)((char*)footer - *footer - sizeof(struct block_meta));
if(!prev_block->is_free) return;
prev_block->size = prev_block->size + sizeof(size_t) + sizeof(struct block_meta) + curr_block->size;
prev_block->next = curr_block->next;
if(curr_block == tail) tail = prev_block;
footer = (size_t*)((char*)(prev_block+1) + prev_block->size);
*footer = prev_block->size;
}
/**
* malloc - allocates heap memory
* @size: memory in bytes to be allocated
*
* Allocates memory block of at least @size bytes. If free block is available,
* then the allocator reuses that, else the heap is extended using sbrk() and a new
* block is created.
*
* Returns:
* Pointer to the start of the memory block
*/
void* malloc(size_t size)
{
struct block_meta *block;
if(block = find_free_block(size))
{
block->is_free = 0;
if(block->size > sizeof(struct block_meta) + sizeof(size_t) + 16)
split_block(block, size);
}
else
{
if(size >= MMAP_THRESHOLD)
{
block = mmap(NULL, size + sizeof(struct block_meta), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if(block == MAP_FAILED) return NULL;
block->is_mmap = 1;
return block;
}
block = sbrk(0);
//allocting space for user data, meta data and footer
if( sbrk(size + sizeof(struct block_meta) + sizeof(size_t)) == (void *)-1 ) return NULL;
block->is_mmap = 0;
size_t *footer = (size_t*)((char*)(block+1) + size);
*footer = size;
//meta data struct would start at the heap top
block->size = size;
block->is_free=0;
block->next=NULL;
//adding to metadata linkedlist
if(tail == NULL)
{
head = block;
tail = block;
}
else
{
tail->next = block;
tail = block;
}
}
return (void*)(block+1);
}
/**
*free
*@ptr: pointer to memory location to be freed
*returns: void
*/
void free(void *ptr)
{
if(ptr == NULL) return;
//ptr points to the user data
//find the meta block and set is_free=1
struct block_meta *curr_block = (struct block_meta*)ptr - 1;
if(curr_block->is_mmap == 1)
{
munmap(curr_block, curr_block->size + sizeof(struct block_meta) );
return;
}
curr_block->is_free=1;
//coalescing
backward_coalesce(curr_block);
forward_coalesce(curr_block);
}
/*prints heap contents */
void heap_dump()
{
struct block_meta *curr = head;
int i=0;
while(curr != NULL)
{
i++;
printf("Block %d: | Free: %d | Size: %zu | MMAP: %d\n",i, curr->is_free, curr->size, curr->is_mmap);
curr = curr->next;
}
}