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
|
/* ****************************************************************************
* string/memset.c -- Initialize the content of a memory area.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of the 'all/core' module,
* in libcarrot, an experimental modular libc project.
*
* This file is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Lesser Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include <string.h>
#include <stdint.h>
/**
* setlil:
* Set a little zone of memory.
*
* @arg dword
*
* @arg dword the word to set.
* @arg full the character fullarized.
* @arg start the starting byte.
* @arg end the ending byte.
*/
static __inline void setlil(unsigned int *p, unsigned char c, size_t count)
{
switch (count % 8) {
case 7: *p++ = c;
case 6: *p++ = c;
case 5: *p++ = c;
case 4: *p++ = c;
case 3: *p++ = c;
case 2: *p++ = c;
case 1: *p++ = c;
case 0: break;
}
}
/**
* memset:
* Initialize a memory area with a value.
*
* @arg s the void thingy.
* @arg c the character.
* @arg n the size.
* @return the memory area.
*/
void *memset(void *s, int c, size_t n)
{
unsigned int lc;
__uintptr_t mask;
unsigned int *p;
int count;
if (!n) return (s);
/* prepare the full character */
lc = c & 0xFF;
lc |= lc << 8;
#if __SIZEOF_INT__ > 2
lc |= lc << 16;
# if __SIZEOF_INT__ > 4
lc |= lc << 32;
# endif
#endif
/* get the initial pointer */
mask = (__uintptr_t)&((unsigned int*)NULL)[1] - 1;
p = (unsigned int*)((__uintptr_t)s & ~mask) + 1;
count = (int)((__uintptr_t)p - (__uintptr_t)s);
n -= count;
/* initial bytes */
setlil(s, c, count);
/* main loop */
for (; n >= sizeof(unsigned int); n -= sizeof(unsigned int))
*p++ = lc;
/* final bytes */
setlil(p, c, n);
/* return destination */
return (s);
}
|