source: mainline/uspace/lib/c/generic/mem.c@ 1b20da0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1b20da0 was 1b20da0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on non-empty lines, in certain file types.

Command used: tools/srepl '\([^[:space:]]\)\s\+$' '\1' -- *.c *.h *.py *.sh *.s *.S *.ag

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