This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_malloc.c
91 lines (81 loc) · 2.15 KB
/
my_malloc.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
/*
** my_malloc.c for C in /home/berny_m/rendu/PSU_2015_malloc
**
** Made by Berny Matias
** Login <berny_m@epitech.net>
**
** Started on Wed Jan 27 15:36:43 2016 Berny Matias
** Last update Wed Feb 10 16:51:16 2016 Delacour Benjamin
*/
#include "my_malloc.h"
static void init_new_elem(t_list *new_elem, size_t size)
{
new_elem->used = TRUE;
new_elem->alloc = size;
new_elem->next = NULL;
new_elem->prev = g_alloc_list_end;
new_elem->next_free = NULL;
if (g_alloc_list == NULL)
g_alloc_list = new_elem;
else
g_alloc_list_end->next = new_elem;
g_alloc_list_end = new_elem;
}
static t_list *alloc_new(size_t size)
{
t_list *new_elem;
if (g_alloc_list == NULL || g_alloc_list_end->used == TRUE)
{
if ((new_elem = sbrk((getpagesize()
* ((GETMULT(size) + sizeof(t_list))
/ getpagesize() + 1)))) == (void *)-1)
return (NULL);
init_new_elem(new_elem, size);
if (sbrk(0) - (void *)(new_elem + 1) - GETMULT(size) > sizeof(t_list))
divide_elem(new_elem, size);
return (new_elem);
}
if ((new_elem = sbrk((getpagesize() * ((GETMULT(size)
- g_alloc_list_end->alloc)
/ getpagesize() + 1)))) == (void *)-1)
return (NULL);
g_alloc_list_end->alloc = my_elem_size(g_alloc_list_end);
return (g_alloc_list_end);
}
static void *alloc_at(t_list *list, size_t size)
{
t_list *prev;
prev = g_alloc_list;
while (prev->next_free && prev->next_free < list)
prev = prev->next_free;
if (prev)
prev->next_free = list->next_free;
list->used = TRUE;
list->next_free = NULL;
list->alloc = size;
if (my_elem_size(list) - GETMULT(size) >= sizeof(t_list))
divide_elem(list, size);
return (list + 1);
}
void *malloc_intern(size_t size)
{
t_list *list;
list = g_alloc_list;
if ((list = best_fit(size)) != NULL)
return (alloc_at(list, size));
if ((list = alloc_new(size)) == NULL)
return (NULL);
if (list->used == TRUE)
return (list + 1);
return (alloc_at(list, size));
}
void *malloc(size_t size)
{
void *res;
if (lock_mutex() == FALSE)
return (NULL);
res = malloc_intern(size);
if (unlock_mutex() == FALSE)
return (NULL);
return (res);
}