-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_realloc.c
45 lines (40 loc) · 973 Bytes
/
my_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
/*
** EPITECH PROJECT, 2021
** B-PSU-400-RUN-4-1-malloc-paul.brancieq
** File description:
** my_realloc
*/
#include "include/my_malloc.h"
void *new_alloc(void *ptr, size_t old_size, size_t size)
{
char *new;
char *old = ptr;
free(ptr);
new = malloc(size);
if (new == NULL) {
return (NULL);
}
for (int i = 0; i != size; i++) {
if (i < old_size)
new[i] = old[i];
}
return new;
}
void *realloc(void *ptr, size_t size)
{
my_malloc_t *to_rea = NULL;
size_t total_size;
if (ptr == NULL)
return malloc(size);
to_rea = ptr - sizeof(my_malloc_t);
if ((to_rea->next != NULL && to_rea->next->state == FREE &&
to_rea->b_offset + sizeof(my_malloc_t) >= size) ||
(size <= to_rea->b_offset)) {
free(ptr);
to_rea = use_space(size, to_rea);
return to_rea->memory;
} else {
ptr = new_alloc(ptr, to_rea->b_offset, size);
return (ptr);
}
}