source: mainline/uspace/lib/c/generic/mem.c@ 1433ecda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1433ecda was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2005 Martin Decky
3 * Copyright (c) 2008 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
41/** Fill memory block with a constant value. */
42void *memset(void *dest, int b, size_t n)
43{
44 char *pb;
45 unsigned long *pw;
46 size_t word_size;
47 size_t n_words;
48
49 unsigned long pattern;
50 size_t i;
51 size_t fill;
52
53 /* Fill initial segment. */
54 word_size = sizeof(unsigned long);
55 fill = word_size - ((uintptr_t) dest & (word_size - 1));
56 if (fill > n)
57 fill = n;
58
59 pb = dest;
60
61 i = fill;
62 while (i-- != 0)
63 *pb++ = b;
64
65 /* Compute remaining size. */
66 n -= fill;
67 if (n == 0)
68 return dest;
69
70 n_words = n / word_size;
71 n = n % word_size;
72 pw = (unsigned long *) pb;
73
74 /* Create word-sized pattern for aligned segment. */
75 pattern = 0;
76 i = word_size;
77 while (i-- != 0)
78 pattern = (pattern << 8) | (uint8_t) b;
79
80 /* Fill aligned segment. */
81 i = n_words;
82 while (i-- != 0)
83 *pw++ = pattern;
84
85 pb = (char *) pw;
86
87 /* Fill final segment. */
88 i = n;
89 while (i-- != 0)
90 *pb++ = b;
91
92 return dest;
93}
94
95struct along {
96 unsigned long n;
97} __attribute__((packed));
98
99static void *unaligned_memcpy(void *dst, const void *src, size_t n)
100{
101 size_t i, j;
102 struct along *adst = dst;
103 const struct along *asrc = src;
104
105 for (i = 0; i < n / sizeof(unsigned long); i++)
106 adst[i].n = asrc[i].n;
107
108 for (j = 0; j < n % sizeof(unsigned long); j++)
109 ((unsigned char *) (((unsigned long *) dst) + i))[j] =
110 ((unsigned char *) (((unsigned long *) src) + i))[j];
111
112 return (char *) dst;
113}
114
115/** Copy memory block. */
116void *memcpy(void *dst, const void *src, size_t n)
117{
118 size_t i;
119 size_t mod, fill;
120 size_t word_size;
121 size_t n_words;
122
123 const unsigned long *srcw;
124 unsigned long *dstw;
125 const uint8_t *srcb;
126 uint8_t *dstb;
127
128 word_size = sizeof(unsigned long);
129
130 /*
131 * Are source and destination addresses congruent modulo word_size?
132 * If not, use unaligned_memcpy().
133 */
134
135 if (((uintptr_t) dst & (word_size - 1)) !=
136 ((uintptr_t) src & (word_size - 1)))
137 return unaligned_memcpy(dst, src, n);
138
139 /*
140 * mod is the address modulo word size. fill is the length of the
141 * initial buffer segment before the first word boundary.
142 * If the buffer is very short, use unaligned_memcpy(), too.
143 */
144
145 mod = (uintptr_t) dst & (word_size - 1);
146 fill = word_size - mod;
147 if (fill > n)
148 fill = n;
149
150 /* Copy the initial segment. */
151
152 srcb = src;
153 dstb = dst;
154
155 i = fill;
156 while (i-- != 0)
157 *dstb++ = *srcb++;
158
159 /* Compute remaining length. */
160
161 n -= fill;
162 if (n == 0)
163 return dst;
164
165 /* Pointers to aligned segment. */
166
167 dstw = (unsigned long *) dstb;
168 srcw = (const unsigned long *) srcb;
169
170 n_words = n / word_size; /* Number of whole words to copy. */
171 n -= n_words * word_size; /* Remaining bytes at the end. */
172
173 /* "Fast" copy. */
174 i = n_words;
175 while (i-- != 0)
176 *dstw++ = *srcw++;
177
178 /*
179 * Copy the rest.
180 */
181
182 srcb = (const uint8_t *) srcw;
183 dstb = (uint8_t *) dstw;
184
185 i = n;
186 while (i-- != 0)
187 *dstb++ = *srcb++;
188
189 return dst;
190}
191
192/** Move memory block with possible overlapping. */
193void *memmove(void *dst, const void *src, size_t n)
194{
195 const uint8_t *sp;
196 uint8_t *dp;
197
198 /* Nothing to do? */
199 if (src == dst)
200 return dst;
201
202 /* Non-overlapping? */
203 if (dst >= src + n || src >= dst + n) {
204 return memcpy(dst, src, n);
205 }
206
207 /* Which direction? */
208 if (src > dst) {
209 /* Forwards. */
210 sp = src;
211 dp = dst;
212
213 while (n-- != 0)
214 *dp++ = *sp++;
215 } else {
216 /* Backwards. */
217 sp = src + (n - 1);
218 dp = dst + (n - 1);
219
220 while (n-- != 0)
221 *dp-- = *sp--;
222 }
223
224 return dst;
225}
226
227/** Compare two memory areas.
228 *
229 * @param s1 Pointer to the first area to compare.
230 * @param s2 Pointer to the second area to compare.
231 * @param len Size of the areas in bytes.
232 *
233 * @return Zero if areas have the same contents. If they differ,
234 * the sign of the result is the same as the sign of the
235 * difference of the first pair of different bytes.
236 *
237 */
238int memcmp(const void *s1, const void *s2, size_t len)
239{
240 uint8_t *u1 = (uint8_t *) s1;
241 uint8_t *u2 = (uint8_t *) s2;
242 size_t i;
243
244 for (i = 0; i < len; i++) {
245 if (*u1 != *u2)
246 return (int)(*u1) - (int)(*u2);
247 ++u1;
248 ++u2;
249 }
250
251 return 0;
252}
253
254/** @}
255 */
Note: See TracBrowser for help on using the repository browser.