PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
memset_s.c
Go to the documentation of this file.
2#include <errno.h>
3#include <stdint.h>
4#include <stdlib.h>
5#include <string.h>
6
8
9errno_t memset_s(void* s, rsize_t smax, int c, rsize_t n)
10{
11 uint8_t* p = (uint8_t*)s;
12
13 if (s == NULL || smax > RSIZE_MAX || n > RSIZE_MAX || n > smax)
14 {
15 if (s != NULL && smax <= RSIZE_MAX)
16 {
17 uint8_t* zeroP = (uint8_t*)s;
18 rsize_t zeroN = smax;
19
20 while (((uintptr_t)zeroP & 7) && zeroN)
21 {
22 *zeroP++ = 0;
23 zeroN--;
24 }
25
26 while (zeroN >= 64)
27 {
28 *(uint64_t*)(zeroP + 0) = 0;
29 *(uint64_t*)(zeroP + 8) = 0;
30 *(uint64_t*)(zeroP + 16) = 0;
31 *(uint64_t*)(zeroP + 24) = 0;
32 *(uint64_t*)(zeroP + 32) = 0;
33 *(uint64_t*)(zeroP + 40) = 0;
34 *(uint64_t*)(zeroP + 48) = 0;
35 *(uint64_t*)(zeroP + 56) = 0;
36 zeroP += 64;
37 zeroN -= 64;
38 }
39
40 while (zeroN >= 8)
41 {
42 *(uint64_t*)zeroP = 0;
43 zeroP += 8;
44 zeroN -= 8;
45 }
46
47 while (zeroN--)
48 {
49 *zeroP++ = 0;
50 }
51 }
52
54 return EINVAL;
55 }
56
57 uint8_t ch8 = (uint8_t)c;
58 uint64_t ch64 = ((uint64_t)ch8) | ((uint64_t)ch8 << 8) | ((uint64_t)ch8 << 16) | ((uint64_t)ch8 << 24) |
59 ((uint64_t)ch8 << 32) | ((uint64_t)ch8 << 40) | ((uint64_t)ch8 << 48) | ((uint64_t)ch8 << 56);
60
61 while (((uintptr_t)p & 7) && n)
62 {
63 *p++ = ch8;
64 n--;
65 }
66
67 while (n >= 64)
68 {
69 *(uint64_t*)(p + 0) = ch64;
70 *(uint64_t*)(p + 8) = ch64;
71 *(uint64_t*)(p + 16) = ch64;
72 *(uint64_t*)(p + 24) = ch64;
73 *(uint64_t*)(p + 32) = ch64;
74 *(uint64_t*)(p + 40) = ch64;
75 *(uint64_t*)(p + 48) = ch64;
76 *(uint64_t*)(p + 56) = ch64;
77 p += 64;
78 n -= 64;
79 }
80
81 while (n >= 8)
82 {
83 *(uint64_t*)p = ch64;
84 p += 8;
85 n -= 8;
86 }
87
88 while (n--)
89 {
90 *p++ = ch8;
91 }
92
93 return 0;
94}
constraint_handler_t _constraintHandler
#define _CONSTRAINT_VIOLATION(e)
int errno_t
Definition errno_t.h:4
#define EINVAL
Invalid argument.
Definition errno.h:142
#define NULL
Pointer error value.
Definition NULL.h:23
errno_t memset_s(void *s, rsize_t smax, int c, rsize_t n)
Definition memset_s.c:9
__SIZE_TYPE__ rsize_t
Definition rsize_t.h:4
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43