-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
67 lines (61 loc) · 1.57 KB
/
utils.c
File metadata and controls
67 lines (61 loc) · 1.57 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
/*
** EPITECH PROJECT, 2021
** ${FOLDER_NAME}
** File description:
** utils.c file
*/
#include <string.h>
#include "my_mem.h"
size_t append_frees(memblock_t *ptr, size_t needed, int *appened)
{
if (needed <= 0) {
return 0;
} else if (ptr->_next && ptr->_next->_free == 'Y') {
(*appened)++;
return ptr->_size +
append_frees(ptr->_next, needed - ptr->_size, appened);
} else
return ptr->_size;
}
void *find_mem(size_t size)
{
memblock_t **cursor = my_blocks();
memblock_t *ret = NULL;
for (; *cursor && (*cursor)->_next; cursor = &(*cursor)->_next) {
if ((*cursor)->_free != 'Y')
continue;
size_t c_size = (*cursor)->_size;
if (!ret && c_size >= size)
ret = (*cursor);
else if (ret && c_size >= size && ret->_size < c_size)
ret = *cursor;
}
return ret;
}
void *init_memory(size_t size, void *ptr)
{
memblock_t ret;
ret._id = 0x6d616c6c6f63;
ret._size = size;
ret._free = 'N';
ret._ptr = (void *) (((long) ptr) + sizeof(memblock_t));
ret._next = NULL;
memcpy(ptr, &ret, sizeof(memblock_t));
memblock_t **btr = my_blocks();
for (; *btr && (*btr)->_next; btr = &(*btr)->_next);
if (*btr) {
((memblock_t *) ptr)->_prev = *btr;
(*btr)->_next = ptr;
} else {
((memblock_t *) ptr)->_prev = NULL;
*btr = (memblock_t *) ptr;
}
return ret._ptr;
}
void fetch_mem(void)
{
void *expand = sbrk(getpagesize() * 2);
if (expand == (void *) -1)
abort();
end_size(sbrk(0));
}