-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmem.c
More file actions
379 lines (323 loc) · 12.5 KB
/
mem.c
File metadata and controls
379 lines (323 loc) · 12.5 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include "mem.h"
//to print out extra logs
int DEBUG = 0;
void *Mem_Init(int sizeOfRegion)
{
head_ptr = mmap(NULL, sizeOfRegion, PROT_READ| PROT_WRITE , MAP_PRIVATE | MAP_ANON , -1 , 0);
if(head_ptr == MAP_FAILED)
{
printf("Mem_Init(): mmap returned NULL.\n");
return NULL;
}
else
{
//at the start of memory I am storing ref to 3 listS(mem_chuncks(by mmap), free_list and alloc_list)
struct header **mem_chunck_list = (struct header **)head_ptr;
struct header **free_list = (struct header **)(head_ptr + sizeof(struct header **));
struct header **alloc_list = (struct header **)(head_ptr + 2*sizeof(struct header **));
//at this moment only one chuck
*mem_chunck_list = NULL;
//because nothing allocated yet
*alloc_list = NULL;
//creating first free block
struct header *first_free_block = head_ptr + 3*sizeof(struct header **);
first_free_block->size =sizeOfRegion-3*sizeof(struct header **)-sizeof(struct header);
first_free_block->next = NULL;
*free_list = first_free_block;
if(DEBUG)
printf("Mem_Init(): (mmap_chuncks): %p, (free_list): %p, (alloc_list): %p\n", *mem_chunck_list, *free_list, *alloc_list);
return head_ptr;
}
}
int Mem_Free(void *ptr, int coalesce, int release)
{
if(ptr == NULL)
return 0;
//getting references to the heads of three lists
struct header **mem_chunck_list = (struct header **)head_ptr;
struct header **free_list = (struct header **)(head_ptr + sizeof(struct header **));
struct header **alloc_list = (struct header **)(head_ptr + 2*sizeof(struct header **));
//getting the address where the header starts
struct header *block_to_free = ptr - sizeof(struct header);
//find the block in the list
struct header *cur = *alloc_list, *prev = *alloc_list;
while (cur && cur != block_to_free)
{
prev = cur;
cur = cur->next;
}
if(DEBUG)
printf("Mem_Free: Addr: %p, Size: %d, Next: %p, Cur: %p, Prev: %p\n", block_to_free, block_to_free->size, block_to_free->next, cur, prev);
if(cur)
{
//remove from allocated list
if(cur == prev) // if there is the first in the list
{
*alloc_list = cur->next;
}
else // if in the middle of the list
{
prev->next = cur->next;
}
//add block at the start of free block
cur->next = *free_list;
*free_list = cur;
if(DEBUG)
printf("Mem_Free: free_list(%p), alloc_list(%p)\n", *free_list, *alloc_list);
}
else
{
//cases when pointer is not assigned by Mem_Alloc()
printf("Mem_Free: Could not free memory, invalid pointer.\n");
return -1;
}
if(coalesce)
{
//because recently freed block is added to the front of the list
struct header *freed_block = *free_list;
//traverse the list and find if adjacents blocks are free to coalesce
struct header *cur_block = (*free_list)->next;
struct header *prev_block = *free_list;
int counter = 0;
while(cur_block)
{
//when block before freed block is free
if((void *)cur_block + sizeof(struct header) + cur_block->size == freed_block)
{
//ditching the recetly created freed block
*free_list = (*free_list)->next;
cur_block->size = freed_block->size + sizeof(struct header) +cur_block->size;
counter++;
if(counter==2)
break;
}
//when block after freed block is free
else if ((void *)freed_block + sizeof(struct header) + freed_block->size == cur_block)
{
//ditching the block after the freed block
prev_block->next = cur_block->next;
freed_block->size = cur_block->size + sizeof(struct header) + freed_block->size;
counter++;
if(counter == 2)
break;
}
prev_block = cur_block;
cur_block = cur_block->next;
}
}
if(release)
{
int _continue;
struct header *cur_chunck = *mem_chunck_list, *prev_chunck = *mem_chunck_list;
while(cur_chunck)
{
struct header *possible_free_block = (void *)cur_chunck + sizeof(struct header);
struct header *cur_free_block = *free_list, *prev_free_block=*free_list;
_continue = 0;
while (cur_free_block)
{
//check if the there is any free block which is entirely on the free_chunck
if(cur_free_block == possible_free_block && cur_free_block->size == cur_chunck->size-2*sizeof(struct header))
{
void *mem_to_unmap = cur_chunck;
int length = cur_chunck->size;
//ditching the free chunck from mem_chunck_list
if(cur_chunck == prev_chunck)
{
*mem_chunck_list = cur_chunck->next;
cur_chunck = *mem_chunck_list;
prev_chunck = *mem_chunck_list;
}
else
{
prev_chunck->next = cur_chunck->next;
cur_chunck=prev_chunck->next;
}
_continue = 1;
//ditching the free block from free_list
if(prev_free_block == cur_free_block)
{
*free_list = cur_free_block->next;
cur_free_block = *free_list;
prev_free_block = *free_list;
}
else
{
prev_free_block->next = cur_free_block->next;
cur_free_block = prev_free_block->next;
}
//unmapping the memory
munmap(mem_to_unmap, length);
}
else
{
prev_free_block=cur_free_block;
cur_free_block = cur_free_block->next;
}
}
if(_continue)
continue;
prev_chunck = cur_chunck;
cur_chunck = cur_chunck->next;
}
}
return 0;
}
void *Mem_Alloc(int size, int expand)
{
//if user request memory of size 0 or less
if(size <= 0)
return NULL;
struct header **mem_chunck_list;
struct header **free_list;
struct header **alloc_list;
retry_allocation:
//getting references of the lists
mem_chunck_list = (struct header **)head_ptr;
free_list = (struct header **)(head_ptr + sizeof(struct header **));
alloc_list = (struct header **)(head_ptr + 2*sizeof(struct header **));
struct header *next_free_block;
struct header *new_free_block;
struct header *prev_free_block;
struct header *cur_free_block;
//find the next suitable block by traversing the free list
cur_free_block = *free_list;
prev_free_block = *free_list;
while(cur_free_block && cur_free_block->next)
{
if (cur_free_block->size == size)
{
break;
}
else if(cur_free_block->size >= size + sizeof(struct header))
{
break;
}
else
{
prev_free_block = cur_free_block;
cur_free_block = cur_free_block->next;
}
}
if(cur_free_block && ((cur_free_block->size >= (size + sizeof(struct header))) || (cur_free_block->size == size)))
{
next_free_block = cur_free_block->next;
//if found block has excess memory than required
if(cur_free_block->size >= size + sizeof(struct header))
{
new_free_block = (struct header *) ((void *)cur_free_block + size + sizeof(struct header));
new_free_block->size = cur_free_block->size-size-sizeof(struct header);
new_free_block->next = next_free_block;
if(cur_free_block == prev_free_block) //means what we had only one block
{
*free_list = new_free_block;
}
else
{
prev_free_block->next = new_free_block;
}
}
else //found block has exactly the same memory
{
if(cur_free_block == prev_free_block)
{
*free_list = next_free_block;
}
else
{
prev_free_block->next = next_free_block;
}
}
//Now adding the given memory block to allocated-list
cur_free_block->size = size;
if(*alloc_list==NULL)
{
*alloc_list=cur_free_block;
cur_free_block->next = NULL;
}
else //adding new block to the front of the list
{
cur_free_block->next = *alloc_list;
*alloc_list = cur_free_block;
}
if(DEBUG)
{
printf("Mem_Alloc: Block Assigned: %p, Size: %d, Next: %p\n", cur_free_block, cur_free_block->size, cur_free_block->next);
printf("Mem_Alloc: free_list(%p), alloc_list(%p)\n", *free_list, *alloc_list);
}
return (void *)cur_free_block + sizeof(struct header);
}
else
{
if(expand)
{
//calculating the page aligned size for new memory allocation
int required_size = size + 2*sizeof(struct header);
int page_size = sysconf(_SC_PAGE_SIZE);
int new_mem_size = page_size + required_size;
int a = new_mem_size % page_size;
new_mem_size = new_mem_size - a;
void *new_chunck = mmap(NULL, new_mem_size, PROT_READ| PROT_WRITE , MAP_PRIVATE | MAP_ANON , -1 , 0);
struct header *chunck_header = new_chunck;
chunck_header->size = new_mem_size; //original chuck size as given by mmap() (excluding size of headers)
if(*mem_chunck_list == NULL) //means first extra chunck
{
chunck_header->next = NULL;
*mem_chunck_list = chunck_header;
}
else //already have extra chucks
{
chunck_header->next = *mem_chunck_list;
*mem_chunck_list = chunck_header;
}
//creating new free block on newly allocated chunck
struct header *new_free_block = new_chunck + sizeof(struct header);
new_free_block->size = new_mem_size - 2*sizeof(struct header);
new_free_block->next = *free_list;
*free_list = new_free_block;
//now that new memory is allocated, try allocating the memory again
goto retry_allocation;
}
else
{
printf("Mem_Alloc(expand=0): Cannot provide memory of %d bytes.\n", size);
return NULL;
}
}
}
void Mem_Dump()
{
struct header **mem_chunck_list = (struct header **)head_ptr;
struct header **free_list = (struct header **)(head_ptr+sizeof(struct header **));
struct header **alloc_list = (struct header **)(head_ptr + 2*sizeof(struct header **));
printf("----------Free List (head: %p)-------------", *free_list);
if(*free_list == NULL) printf("---------\n"); else printf("\n");
struct header *cur = *free_list;
while(cur)
{
printf("Address: %p, Size: %d, Next: %p\n", cur, cur->size, cur->next);
cur = cur->next;
}
printf("-------------------------------------------------------\n");
printf("-------Allocated List (head: %p)-----------", *alloc_list);
if(*alloc_list == NULL) printf("---------\n"); else printf("\n");
cur = *alloc_list;
while(cur)
{
printf("Address: %p, Size: %d, Next: %p\n", cur, cur->size, cur->next);
cur = cur->next;
}
printf("-------------------------------------------------------\n");
printf("--------Chunck List (head: %p)-------------", *mem_chunck_list);
if(*mem_chunck_list == NULL) printf("---------\n"); else printf("\n");
cur = *mem_chunck_list;
while(cur)
{
printf("Address: %p, Size: %d, Next: %p\n", cur, cur->size, cur->next);
cur = cur->next;
}
printf("-------------------------------------------------------\n");
}