-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealloc.c
More file actions
68 lines (61 loc) · 2.05 KB
/
realloc.c
File metadata and controls
68 lines (61 loc) · 2.05 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* realloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ffloris <ffloris@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/03 13:39:16 by ffloris #+# #+# */
/* Updated: 2019/02/26 21:05:24 by ffloris ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc.h"
static void *reallocate_large_block(void *ptr, size_t size)
{
t_zone *zone;
zone = (t_zone*)ptr - 1;
if (zone->size - LARGE_ZONE_HEADER_SIZE >= size)
return (ptr);
if (!(ptr = allocate(size)))
return (NULL);
ft_memcpy(ptr, zone + 1, zone->size - LARGE_ZONE_HEADER_SIZE);
free_large_block(zone + 1);
return (ptr);
}
static void *reallocate_block(void *ptr, size_t size)
{
t_zone_type zone_type;
t_block *block;
t_zone *zone;
block = get_block_info(ptr, &zone, &zone_type);
if (IS_LARGE_ZONE(zone_type))
return (reallocate_large_block(ptr, size));
if (!block)
return (NULL);
if (BLOCK_SIZE(block->size_log2) - sizeof(t_block) >= size)
return (ptr);
if (!(ptr = allocate(size)))
return (NULL);
ft_memcpy(ptr, block + 1,
BLOCK_SIZE(block->size_log2) - sizeof(t_block));
free_block(block + 1);
return (ptr);
}
static void *reallocate(void *ptr, size_t size)
{
if (!ptr)
return (allocate(size));
if (!size)
{
free_block(ptr);
return (NULL);
}
return (reallocate_block(ptr, size));
}
void *realloc(void *ptr, size_t size)
{
pthread_mutex_lock(&g_mutex);
ptr = reallocate(ptr, size);
pthread_mutex_unlock(&g_mutex);
return (ptr);
}