-
Notifications
You must be signed in to change notification settings - Fork 2
/
realloc.c
60 lines (55 loc) · 1.21 KB
/
realloc.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
/*
** realloc.c for realloc in /home/nasrat_v/rendu/tek2/malloc/PSU_2020_malloc
**
** Made by Valentin Nasraty
** Login <valentin.nasraty@epitech.eu>
**
** Started on Mon Feb 6 17:34:49 2017 Valentin Nasraty
** Last update Sun Feb 12 20:28:29 2017 Valentin Nasraty
*/
#include "malloc.h"
void *base_bloc;
void *realloc_func(t_bloc *bloc, void *ptr, size_t size)
{
if (bloc->data == ptr)
{
if (size == bloc->getSize)
{
unlock_thread();
return (ptr);
}
if (split_bloc(bloc, size) == 1)
{
fusion_free_bloc(bloc);
if (split_bloc(bloc, size) == 1)
{
if ((ptr = copy_bloc(bloc, size)) == NULL)
return (malloc(size));
}
}
unlock_thread();
return (ptr);
}
unlock_thread();
return (malloc(size));
}
void *realloc(void *ptr, size_t size)
{
t_bloc *bloc;
trylock_thread();
if ((long int)size < 0)
return (unlock_thread());
size = __ALIGN_ADDR__(size, 16);
if ((ptr == NULL) || ((bloc = get_bloc(ptr)) == NULL))
{
unlock_thread();
return (malloc(size));
}
else if (size == 0)
{
unlock_thread();
free(ptr);
return (NULL);
}
return (realloc_func(bloc, ptr, size));
}