-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_memset.c
101 lines (90 loc) · 2.33 KB
/
ft_memset.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
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadhesiv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/04 17:49:01 by oadhesiv #+# #+# */
/* Updated: 2021/01/06 18:00:00 by oadhesiv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_ulong make_memory_cell(int c)
{
t_ulong ret;
t_byte i;
ret = (t_byte)c;
i = 1;
while (i < sizeof(long))
ret |= ret << 8 * i++;
return (ret);
}
static void align_pointer(void **b, int c, size_t *len)
{
t_byte *dest;
dest = (t_byte*)*b;
while ((t_ulong)dest % sizeof(long) != 0 && *len > 0)
{
*dest++ = (t_byte)c;
*len -= 1;
}
*b = (void*)dest;
}
static size_t fill_eight_ulongs(void **b, t_ulong cell, size_t len)
{
t_ulong *dest;
size_t ops;
size_t size;
dest = (t_ulong*)*b;
size = 8 * sizeof(long);
ops = len / size;
while (ops--)
{
*dest = cell;
*(dest + 1) = cell;
*(dest + 2) = cell;
*(dest + 3) = cell;
*(dest + 4) = cell;
*(dest + 5) = cell;
*(dest + 6) = cell;
*(dest + 7) = cell;
dest += 8;
len -= size;
}
*b = (void*)dest;
return (len);
}
static size_t fill_ulong(void **b, t_ulong cell, size_t len)
{
t_ulong *dest;
size_t ops;
dest = (t_ulong*)*b;
ops = len / sizeof(long);
while (ops--)
{
*dest++ = cell;
len -= sizeof(long);
}
*b = (void*)dest;
return (len);
}
void *ft_memset(void *b, int c, size_t len)
{
t_ulong cell;
t_ulong *dest;
t_byte *dest_byte;
void *ret;
if (!b || !len)
return (b);
ret = b;
cell = make_memory_cell(c);
align_pointer(&b, c, &len);
dest = (t_ulong*)b;
len = fill_eight_ulongs((void**)&dest, cell, len);
len = fill_ulong((void**)&dest, cell, len);
dest_byte = (t_byte*)dest;
while (len--)
*dest_byte++ = (t_byte)c;
return (ret);
}