1 | /*
|
---|
2 | * Copyright (c) 2005 Martin Decky
|
---|
3 | * Copyright (c) 2018 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <mem.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <stddef.h>
|
---|
39 | #include <stdint.h>
|
---|
40 | #include "private/cc.h"
|
---|
41 |
|
---|
42 | /** Fill memory block with a constant value. */
|
---|
43 | ATTRIBUTE_OPTIMIZE_NO_TLDP
|
---|
44 | void *memset(void *dest, int b, size_t n)
|
---|
45 | {
|
---|
46 | char *pb;
|
---|
47 | unsigned long *pw;
|
---|
48 | size_t word_size;
|
---|
49 | size_t n_words;
|
---|
50 |
|
---|
51 | unsigned long pattern;
|
---|
52 | size_t i;
|
---|
53 | size_t fill;
|
---|
54 |
|
---|
55 | /* Fill initial segment. */
|
---|
56 | word_size = sizeof(unsigned long);
|
---|
57 | fill = word_size - ((uintptr_t) dest & (word_size - 1));
|
---|
58 | if (fill > n)
|
---|
59 | fill = n;
|
---|
60 |
|
---|
61 | pb = dest;
|
---|
62 |
|
---|
63 | i = fill;
|
---|
64 | while (i-- != 0)
|
---|
65 | *pb++ = b;
|
---|
66 |
|
---|
67 | /* Compute remaining size. */
|
---|
68 | n -= fill;
|
---|
69 | if (n == 0)
|
---|
70 | return dest;
|
---|
71 |
|
---|
72 | n_words = n / word_size;
|
---|
73 | n = n % word_size;
|
---|
74 | pw = (unsigned long *) pb;
|
---|
75 |
|
---|
76 | /* Create word-sized pattern for aligned segment. */
|
---|
77 | pattern = 0;
|
---|
78 | i = word_size;
|
---|
79 | while (i-- != 0)
|
---|
80 | pattern = (pattern << 8) | (uint8_t) b;
|
---|
81 |
|
---|
82 | /* Fill aligned segment. */
|
---|
83 | i = n_words;
|
---|
84 | while (i-- != 0)
|
---|
85 | *pw++ = pattern;
|
---|
86 |
|
---|
87 | pb = (char *) pw;
|
---|
88 |
|
---|
89 | /* Fill final segment. */
|
---|
90 | i = n;
|
---|
91 | while (i-- != 0)
|
---|
92 | *pb++ = b;
|
---|
93 |
|
---|
94 | return dest;
|
---|
95 | }
|
---|
96 |
|
---|
97 | struct along {
|
---|
98 | unsigned long n;
|
---|
99 | } __attribute__((packed));
|
---|
100 |
|
---|
101 | static void *unaligned_memcpy(void *dst, const void *src, size_t n)
|
---|
102 | {
|
---|
103 | size_t i, j;
|
---|
104 | struct along *adst = dst;
|
---|
105 | const struct along *asrc = src;
|
---|
106 |
|
---|
107 | for (i = 0; i < n / sizeof(unsigned long); i++)
|
---|
108 | adst[i].n = asrc[i].n;
|
---|
109 |
|
---|
110 | for (j = 0; j < n % sizeof(unsigned long); j++)
|
---|
111 | ((unsigned char *) (((unsigned long *) dst) + i))[j] =
|
---|
112 | ((unsigned char *) (((unsigned long *) src) + i))[j];
|
---|
113 |
|
---|
114 | return (char *) dst;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Copy memory block. */
|
---|
118 | ATTRIBUTE_OPTIMIZE_NO_TLDP
|
---|
119 | void *memcpy(void *dst, const void *src, size_t n)
|
---|
120 | {
|
---|
121 | size_t i;
|
---|
122 | size_t mod, fill;
|
---|
123 | size_t word_size;
|
---|
124 | size_t n_words;
|
---|
125 |
|
---|
126 | const unsigned long *srcw;
|
---|
127 | unsigned long *dstw;
|
---|
128 | const uint8_t *srcb;
|
---|
129 | uint8_t *dstb;
|
---|
130 |
|
---|
131 | word_size = sizeof(unsigned long);
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Are source and destination addresses congruent modulo word_size?
|
---|
135 | * If not, use unaligned_memcpy().
|
---|
136 | */
|
---|
137 |
|
---|
138 | if (((uintptr_t) dst & (word_size - 1)) !=
|
---|
139 | ((uintptr_t) src & (word_size - 1)))
|
---|
140 | return unaligned_memcpy(dst, src, n);
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * mod is the address modulo word size. fill is the length of the
|
---|
144 | * initial buffer segment before the first word boundary.
|
---|
145 | * If the buffer is very short, use unaligned_memcpy(), too.
|
---|
146 | */
|
---|
147 |
|
---|
148 | mod = (uintptr_t) dst & (word_size - 1);
|
---|
149 | fill = word_size - mod;
|
---|
150 | if (fill > n)
|
---|
151 | fill = n;
|
---|
152 |
|
---|
153 | /* Copy the initial segment. */
|
---|
154 |
|
---|
155 | srcb = src;
|
---|
156 | dstb = dst;
|
---|
157 |
|
---|
158 | i = fill;
|
---|
159 | while (i-- != 0)
|
---|
160 | *dstb++ = *srcb++;
|
---|
161 |
|
---|
162 | /* Compute remaining length. */
|
---|
163 |
|
---|
164 | n -= fill;
|
---|
165 | if (n == 0)
|
---|
166 | return dst;
|
---|
167 |
|
---|
168 | /* Pointers to aligned segment. */
|
---|
169 |
|
---|
170 | dstw = (unsigned long *) dstb;
|
---|
171 | srcw = (const unsigned long *) srcb;
|
---|
172 |
|
---|
173 | n_words = n / word_size; /* Number of whole words to copy. */
|
---|
174 | n -= n_words * word_size; /* Remaining bytes at the end. */
|
---|
175 |
|
---|
176 | /* "Fast" copy. */
|
---|
177 | i = n_words;
|
---|
178 | while (i-- != 0)
|
---|
179 | *dstw++ = *srcw++;
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Copy the rest.
|
---|
183 | */
|
---|
184 |
|
---|
185 | srcb = (const uint8_t *) srcw;
|
---|
186 | dstb = (uint8_t *) dstw;
|
---|
187 |
|
---|
188 | i = n;
|
---|
189 | while (i-- != 0)
|
---|
190 | *dstb++ = *srcb++;
|
---|
191 |
|
---|
192 | return dst;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Move memory block with possible overlapping. */
|
---|
196 | void *memmove(void *dst, const void *src, size_t n)
|
---|
197 | {
|
---|
198 | const uint8_t *sp;
|
---|
199 | uint8_t *dp;
|
---|
200 |
|
---|
201 | /* Nothing to do? */
|
---|
202 | if (src == dst)
|
---|
203 | return dst;
|
---|
204 |
|
---|
205 | /* Non-overlapping? */
|
---|
206 | if (dst >= src + n || src >= dst + n) {
|
---|
207 | return memcpy(dst, src, n);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* Which direction? */
|
---|
211 | if (src > dst) {
|
---|
212 | /* Forwards. */
|
---|
213 | sp = src;
|
---|
214 | dp = dst;
|
---|
215 |
|
---|
216 | while (n-- != 0)
|
---|
217 | *dp++ = *sp++;
|
---|
218 | } else {
|
---|
219 | /* Backwards. */
|
---|
220 | sp = src + (n - 1);
|
---|
221 | dp = dst + (n - 1);
|
---|
222 |
|
---|
223 | while (n-- != 0)
|
---|
224 | *dp-- = *sp--;
|
---|
225 | }
|
---|
226 |
|
---|
227 | return dst;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** Compare two memory areas.
|
---|
231 | *
|
---|
232 | * @param s1 Pointer to the first area to compare.
|
---|
233 | * @param s2 Pointer to the second area to compare.
|
---|
234 | * @param len Size of the areas in bytes.
|
---|
235 | *
|
---|
236 | * @return Zero if areas have the same contents. If they differ,
|
---|
237 | * the sign of the result is the same as the sign of the
|
---|
238 | * difference of the first pair of different bytes.
|
---|
239 | *
|
---|
240 | */
|
---|
241 | int memcmp(const void *s1, const void *s2, size_t len)
|
---|
242 | {
|
---|
243 | uint8_t *u1 = (uint8_t *) s1;
|
---|
244 | uint8_t *u2 = (uint8_t *) s2;
|
---|
245 | size_t i;
|
---|
246 |
|
---|
247 | for (i = 0; i < len; i++) {
|
---|
248 | if (*u1 != *u2)
|
---|
249 | return (int)(*u1) - (int)(*u2);
|
---|
250 | ++u1;
|
---|
251 | ++u2;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return 0;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Search memory area.
|
---|
258 | *
|
---|
259 | * @param s Memory area
|
---|
260 | * @param c Character (byte) to search for
|
---|
261 | * @param n Size of memory area in bytes
|
---|
262 | *
|
---|
263 | * @return Pointer to the first occurrence of @a c in the first @a n
|
---|
264 | * bytes of @a s or @c NULL if not found.
|
---|
265 | */
|
---|
266 | void *memchr(const void *s, int c, size_t n)
|
---|
267 | {
|
---|
268 | uint8_t *u = (uint8_t *) s;
|
---|
269 | unsigned char uc = (unsigned char) c;
|
---|
270 | size_t i;
|
---|
271 |
|
---|
272 | for (i = 0; i < n; i++) {
|
---|
273 | if (u[i] == uc)
|
---|
274 | return (void *) &u[i];
|
---|
275 | }
|
---|
276 |
|
---|
277 | return NULL;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /** @}
|
---|
281 | */
|
---|